Java log slf4j and logback simple use

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.