Appearance
SLF4J and Logback
Commons Logging and Log4j were introduced earlier. They are good friends. One of them is responsible for serving as the logging API, and the other is responsible for implementing the bottom layer of the log. They are very convenient for development when used together.
Some children's shoes may have heard of SLF4J and Logback. These two things also look like logs, what are they?
In fact, SLF4J is similar to Commons Logging and is also a logging interface, while Logback is similar to Log4j and is a log implementation.
Why did SLF4J and Logback pop up after Commons Logging and Log4j? This is because Java has a very long history of open source. Not only is OpenJDK itself open source, but almost all third-party libraries we use are open source. One specific feature of the rich open source ecosystem is that you can find several competing open source libraries for the same function.
Because they were dissatisfied with the interface of Commons Logging, someone developed SLF4J. Because they were dissatisfied with the performance of Log4j, someone started working on Logback.
Let’s first take a look at how SLF4J has improved the Commons Logging interface. In Commons Logging, we want to print logs, sometimes we have to write like this:
java
int score = 99;
p.setScore(score);
log.info("Set score " + score + " for Person " + p.getName() + " ok.");
Spelling strings is a very troublesome thing, so SLF4J’s log interface has been improved to this:
java
int score = 99;
p.setScore(score);
logger.info("Set score {} for Person {} ok.", score, p.getName());
We can guess it by guessing. SLF4J's log interface passes in a string with a placeholder, and the placeholder is automatically replaced with the following variables, so it looks more natural.
How to use SLF4J? Its interface is actually almost identical to Commons Logging:
java
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
class Main {
final Logger logger = LoggerFactory.getLogger(getClass());
}
Compare the interfaces of Commons Logging and SLF4J:
Commons Logging | SLF4J |
---|---|
org.apache.commons.logging.Log | org.slf4j.Logger |
org.apache.commons.logging.LogFactory | org.slf4j.LoggerFactory |
The difference is that Log
becomes Logger
and LogFactory
becomes LoggerFactory
.
Using SLF4J and Logback is similar to using Commons Logging plus Log4j mentioned earlier. First download SLF4J and Logback respectively, and then put the following jar packages on the classpath:
- slf4j-api-1.7.x.jar
- logback-classic-1.2.x.jar
- logback-core-1.2.x.jar
Then use SLF4J's Logger
and LoggerFactory
.
Similar to Log4j, we still need a Logback configuration file. Put logback.xml
on the classpath and configure it as follows:
xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
<charset>utf-8</charset>
</encoder>
<file>log/output.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>log/output.log.%i</fileNamePattern>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>1MB</MaxFileSize>
</triggeringPolicy>
</appender>
<root level="INFO">
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE" />
</root>
</configuration>
Run to get output similar to the following:
13:15:25.328 [main] INFO com.itranswarp.learnjava.Main - Start process...
Judging from the current trend, more and more open source projects are switching from Commons Logging plus Log4j to SLF4J plus Logback.
Summary
SLF4J and Logback can replace Commons Logging and Log4j;
Always use the SLF4J interface to write logs. Using Logback only requires configuration and no code modification.