Use the Logback output log to customize the output format __logback

Source: Internet
Author: User
Tags log4j

In the development of more common or log4j, basically every developer knows, but the use of Logback output log, the estimated use of not many people, in fact, these two are SLF4J implementation, but also the same author.

Log4j is an open source project in Apache, through the use of log4j, we can control the log information delivery destination is the console, file, GUI components, or even a set of interface server, NT event memory

Recorder, UNIX syslog daemon, etc. we can also control the output format of each log, and we can control the log more carefully by defining the level of each log information

The build process. Most interesting of all, these can be flexibly configured with a single configuration file without the need to modify the applied code. Logback is another open source log component designed by the founder of Log4j. Logback is currently divided into three modules: Logback-core,logback-classic and logback-access. Logback-core is the base module for the other two modules. Logback-classic is an improved version of log4j. The Logback-classic full implementation SLF4J API allows you to easily replace it with other log systems such as log4j or JDK14 Logging. The Logback-access access module and the servlet container integration provide the ability to access logs through HTTP. Logback is to be combined with SLF4J with two components of the official website as follows:

Logback's official website: http://logback.qos.ch

SLF4J's official website: http://www.slf4j.org uses Logback's benefits: It works better than log4j and is more efficient.

First, the following dependency jars are introduced into the Pom.xml:

<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic< /artifactid>
    <version>1.1.8</version>
</dependency>
Configure the Logback.xml file with the following configuration information:

<?xml version= "1.0" encoding= "UTF-8"?> <configuration scan= "true" scanperiod= "minutes" > <property NA        
  Me= "Log_home" value= "F:\\logs"/> <appender name= "Stdot" class= "Ch.qos.logback.core.ConsoleAppender" > <layout class= "Ch.qos.logback.classic.PatternLayout" > <pattern>%d{yyyy-mm-dd HH:mm:ss} [%p][%c][ %m][%l]->%m%n</pattern> </layout> </appender> <appender name= "file" class= "Ch.qos.log Back.core.rolling.RollingFileAppender "> <rollingpolicy class=" ch.qos.logback.core.rolling.TimeBasedRolling        
   Policy "> <filenamepattern>${log_home}/log.%d{yyyy-mm-dd} (%i) .log</filenamepattern>, <cleanHistoryOnStart>true</cleanHistoryOnStart> <timebasedfilenamingandtriggeringpo Licy class= "Ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP" > <maxfilesize>10mb</maxfilesi Ze> </timebasedfilenamingandtriggeringpolicy> </rollingPolicy> <encoder> &LT;CHARSET&G  
  T;utf-8</charset> <pattern>%d{yyyy-mm-dd HH:mm:ss} [%p][%c][%m][%l]->%m%n</pattern> </encoder> <append>false</append> <prudent>false</prudent> </append 
 er> <logger name= "Org.mortbay.log" additivity= "false" level= "ERROR" > <appender-ref ref= "Stdot"/> </logger> <logger name= "org.mybatis.spring" additivity= "false" level= "ERROR" > <appender-ref re f= "Stdot"/> </logger> <root level= "Debug" > <appender-ref ref= "Stdot"/> <appende R-ref ref= "file"/> </root> </configuration>


Where level is the priority of logging, divided into off, FATAL, ERROR, WARN, INFO, DEBUG, all, or custom levels. LOG4J recommends using only four levels, from high to low, respectively, for error, WARN, INFO, and DEBUG. By the level defined here, you can control the switch to log information at the appropriate level in your application. For example, the info level is defined here, and only those that are equal to and above this level are processed, and all debug-level log information in the application is not printed. All: Print all logs, off: Close all log outputs. Appendername is where you specify the output of the log information. Multiple output destinations can be specified at the same time
After the configuration is complete, place the Logback.xml file in the resource file directory and start the project. Logback will match the load according to the Logback name.

<pattern>%d{yyyy-mm-dd HH:mm:ss} [%p][%c][%m][%l]->%m%n</pattern>

The format described above is as follows:

%m

The message specified in the output code

%p

Output priority, i.e. Debug,info,warn,error,fatal

%r

The number of milliseconds to output from the application boot to the output of this log information

%c

The class to which the output belongs, usually the full name of the class in which it is located

%t

Output the name of the thread that generated the log event

%n

Output a carriage return line break, Windows platform is "\ r \ n", Unix platform is "\ n"

%d

The date or time of the output log point of time, the default format is ISO8601, or the format can be specified thereafter, for example:%d{yyy MMM dd Hh:mm:ss,sss},

Output similar to: October 18, 2002 22:10:28,921

%l

The location where the output log event occurs, including the class name, the thread that occurred, and the number of lines in the code. Example: Testlog4.main (testlog4.java:10)


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.