Proper Use of Java Logging Systems and Frameworks
To correctly implement Java logging, choose a compatible framework‑system pair such as SLF4J with Logback, configure Maven dependencies and a rolling Logback XML, use MDC for context, enable dynamic level changes, avoid custom wrappers, and follow a checklist for cleanup and troubleshooting.
This article explains how to correctly use logging systems in Java applications, covering dependencies, output, cleanup, troubleshooting, and alerting.
Logging Systems and Frameworks
Common logging systems include:
JUL (java.util.logging)
Log4j (https://logging.apache.org/, now at 2.x)
Logback (http://logback.qos.ch/)
Logging frameworks that provide a unified API:
JCL (Apache Commons Logging)
SLF4J (https://www.slf4j.org/)
Frameworks do not output logs themselves; they delegate to a logging system. The typical modern stack is SLF4J + Logback .
Correct Combination of Systems and Frameworks
Some combinations are mutually exclusive (e.g., logback and slf4j‑log4j12 cannot coexist). Recommended combination:
SLF4J + Logback (with bridges for JCL, Log4j, JUL)
Example Maven dependencyManagement and dependencies for the SLF4J+Logback stack (the version placeholders are intentional placeholders to exclude unwanted transitive dependencies):
<properties>
<slf4j.version>${xxxx}</slf4j.version>
<logback.version>${yyyyy}</logback.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>999-not-exist</version>
</dependency>
... (other placeholder dependencies) ...
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
</dependencies>Logback Configuration Samples
Typical logback‑spring.xml for Spring Boot (or logback.xml for plain Java) includes property definitions, a rolling file appender with SizeAndTimeBasedRollingPolicy , and logger definitions:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="APP_NAME" value="demo1" />
<property name="LOG_PATH" value="${user.home}/${APP_NAME}/logs" />
<property name="LOG_FILE" value="${LOG_PATH}/application.log" />
<property name="LOG.PATTERN" value="%d %-5level %X{EAGLEEYE_TRACE_ID} %logger{5}[%L] - %msg%n" />
<appender name="APPLICATION" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_FILE}</file>
<encoder>
<pattern>${LOG.PATTERN}</pattern>
<charset>UTF-8</charset>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${LOG_FILE}.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxHistory>7</maxHistory>
<maxFileSize>50MB</maxFileSize>
<totalSizeCap>20GB</totalSizeCap>
</rollingPolicy>
</appender>
... (logger definitions) ...
<root level="error">
<appender-ref ref="APPLICATION" />
</root>
</configuration>Practical Tips
Dynamic log level change (e.g., via Arthas logger command ).
Enrich logs with MDC key‑value pairs (e.g., MDC.put("traceId", UUID.randomUUID().toString().replace("-", "")); and use %X{traceId} in the pattern).
Configure log cleanup with rolling policies to avoid disk exhaustion.
Avoid wrapping the logging framework in a custom LogUtil ; use the framework API directly to retain correct source information.
Troubleshooting Checklist
Identify the exact logging framework + system combination used.
Ensure Maven dependencies match the intended combination (remove conflicting bridges).
Verify that the logback.xml (or logback‑spring.xml ) is placed in the correct classpath location.
Check logger definitions and levels; make sure they are not set to a higher level than desired.
Proper use of logging systems greatly simplifies maintenance and issue diagnosis in Java applications.
DaTaobao Tech
Official account of DaTaobao Technology
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.