[Java-log component] slf4j + logback configuration and details, java-slf4j

Source: Internet
Author: User

[Java-log component] slf4j + logback configuration and details, java-slf4j

This article mainly introduces the configuration of slf4j + logback in the java project. It is intended for readers who have some knowledge about slf4j + logback, see the link at the end of the article. In the future, I will write an article about the slf4j framework, which will describe the relationship between logback and it. Next let's go to the topic.

1. Configure slf4j + logback pom. xml based on maven

<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>

2. Create a logback. xml configuration file under the classpath directory

<? Xml version = "1.0" encoding = "UTF-8"?> <! -- Scan: If this attribute is set to true, the configuration file will be reloaded if it is changed. The default value is true. ScanPeriod: sets the interval for monitoring whether the configuration file has been modified. If no time unit is provided, the default unit is millisecond. When scan is set to true, this attribute takes effect. The default interval is 1 minute. Debug: When this attribute is set to true, the logback internal log information is printed to view the running status of logback in real time. The default value is false. --> <Configuration scan = "false" scanPeriod = "60 seconds" debug = "false"> <! -- Define the log root directory --> <property name = "LOG_HOME" value = "/app/log"/> <! -- Define the log file name --> <property name = "appName" value = "netty"> </property> <! -- Ch. qos. logback. core. consoleAppender indicates console output --> <appender name = "stdout" class = "ch. qos. logback. core. consoleAppender "> <Encoding> UTF-8 </Encoding> <! -- Log output format: % d indicates the date and time, % thread indicates the thread name, %-5 level: the level is 5 Characters in width % logger {50} from the left, indicating that the logger name can contain a maximum of 50 characters. Otherwise, logger names are separated by periods. % Msg: Log message. % n is a line break --> <layout class = "ch. qos. logback. classic. patternLayout "> <pattern> % d {yyyy-MM-dd HH: mm: ss. SSS} [% thread] %-5 level % logger {50}-% msg % n </pattern> </layout> </appender> <! -- Scroll the record file, first record the log to the specified file, when a condition is met, record the log to another file --> <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 rolling occurs, it determines the RollingFileAppender action, involving file movement and renaming TimeBasedRollingPolicy: The most common rolling policy. It sets a rolling policy based on time, which is responsible for both rolling and rolling. --> <RollingPolicy class = "ch. qos. logback. core. rolling. TimeBasedRollingPolicy"> <! -- Storage location and name of the file generated during scrolling % d {yyyy-MM-dd}: log scrolling by day % I: when the file size exceeds maxFileSize, scroll by I --> <fileNamePattern >$ {LOG_HOME}/$ {appName}-% d {yyyy-MM-dd}-% I. log </fileNamePattern> <! -- An optional node that controls the maximum number of archive files that are retained. If the maximum number is exceeded, the old files are deleted. For example, if you set daily scrolling and maxHistory to 365, only the files of the last 365 days are saved and the old files are deleted. Note that deleting old files is the same as deleting directories created for archiving. -- & Gt; <MaxHistory> 365 </MaxHistory> <! -- If the size of the log file exceeds the size specified by maxFileSize, scroll the log file according to % I mentioned above. Note that SizeBasedTriggeringPolicy cannot be configured here to scroll by file size, you must configure timeBasedFileNamingAndTriggeringPolicy --> <timeBasedFileNamingAndTriggeringPolicy class = "ch. qos. logback. core. rolling. sizeAndTimeBasedFNATP "> <maxFileSize> 100 MB </maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> <! -- Log output format: % d indicates the date and time, % thread indicates the thread name, %-5 level: the level is 5 Characters in width % logger {50} from the left, indicating that the logger name can contain a maximum of 50 characters. Otherwise, logger names are separated by periods. % Msg: Log message. % n is a line break --> <layout class = "ch. qos. logback. classic. patternLayout "> <pattern> % d {yyyy-MM-dd HH: mm: ss. SSS} [% thread]-[%-5 level] [% logger {50 }: % line]-% msg % n </pattern> </layout> </appender> <! -- Logger is mainly used to store log objects. You can also define the log type and level name: indicates the matched logger type prefix, that is, the level of the first half of the package: the log level to be recorded, including TRACE <DEBUG <INFO <WARN <ERROR additider: determines whether children-logger uses the appender configured by rootLogger for output. false: indicates that only the appender-ref of the current logger is used. true: indicates that the appender-ref of the current logger and the appender-ref of the rootLogger are both 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 "additider =" true "> <appender-ref =" appLogAppender "/> </logger> <! -- The root and logger are parent-child relationships. The default value is root if there is no special definition. Any class will only correspond to a logger, either the defined logger or the root, the key to judgment is to find the logger and then determine the appender and level of the logger. --> <Root level = "info"> <appender-ref = "stdout"/> <appender-ref = "appLogAppender"/> </root> </configuration>

References
Relationship and debugging of log4j + logback + slf4j + commons-logging
Logback
Use logback
Joint Use of SizeBasedTriggeringPolicy and TimeBasedRollingPolicy of logback

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.