Logback configuration and use

Source: Internet
Author: User

Logback configuration and use

1. logback Introduction

Logback is another open-source log component designed by the founder of log4j. Logback is divided into three modules: logback-core, logback-classic, and logback-access. Logback-core is the basic module of the other two modules. Logback-classic is an improved version of log4j. In addition, logback-classic's complete implementation of SLF4J API allows you to easily replace it with other log systems such as log4j or JDK14 Logging. The logback-access module integrates with the Servlet container to provide the Log access function through Http.

 

2. maven dependency

<!-- logback+slf4j -->  <dependency>      <groupId>org.slf4j</groupId>      <artifactId>slf4j-api</artifactId>      <version>1.7.21</version>      <type>jar</type>      <scope>compile</scope>  </dependency>  <dependency>      <groupId>ch.qos.logback</groupId>      <artifactId>logback-core</artifactId>      <version>1.1.7</version>      <type>jar</type>  </dependency>  <dependency>      <groupId>ch.qos.logback</groupId>      <artifactId>logback-classic</artifactId>      <version>1.1.7</version>      <type>jar</type>  </dependency> 

3. Configure and use

Logback configuration files are stored in the logback. xml file in the/src/main/resource/folder. The logback. xml file is the configuration file of logback. After the file is placed, the system will automatically find the configuration file.

3.1 Configuration

<? 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 (this path generates the relative path of the eclipse root directory. We recommend that you use the absolute path with environment variables in the production environment) --> <property name = "LOG_HOME" value = "E: \ Tomcat \ logback"/> <! -- Define the log file name --> <property name = "appName" value = "ideal_system"> </property> <! -- 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 (according to the log file generated every day) --> <appender name = "FILE" class = "ch. qos. logback. core. rolling. rollingFileAppender "> <Encoding> UTF-8 </Encoding> <! -- Specify the name of the log file --> <file >$ {LOG_HOME}/$ {appName}/sys. log </file> <! -- Reject ERROR logs --> <filter class = "ch. qos. logback. classic. filter. levelFilter "> <level> ERROR </level> <onMatch> DENY </onMatch> <onMisMatch> NEUTRAL </onMisMatch> </filter> <filter class =" ch. qos. logback. classic. filter. thresholdFilter "> <level> DEBUG </level> <onMatch> ACCEPT </onMatch> <onMismatch> DENY </onMismatch> </filter> <! -- 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}/sys-% 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 180, only the files of the last 180 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> 180 </MaxHistory> <! -- When the log file size 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> <appender name = "FILE-ERROR" class = "ch. qos. logback. core. rolling. rollingFileAppender "> <Encoding> UTF-8 </Encoding> <file >$ {LOG_HOME}/$ {appName}/sys-error.log </file> <filter class =" ch. qos. logback. clas Sic. filter. levelFilter "> <level> ERROR </level> <onMatch> ACCEPT </onMatch> <onMismatch> DENY </onMismatch> </filter> <rollingPolicy class =" ch. qos. logback. core. rolling. timeBasedRollingPolicy "> <fileNamePattern >$ {LOG_HOME}/$ {appName}/sys-error-% d {yyyy-MM-dd}-% I. log </fileNamePattern> <MaxHistory> 180 </MaxHistory> <timeBasedFileNamingAndTriggeringPolicy class = "ch. qos. logback. core. rolling. sizeAndTimeBasedFNA TP "> <maxFileSize> 100 MB </maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> <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 --> <! -- Mybatis logger --> <logger name = "org. mybatis" level = "error"/> <! -- Spring framework logger --> <logger name = "org. springframework" level = "error" additi.pdf = "false"> </logger> <! -- <Logger name = "com. ideal" level = "info" additivity = "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 = "debug"> <appender-ref = "STDOUT"/> <appender-ref = "FILE"/> <appender-ref = "FILE -ERROR "/> </root> </configuration>

3.2 use

We can use logs directly by using org. slf4j. LoggerFactory. Definition: protected final Logger logger = LoggerFactory. getLogger (this. getClass (); Use: @ Controller @ RequestMapping (value = "") public class LoginController {@ RequestMapping (value = ") @ ResponseBody public void login (HttpServletResponse response) throws IOException {logger. debug ("log output at the DEBUG level"); logger.info ("log output at the INFO level"); logger. error ("Output ERROR-level logs ");}}

  

 

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.