Example of the Java log System "Logback" 2

Source: Internet
Author: User
Tags log4j

<?xml version= "1.0"  encoding= "UTF-8"?><configuration>    <!--   Console output  -->    <appender name= "stdout"  class= " Ch.qos.logback.core.ConsoleAppender ">        <encoder>             <pattern>%date [%thread] %- 5level %logger{80} - %msg%n</pattern>        < /encoder>    </appender>    <!--  time rolling output  level  DEBUG  log  -->    <appender name= "File-debug"          class= "Ch.qos.logback.core.rolling.RollingFileAppender" >                    There is a filtering behavior here, Once you need to use the Evaluatorfilter filter to filter special characters in the MSG log you need to import           its dependency package janino-2.3.2.jar                 <filter class= " Ch.qos.logback.classic.filter.LevelFilter ">             <level>DEBUG</level>             <onMatch>ACCEPT</onMatch>             <onmismatch>deny </onmismatch>        </ Filter>        <rollingpolicy class= " Ch.qos.logback.core.rolling.TimeBasedRollingPolicy ">             <FileNamePattern>D:/logs/debug.%d{yyyy-MM-dd}.log</FileNamePattern>             <maxhistory>30</maxhistory>        </rollingpolicy>         <encoder>             <pattern>%date [%thread] %-5level %logger{80} - %msg%n</ pattern>        </encoder>    </ appender>    <!--  Time scrolling output  level to  ERROR  log  -->     <appender name= "File-error"         class= " Ch.qos.logback.core.rolling.RollingFileAppender ">        <filter  class= "Ch.qos.logback.classic.filter.LevelFilter" >             <level>ERROR</level>             <onmatch>accepT</onmatch>            <onmismatch> deny </onmismatch>        </filter>         <rollingpolicy class= "Ch.qos.logback.core.rolling.TimeBasedRollingPolicy ">            <filenamepattern>d:/logs/ error.%d{yyyy-mm-dd}.log</filenamepattern>             <maxhistory>30</maxhistory>        </ rollingpolicy>        <encoder>      &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&LT;PATTERN&GT;%DATE&NBSP;[%THREAD]&NBSP;%-5LEVEL&NBSP;%LOGGER{80}  - %msg%n</pattern>        </encoder>     </appender>    <!--  Specific filtering of logs containing a string  -->    <appender  Name= "File-str"         class= " Ch.qos.logback.core.rolling.RollingFileAppender ">        <filter  class= "Ch.qos.logback.core.filter.EvaluatorFilter" >             <evaluator>                 <expression>message.contains ("str") </expression>             </evaluator>             <onMatch>ACCEPT</onMatch>             <onMismatch>DENY</onMismatch>         </filter>        <rollingpolicy class= "Ch.qos.logback.core.rolling.TimeBasedRollingPolicy" >             <filenamepattern>d:/logs/contains.%d{ Yyyy-mm-dd}.log            </filenamepattern >            <maxhistory>30</ maxhistory>        </rollingpolicy>         <encoder>             <pattern>%date [%thread] %-5level %logger{80} - %msg%n</pattern>         </encoder>    </appender>     <!--  Database Output  -->    <appender name= "DB"  class= "Ch.qos.logback.classic.db.DBAppeNDEr ">        <connectionSource             class= "Ch.qos.logback.core.db.DriverManagerConnectionSource" >             <driverClass> com.mysql.jdbc.driver</driverclass>             <url>jdbc:mysql://host_name:3306/datebase_name</url>             <user>username</user>             <password>password</password>         </connectionSource>    </appender>     <logger name= "Java.sql.Connection" >        <level  Value= "DEBUG"  />    </lOgger>    <logger name= "Java.sql.Statement" >         <level value= "DEBUG"  />    </logger>     <logger name= "Com.ibatis" >        <level  value= "DEBUG"  />    </logger>    <logger  Name= "Com.ibatis.common.jdbc.SimpleDataSource" >        <level  value= "DEBUG"  />    </logger>    <logger  Name= "Com.ibatis.common.jdbc.ScriptRunner" >        <level  Value= "DEBUG"  />    </logger>    <logger name= " Com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate ">        < Level value= "DEBUG" &nbsP;/>    </logger>    <logger name= " Com.danga.MemCached ">        <level value=" INFO " />     </logger>    <logger name= "Org.springframework.test" >        <level value= "DEBUG"  />     </logger>    <logger name= "Org.apache.struts2" >         <level value= "DEBUG"  />    </logger>     <root level= "DEBUG" >        < appender-ref ref= "stdout"  />        <appender-ref  ref= "File-debug"  />        <appender-ref ref= " File-error " />        <appender-ref ref= "File-str"  />         <appender-ref ref= "DB"  />    </root></configuration >



A simple explanation for Logback:

1, Logback to replace log4j and born

Logback is another open source log component designed by LOG4J founder Ceki Gülcü. The Logback is currently divided into three modules: Logback-core,logback-classic and logback-access.


2, the core object of Logback: Logger, Appender, Layout

Logback is mainly based on the three classes of logger, Appender and Layout.

1:logger: Log logger, which is associated with the corresponding context of the application, is primarily used for storing log objects, and can also define log types and levels. Logger objects are generally more defined as static constants, such as:

2:appender: Used to specify the destination of the log output, the destination can be console, file, remote socket, Mysql,postresql,oracle, and other databases

3:layout: The output of the log information that is responsible for converting the time into strings, format words.


3 levels of the young level, logger can assign levels, levels include: Trace,debug,info,warn, and error, defined in

Ch.qos.logback.classic.Level class, the program prints a Izhi that is above or equal to the set level, the higher the log level of the set level, the printed day

If the log information of the set level info can be output, less than that level, such as debug will not be output, to ensure that all logger can be

Eventually inherits a level, so the root logger always has a level, in general, this level is the debug


4: three-valued logic

The Logback filters are based on the three-valued logic, allowing them to be assembled into chains, thus assembling them into any filtering strategy, and the filter is largely inspired by the iptables, where the so-called three-valued logic is said:

If Deny is returned, the recording time is discarded immediately, not through the remaining filters

If neutral is returned, the next filter in the sequence table will then process the logged event

If you return to accept, the recording time is processed immediately and no longer passes through the remaining filters


5 Filter Filters

Logback-classic offers two types of filtration:

5.1: General Filter

5.2:turobofilter Filter


The overall log processing process is as follows:

Example of the Java log System "Logback" 2

Related Article

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.