The good friends Commons Logging and Log4j are introduced earlier. One of them is responsible for the log API, and the other is responsible for implementing the bottom layer of the log. It is very easy to develop when used together.
Some children’s shoes may also 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 log interface, while Logback is similar to Log4j and is a log implementation.
Why with Commons Logging and Log4j, SLF4J and Logback pop up again? This is because Java has a very long history of open source. Not only is OpenJDK itself open source, but almost all of the third-party libraries we use are open source. A particular feature of the rich open source ecosystem is that for the same function, several competing open source libraries can be found.
Because of dissatisfaction with the interface of Commons Logging, some people engaged in SLF4J. Because of dissatisfaction with the performance of Log4j, some people engaged in Logback.
Let’s first take a look at how SLF4J improves the Commons Logging interface. In Commons Logging, we want to print the log, sometimes we have to write:
1 2 3 4 |
int score = 99; p.setScore(score); log.info("Set score " + score + " for Person " + p.getName() + " ok."); |
Spelling strings is a very troublesome thing, so the log interface of SLF4J has been improved as follows:
1 2 3 4 |
int score = 99; p.setScore(score); logger.info("Set score {} for Person {} ok.", score, p.getName()); |
We can guess by guessing. The log interface of SLF4J passes in a string with placeholders, and the placeholders are automatically replaced with the following variables, so it looks more natural.
How to use SLF4J? Its interface is actually almost identical to Commons Logging:
1 2 3 4 5 6 7 |
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 and Log4j as 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, logback.xml
put it on the classpath, and configure it as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<?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 it to get output similar to the following:
1 2 |
13:15:25.328 [main] INFO com.itranswarp.learnjava.Main - Start process... |
Judging from the current trend, more and more open source projects have shifted from Commons Logging plus Log4j to SLF4J plus Logback.
practise
According to the configuration file, observe the log files written by Logback.
summary
SLF4J and Logback can replace Commons Logging and Log4j;
Always use SLF4J’s interface to write logs. Using Logback only requires configuration and no code modification.