Use Logback log components

Logback is another open source log4j log component by the founder of the design. 

A logback introduction 
logback currently divided into three modules: logback -core, logback- Classic and logback-access. logback-core is the basis for the other two modules of the module. logback-classic is a modified version of log4j. Further logback-classic complete implementation SLF4J API so you can easily be changed into other systems, such as logs or log4j JDK14 Logging. logback- Access integrated access module and Servlet containers supplied by Http access log function. Logback is to be combined with SLF4J up as follows with the official website of two components: 

logback's official website: HTTP: // logback.qos.ch 

SLF4J's official website: HTTP: // www.slf4j.org 

this article with to the following components: Please to the official website itself! 

the logback -Access- 1.0 . 0 .jar 

the logback -classic- 1.0 . 0 .jar 

the logback -core- 1.0. 0 .jar 

SLF4J -api- 1.6 . 0 .jar 

two grounds of logback replace log4j:. 
Logback and log4j are very similar, if you are familiar with log4j, it will soon be handy for logback. Logback listed below with respect to some of the advantages of the log4j: 
1 . Logback achieve faster core rewritten, in some key performance execution path 10 or more times. And logback not only improves performance, but also initialize memory to load even smaller. 
2 . Logback very adequately tested after a few years, countless hours of testing. Logback test a completely different level. In the author's opinion, this is a simple choice logback important reason rather than log4j. 
3 . Logback achieve a very natural-Classic SLF4j Logback-classic realized SLF4j. SLF4j in use, you will not feel logback-classic. And because logback- Classic the SLF4J achieved very natural, so log4j or switch to another very easily, need only provide the packet to another jar on OK, the code that do not need to move through SLF4JAPI achieved. 
4Very fully document the official website has 200 pages of documents. 
5 . Automatic reload the configuration file when the configuration file is modified, Logback- Classic automatically reload the configuration file. Fast and secure scanning process, it does not need to create a scan thread. The technology to ensure the application can run in JEE environment very happy inside. 
6 . Lilith Lilith is an observer log events, and chainsaw log4j similar. The lilith can handle a large amount of log data. 
7 . Prudent and very friendly mode of recovery in the cautious mode, multiple instances FileAppender run under multiple JVM, you can write the same log file safely. RollingFileAppender be some restrictions. The FileAppender Logback and its subclasses can be very friendly RollingFileAppender comprises from I / recovery O exception. 
8 . Profile can handle different situations developers often need to determine different Logback profiles in different environments (development, test, production). These profiles with only few minor differences, you can, and to achieve such a profile can adapt to multiple environments. 
9. Filters (filter) There are times when you need to diagnose a problem and needs to play log. In log4j, logging only the lower level, but it will play a large number of logs, it will affect application performance. In Logback, you can continue to maintain the log level and get rid of some special circumstances, such as alice the user logs in, she will hit the DEBUG log level and other users can continue to play at the WARN level. To achieve this just add 4 lines of XML configuration. You can refer MDCFIlter. 

SiftingAppender (a very versatile the Appender) which can be used to split a log file of any given operating parameters. Such as, SiftingAppender able to distinguish log events follow a user's Session, then each user will have a log file. 

Automatic compression has been playing out of log RollingFileAppender when a new file is automatically compressed been playing out of log files. Compression is an asynchronous process, so even for large log files during compression applications will not be affected. 

Stack Package Version Tree data with play when the stack Logback tree logs, will bring the package. 

Automatically remove old log files by setting TimeBasedRollingPolicy or SizeAndTimeBasedFNATP of maxHistory property, you can control the maximum number of log files has been generated. If maxHistory 12 , that those log files exceeds 12 months will be automatically removed. 

In short, logback log4j too better than, let us all build applications on logback it! 

Three. Logback configuration Introduction. 1 . Logger. Layout and the appender 

after log recording Logger, and to associate it with the corresponding context applications, mainly for storage of log objects can also be defined log type. Level. 

Appender mainly used to specify the destination of log output destination can be a console file. Remote socket server. MySQL. PostreSQL. Oracle and other databases. JMS and remote UNIX Syslog daemons. 

Layout is responsible for converting the event into a string, formatted output log information. 
2 . Logger context 

each are associated with a logger LoggerContext, LoggerContext responsible for manufacturing logger, a tree structure is also responsible for the arrangement of each logger. All other logger also getLogger achieved by static method org.slf4j.LoggerFactory class. getLogger method name as a parameter to the logger. The same method name to call LoggerFactory.getLogger obtained always refer to the same logger object. 

Effective level and the level of inheritance 
Logger can be assigned level. Levels include:... TRACE DEBUG INFO WARN and ERROR, ch.qos.logback.classic.Level defined in the class. If the logger is not assigned level, then it inherits from level has been assigned level of recent ancestors. The default is the root logger level DEBUG.

Printing method and basic selection rules
Print method determines the level recorded request. For example, if L is a logger instance, then the statement L.info ( "..") is a statement record level INFO. Request to record levels in higher than or equal to the effective level of its logger are called to be enabled, otherwise known as disabled. Recording request level p, that the effective level logger is q, then only if P > = time q, the request will be executed. 

The rule is at the heart of logback. Level order of: the TRACE <DEBUG <INFO <WARN < ERROR. 

