"Java-Log Components" Slf4j+logback configuration and explanation

Source: Internet
Author: User

Text mainly introduce slf4j+logback in Java project configuration, facing the reader is already have a certain understanding of slf4j+logback classmates, do not know the classmate, please see the article at the end of the relevant links. I will write an article about SLF4J framework, which will explain the relationship between Logback. Let's get down to the chase.

One, maven-based Slf4j+logback pom.xml configuration

<dependency>    <groupId>Org.slf4j</groupId>    <artifactid>Slf4j-api</artifactid>    <version>1.7.10</version></Dependency><dependency>    <groupId>Ch.qos.logback</groupId>    <artifactid>Logback-classic</artifactid>    <version>1.1.2</version></Dependency><dependency>    <groupId>Ch.qos.logback</groupId>    <artifactid>Logback-core</artifactid>    <version>1.1.2</version></Dependency>

II. Create a new logback.xml configuration file in the Classpath directory

<?xml version= "1.0" encoding= "UTF-8"?><!--Scan: When this property is set to True, the configuration file will be reloaded if it changes, and the default value is true. Scanperiod: Sets whether the monitoring profile has a modified interval, if no time unit is given, the default unit is milliseconds when scan is true, this property takes effect. The default time interval is 1 minutes. Debug: When this property is set to True, the Logback internal log information is printed and the Logback run status is viewed in real time. The default value is False. -<configuration Scan="false" scanperiod="Seconds" debug ="false">    <!--define the root directory of the log--    < property name="Log_home" value="/app/log" />     <!--define the log file name --    < property name="AppName" value="Netty"></Property >    <!--ch.qos.logback.core.ConsoleAppender means console output --    <appender name= "stdout" class=" Ch.qos.logback.core.ConsoleAppender ">        <Encoding>UTF-8</Encoding>        <!--log Output format:%d represents a DateTime,%thread represents a thread name,%-5level: the level displays 5 character widths from the left%logger{50} represents the logger name with a maximum of 50 characters, otherwise it is split by a period. %MSG: Log messages,%n are newline characters--        <layout class="Ch.qos.logback.classic.PatternLayout">            <pattern>%d{yyyy-mm-dd HH:mm:ss. SSS} [%thread]%-5level%logger{50}-%msg%n</pattern>        </layout>    </appender>    <!--Scroll through the log files to the specified file, logging to other files when a certain condition is met --      <appender name= "applogappender" class=" Ch.qos.logback.core.rolling.RollingFileAppender ">        <Encoding>UTF-8</Encoding>        <!--Specify the name of the log file--          <file>${log_home}/${appname}.log</file>        <!--When scrolling occurs, determines the behavior of rollingfileappender, involving file movement and renaming Timebasedrollingpolicy: The most commonly used scrolling strategy, which is based on time to develop a rolling strategy that is both responsible for rolling        Also responsible for starting rolling. -        <rollingpolicy class="Ch.qos.logback.core.rolling.TimeBasedRollingPolicy">             <!--the location and file name of the file generated when scrolling%d{yyyy-mm-dd}: Log scrolling by day%i: File scrolling by I when file size exceeds MaxFileSize -            <filenamepattern>${log_home}/${appname}-%d{yyyy-mm-dd}-%i.log</filenamepattern>            <!--An optional node that controls the maximum number of archived files that are retained, and deletes old files if they exceed the number. Assuming that the settings are scrolled every day, and Maxhistory is 365, only the last 365 days of files are saved, and the old files are deleted.            Note that deleting old files is the ones that are created to be archived and also deleted. -            <maxhistory>365</maxhistory>            <!--The log file exceeds maxfilesize specified size is, according to the%i mentioned above for logging file scrolling Note This configuration Sizebasedtriggeringpolicy is not possible to scroll by file size, you must configure time Basedfilenamingandtriggeringpolicy --            <timebasedfilenamingandtriggeringpolicy class=" Ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP ">                <maxfilesize>100MB</maxfilesize>            </timebasedfilenamingandtriggeringpolicy>        </rollingpolicy>        <!--log Output format:%d represents a DateTime,%thread represents a thread name,%-5level: the level displays 5 character widths from the left%logger{50} represents the logger name with a maximum of 50 characters, otherwise it is split by a period. %MSG: Log messages,%n are newline characters--             <layout class="Ch.qos.logback.classic.PatternLayout">            <pattern>%d{yyyy-mm-dd HH:mm:ss. SSS} [%thread]-[%-5level] [%logger{50}:%line]-%msg%n</pattern>        </layout>    </appender>    <!--logger is primarily used to store log objects, you can also define the log type, level name: Represents the matching logger type prefix, which is the first half of the package level: Log levels to log, including TRACE < DEBUG < INFO < WARN < ERROR additivity: The effect is children-logger whether to use Appender for Rootlogger configuration to output, False: Indicates that only the current logger appender-ref,true is used: Rootlogger that represents the current logger appender-ref and Appender-ref are valid -    <!--Hibernate logger --    <logger name="Org.hibernate" level ="error" />    <!--Spring Framework Logger --    <logger name="Org.springframework" level ="Error"  additivity="false"></Logger>    <logger name="Com.creditease" level ="Info"  additivity="true">        <appender-ref ref="Applogappender" />    </Logger>    <!--root and logger is a parent-child relationship, no special definition is the default root, any class will only be corresponding to a logger, either defined logger, or root, the key to judge is to find this logger,     Then judge the Appender and level of the logger. -    <root level ="Info">        <appender-ref ref="stdout" />        <appender-ref ref="Applogappender" />    </root></configuration> 

Resources
The relationship and debugging of log4j+logback+slf4j+commons-logging
Introduction to log Component Logback and how to configure it
Using Logback
Logback Sizebasedtriggeringpolicy and Timebasedrollingpolicy joint use issues

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Java-Log Components" Slf4j+logback configuration and explanation

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.