Logback Basic Usage

Source: Internet
Author: User

Logger code

Use the unified interface provided by SLF4J-API (from Logback website http://logback.qos.ch/xref/chapters/introduction/HelloWorld1.html)

 Package chapters.introduction; Import Org.slf4j.Logger; Import org.slf4j.LoggerFactory;  Public class HelloWorld1 {  publicstaticvoid  main (string[] args) {    = Loggerfactory.getlogger ("Chapters.introduction.HelloWorld1");    Logger.debug ("Hello world.") );  }}

Logger configuration: Logback.xml
<?XML version= "1.0" encoding= "UTF-8"?><Configuration>    <!--defining the log Output format -    < Propertyname= "Log_pattern"value= "%d{yyyy-mm-dd HH:mm:ss. SSS} [%thread]%-5level%logger{36}[%line]-%msg%n " />    <!--output to a scrolling file -    <Appendername= "ACCESS"class= "Ch.qos.logback.core.rolling.RollingFileAppender">        <!--file name -        <File>/tmp/log/api/access.log</File>        <!--Configuring a time-based scrolling policy -        <Rollingpolicyclass= "Ch.qos.logback.core.rolling.TimeBasedRollingPolicy">            <!--The scrolling time period is inferred from Filenamepattern. -            <!--For example, format: {YYYY-MM-DD} means scrolling by day; {yyyy-mm-dd_hh} means scrolling by the hour -            <Filenamepattern>/tmp/log/api/access.%d{yyyy-mm-dd}.log</Filenamepattern>            <!--Keep history log number -            <maxhistory>7</maxhistory>        </Rollingpolicy>        <Encoder>            <pattern>${log_pattern}</pattern>        </Encoder>    </Appender>        <Appendername= "Debug_file"class= "Ch.qos.logback.core.rolling.RollingFileAppender">        <File>/tmp/log/api/debug.log</File>        <Rollingpolicyclass= "Ch.qos.logback.core.rolling.TimeBasedRollingPolicy">            <Filenamepattern>/tmp/log/api/debug.%d{yyyy-mm-dd}.log</Filenamepattern>            <maxhistory>7</maxhistory>        </Rollingpolicy>        <Encoder>            <pattern>${log_pattern}</pattern>        </Encoder>    </Appender>    <Appendername= "CONSOLE"class= "Ch.qos.logback.core.ConsoleAppender">        <Encoder>            <pattern>${log_pattern}</pattern>        </Encoder>    </Appender>    <Loggername= "Com.xqb.api.commons.GlobalExceptionHandler" Level= "DEBUG"/>    <!--specific logs using the specified Appender -    <Loggername= "Org.springframework.web.servlet.DispatcherServlet"additivity= "false" Level= "DEBUG">        <Appender-refref= "ACCESS"/>    </Logger>    <Loggername= "Org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor"additivity= "false" Level= "DEBUG">        <Appender-refref= "ACCESS"/>    </Logger>    <Loggername= "Org.springframework.jdbc.core" Level= "TRACE"/>    <Loggername= "Org.hibernate.type.descriptor.sql.BasicBinder" Level= "TRACE"/>    <Loggername= "Org.hibernate.SQL" Level= "DEBUG"/>    <Root Level= "INFO">        <!--appender List of enabled -        <!--The ACCESS Appender is not added because it was used when defining Dispatcherservlet logger -        <Appender-refref= "CONSOLE"/>        <Appender-refref= "Debug_file"/>    </Root></Configuration>

Dependency Packages

Slf4j-api.jar

Logback-core.jar

Logback-classic.jar

MAVEN configuration:
<Dependency>    <groupId>Org.slf4j</groupId>    <Artifactid>Slf4j-api</Artifactid>    <version>${slf4j-version}</version></Dependency><Dependency>    <groupId>Ch.qos.logback</groupId>    <Artifactid>Logback-classic</Artifactid>    <version>1.1.2</version></Dependency>

Logback Basic Usage

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.