1. Understand
slf4j (Simple Logging Facade for Java) represents a simple log facade provided for Java, and a more basic point is an interface. By importing the information in the program into the log system and recording it, decoupling between the program and the log system can be achieved.
The log facade interface itself usually does not have the actual log output capability. It still needs to call the specific log framework API at the bottom, that is, it actually needs to be used in combination with the specific log framework. Since there are many specific log frameworks and are mostly incompatible with each other, if the log facade interface is combined with any log framework, it may require a corresponding bridge, just as the combination between JDBC and various different databases requires a corresponding JDBC driver.
1. Log facade (Facade)
1.slf4j (recommended)
2.commons-logging
2. Slf4j connection specific logs
When connecting slf4j and other log frames, a桥接器is needed in the middle. Some logging frameworks themselves use the slf4j API, so they do not need to use bridges.
logback is an improved version of log4j, and the slf4j API is directly used internally, so there is no bridge. (logback and slf4j are said to be from the same author)
1.slf4j-api.jar >slf4j-log4j12.jar bridge >log4j.jar
2.slf4j-api.jar > logback-core.jar,logback-classic.jar
3.slf4j-api.jar >slf4j-simple.jar
4.slf4j-api.jar > log4j-slf4j-impl-2.8.2.jar > org.apache.logging.log4j:log4j-api:2.8.2, org.apache.logging.log4j:log4j-core:2.8.2, org.apache.logging.log4j:log4j-web:2.8.2 (recommended to use log4j2)
2. Use slf4j+log4j2
log4j2.xml
Put it in the resources directory
<?xml version="1.0" encoding="UTF-8"?><!-- The 6 priority levels are: OFF, FATAL, ERROR, WARN, INFO, DEBUG, TRACE, ALL. If the priority is set to WARN, then the four levels of logs, OFF, FATAL, ERROR, and WARN, can output normally to OFF. This means that logs of log4j2 are not recorded. --><!-- status: used to specify the printing log level of log4j itself. monitorInterval: specify the monitoring interval time for log4j automatically reconfigured --><configuration status="INFO" monitorInterval="30"> <!-- Set the properties yourself, and then access them through ${} --> <properties> <property name="LOG_HOME">${web:rootDir}/logs</property> </properties> <appenders> <!--Appender 1. Output to the Console console, specify the output format and filter level as INFO --> <Console name="Console" target="SYSTEM_OUT"> <!--ThresholdFilter Specifies the lowest level of output of log messages --> <ThresholdFilter level="ALL" onMatch="ACCEPT" onMismatch="DENY"/> <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/> </Console> <!--Appender 2. Output to the scroll saved file, The condition that triggers the save log file is that the log file is greater than 3KB, and only the latest 10 logs are saved--> <File name="allLog" fileName="${LOG_HOME}/all.log"> <ThresholdFilter level="ALL" onMatch="ACCEPT" onMismatch="DENY"/> <PatternLayout charset="UTF-8" pattern="%d{yyyyy.MM.dd 'at' HH:mm:ss z} %-5level %class{36} %L %M - %msg%xEx%n"/> </File> <!--Appender 3. Export to the scroll saved file, The condition that triggers the save log file is that the log file is greater than 3KB, and only the latest 10 logs are saved --> <RollingFile name="debugLog" fileName="${LOG_HOME}/debug.log" filePattern="${log.path}/debug-%i.log"> <ThresholdFilter level="DEBUG" onMatch="ACCEPT" onMismatch="DENY"/> <PatternLayout charset="UTF-8" pattern="[%-5level][%d{yyyy-MM-dd HH:mm:ss}][%F:%L] - %m%n"/> <SizeBasedTriggeringPolicy size="3KB"/> <!-- The parameter max in DefaultRolloverStrategy can limit the max archives after the size exceeds the size in SizeBasedTriggeringPolicy--> <DefaultRolloverStrategy max="10"/> </RollingFile> <!--Appender 4. Export to the scroll saved file, the condition that triggers the save log file is the first log event per minute. ERROR logs are generated by minutes --> <RollingFile name="errorLog" fileName="${LOG_HOME}/error.log" filePattern="${log.path}/error-%d{yyyy-MM-dd_HH-mm}.log"> <ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY"/> <PatternLayout charset="UTF-8" pattern="[%-5level][%d{yyyyy-MM-dd HH:mm:ss}][%C:%F:%L] - %m%n"/> <TimeBasedTriggeringPolicy/> </RollingFile> <RollingFile name="RollingFile" fileName="${LOG_HOME}/rar.log" filePattern="${LOG_HOME}/$${date:yyyy-MM}/${FILE_NAME}-%d{MM-dd-yyyy}-%i.log.gz"> <PatternLayout charset="UTF-8" pattern="%d{yyyy-MM-dd 'at' HH:mm:ss z} %-5level %class{36} %L %M - %msg%xEx%n"/> <!--The maximum log file value is compressed the next day --> <Policies> <TimeBasedTriggeringPolicy/> <SizeBasedTriggeringPolicy/> </Policies> </RollingFile> </appenders> <!--root default loading--> <loggers> <root level="info"> <appender-ref ref="Console"/> <!--<appender-ref ref="allLog"/>--> <!--<appender-ref ref="debugLog"/>--> <appender-ref ref="errorLog"/> <!--<appender-ref ref="RollingFile"/> </root> </loggers></configuration>LogTest.java
import org.slf4j.Logger;import org.slf4j.LoggerFactory;public class LogTest { public final Logger logger=LoggerFactory.getLogger(getClass()); public static void main(String[] args) { log.trace("trace"); log.debug("debug"); log.warn("warn"); log.info("info"); log.error("error"); }} Reference article: https://www.VeVB.COM/article/143486.htm
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.