Recently encountered in the development of logs is the use of slf4j and logback.xml configuration, so it is recorded.
1. Import these jar packages:
Logback is divided into three modules: logback-core,logback-classic,logback-access
Logback-core is the core;
Logback-classic improves the log4j and implements the SLF4J API itself, so using logback you can still use other log implementations, such as the original log4j,java.util.logging;
Logback-access makes it easy for you to access log information, such as through HTTP.
Slf4j-api.jar
Build Logback.xml under the Engineering src directory (i.e., classpath path)
1.logback try to find the Logback.groovy file first;
2. When not found, continue to try to find the Logback-test.xml file;
3. When not found, continue to try to find the Logback.xml file;
4. If it is still not found, use the default configuration (print to console).
2.logback.xml configuration file
<?xml version= "1.0" encoding= "UTF-8"?>
<!--for assistance related to Logback-translator or configuration --
<!--files in general, please contact the logback user mailing list--
<!--at Http://www.qos.ch/mailman/listinfo/logback-user -
<!---->
<!--for professional support
<!--http://www.qos.ch/shop/products/professionalSupport --
<!---->
<configuration scan= "true" scanperiod= "seconds" >
<appender name= "stdout" class= "Ch.qos.logback.core.ConsoleAppender" >
<encoder>
<pattern>[%p] [%d] [%t] [%c{1}:%l]-[%m]%n</pattern>
</encoder>
<filter class= "Ch.qos.logback.classic.filter.ThresholdFilter" >
<level>ERROR</level>
</filter>
</appender>
<appender name= "ERROR" class= "Ch.qos.logback.core.rolling.RollingFileAppender" >
<rollingpolicy class= "Ch.qos.logback.core.rolling.TimeBasedRollingPolicy" >
<filenamepattern>, .... /log/%d{yyyy-mm-dd/hh}/error.log.%i</filenamepattern>
<timebasedfilenamingandtriggeringpolicy class= "Ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP" >
<maxFileSize>20MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
<pattern>[%-5level] [%d] [%thread] [%logger{0}:%l]-[%m]%n</pattern>
</encoder>
<filter class= "Ch.qos.logback.classic.filter.ThresholdFilter" >
<level>ERROR</level>
</filter>
</appender>
<root level= "INFO" >
<appender-ref ref= "stdout"/>
<appender-ref ref= "MAIN"/>
<appender-ref ref= "ERROR"/>
</root>
<appender name= "Collectordemoapp" class= "Ch.qos.logback.core.rolling.RollingFileAppender" >
<rollingpolicy class= "Ch.qos.logback.core.rolling.TimeBasedRollingPolicy" >
<filenamepattern>, .... /log/%d{yyyy-mm-dd/hh}/ Collectordemoapp.log.%i</filenamepattern>
<timebasedfilenamingandtriggeringpolicy class= "Ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP" >
<maxFileSize>20MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
<pattern>[%-5level] [%d] [%thread] [%logger{0}:%l]-[%m]%n</pattern>
</encoder>
</appender>
<logger name= "cn.com. collectordemoapp"level=" info ">
<appender-ref ref= " collectordemoapp"/>
</logger>
</configuration>
3. Use in Java classes
private static final Logger Logger = Loggerfactory.getlogger (Collectordemoapp.class);
Java log slf4j and logback simple use