IV. The default configuration of Logback 
If the configuration file logback -test.xml and logback.xml do not exist, then the default logback calls BasicConfigurator, create a minimal configuration. ConsoleAppender a minimal configuration by the associated root logger composition. The output mode is HH {D%: mm: ss.SSS} [Thread%]%% -5level Logger { 36 } -% MSG% n-PatternLayoutEncoder of format. The default is the root logger level DEBUG. 
1 . Logback configuration file 

syntax Logback configuration file is very flexible. Because of the flexibility, it can not be defined with a DTD or XML schema. Nevertheless, the basic structure can be described in the configuration file: In<configuration> at the beginning, followed by zero or more <appender> element, zero or more <logger> element, up to a <root> element. 
2 . Logback default configuration steps 

( 1 ) to try to find the file in logback- CLASSPATH. The test.xml; 

( 2 .) If the file does not exist, then the lookup file logback.xml; 

( . 3 ) If the two. file does not exist, logback with Bas icConfigurator automatically configure itself, which causes the log output to the console. 
. 3 . Logback.xml file

 <? XML Version = " 1.0 " encoding = " UTF-. 8 " ?> 
<Configuration> 
    <-! Storage address defined in LogBack log files do not use a relative path configuration ->   
    < name = Property " LOG_HOME "C: / log " />   
    ! <- console output ->    
    <the appender name = " STDOUT "  class = " ch.qos.logback.core.ConsoleAppender " > 
       <- log output code ->!   
       <Encoding > UTF . 8 </ Encoding>    
        <layout class = " ch.qos.logback.classic.PatternLayout " >    
             <- output format:!% d represents the date,% thread indicates that the thread name,% - 5level: left level display character width 5% msg: log messages,% n newline -> 
            <pattern>% {D the mM-dd-YYYY HH: mm: SS.SSS} [%thread] %-5level %logger{50} - %msg%n   
            </pattern>   
        </ layout>    
    </ the appender>    
    <-! log files are generated per day according to the ->    
    <the appender name = " the FILE "   class = " ch.qos.logback.core.rolling.RollingFileAppender " >    
        <Encoding> UTF . 8 < / Encoding>    
        <rollingPolicy class = " ch.qos.logback.core.rolling.TimeBasedRollingPolicy " > 
            <-! file name of the log file output -> 
            <FileNamePattern> LOG_HOME $ {} {YYYY /myApp.log.%d } .log-dd -MM </ FileNamePattern>    
            <MaxHistory> 30 </ MaxHistory>
        </rollingPolicy>    
        <layout class="ch.qos.logback.classic.PatternLayout " >   
            <- Output Format:!% d represents the date,% thread indicates that the thread name,% - 5level: Level 5 show left character width% msg: log messages,% n newline -> 
            <pattern>% {D the mM-dd-YYYY HH: mm: ss.SSS} [Thread%]%% -5level Logger { 50 } -% MSG% n-   
             </ pattern>    
       </ layout> 
        <! - the maximum log file size -> 
       <triggeringPolicy class = " ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy " > 
         <the MaxFileSize> 10MB </ the MaxFileSize> 
       </ triggeringPolicy> 
    </ appender> 
   <!- Show the Parameters for Hibernate Hibernate customized specifically for SQL ->  
    <logger name="org.hibernate.type.descriptor.sql.BasicBinder"  level="TRACE" />  
    <logger name="org.hibernate.type.descriptor.sql.BasicExtractor"  level="DEBUG" />  
    <logger name="org.hibernate.SQL" level="DEBUG" />  
    <logger name="org.hibernate.engine.QueryParameters" level="DEBUG" />  
    <logger name="org.hibernate.engine.query.HQLQueryPlan " Level = " the DEBUG " />   
    
    <-! log output level -> 
    <= the root Level " the INFO " >    
        <appender- REF  REF = " STDOUT " />    
        <appender- REF  REF = " the FILE " />    
    </ the root> 
     
     <-! asynchronous log to the database ->   
    <the appender name = " DB "  class = " ch.qos.logback.classic.db.DBAppender"> 
        <-! Asynchronous log into the database ->
        <connectionSource class="ch.qos.logback.core.db.DriverManagerConnectionSource">
           <!--连接池 --> 
           <dataSource class="com.mchange.v2.c3p0.ComboPooledDataSource">
              <driverClass>com.mysql.jdbc.Driver</driverClass>
              <url>jdbc:mysql://127.0.0.1:3306/databaseName</url>
              <user>root</user>
              <password>root</password>
            </dataSource>
        </connectionSource>
  </appender> -->
</ configuration> 
V. in the program with reference Logback
com.stu.system.action Package; 

Import org.slf4j.Logger; 
Import org.slf4j.LoggerFactory; 

public  class BlogAction {
      // define a global recorder, obtained by of LoggerFactory 
     Private Final static Logger Logger = LoggerFactory.getLogger (BlogAction . class ); 
      / 
     * @param args
      * /
     public  static  void main (String [] args) { 
        logger.info ( " the logback successfully " ); 
        logger.error ( " the logback successfully " ); 
    }  
}
. Logback manual pdf ( 3.6MB) 
Donate the Comment here Wallpaper 
a reward 
author: IIsKei 
This link: HTTP: // www.iskei.cn/posts/17154.html 
Disclaimer: All articles in this blog unless otherwise specified, are using the NC-BY-SA CC 4.0 agreement. Please indicate the source!

 

Guess you like

Origin www.cnblogs.com/Jeely/p/11224205.html