Springboot logback Log Configuration

Source: Internet
Author: User

Logback Configuration Description:

1, Logger, Appender and layout

Logger, which is used as a logger for logging, is associated with the corresponding context of the application, primarily for storing log objects, and for defining log types and levels.

The Appender is primarily used to specify the destination of the log output, which can be a console, file, remote socket server, MySQL, Postresql, Oracle and other databases, JMS and remote Unix syslog daemon, and so on.

Layout is responsible for converting events into strings and formatting the output of log information.

2, Logger context

Each logger is associated to a loggercontext,loggercontext responsible for manufacturing logger, and is responsible for arranging the logger in a tree structure. All other logger are also obtained by GetLogger the static method of the Org.slf4j.LoggerFactory class. The GetLogger method is called a parameter with the logger name. Calling the Loggerfactory.getlogger method with the same name will always get a reference to the same logger object.

3. Inheritance of effective levels and levels

Logger can be assigned a level. Levels include: TRACE, DEBUG, INFO, WARN, and ERROR, defined in the Ch.qos.logback.classic.Level class. If the logger is not assigned a level, it inherits from the nearest ancestor with the assigned level. Root logger The default level is DEBUG.

4. Printing methods and basic selection rules

The Print method determines the level at which requests are recorded. For example, if L is an logger instance, then the statement l.info ("..") is a record statement with a level of INFO. The level of logging requests is known to be enabled when it is higher or equal to the valid level of its logger, otherwise, it is called disabled. The record request level is P, and its logger has a valid level of Q, and the request is only executed when p>=q.

This rule is the core of Logback. Levels sorted By: TRACE < DEBUG < INFO < WARN < ERROR.

use of Logback:

Default configuration for Logback

If none of the profiles Logback-test.xml and Logback.xml exist, then Logback calls Basicconfigurator by default, creating a minimized configuration. The minimized configuration consists of a consoleappender that is associated to the root logger. The output mode is%D{HH:MM:SS. SSS} [%thread]%-5level%logger{36}-%msg%n Patternlayoutencoder is formatted. Root logger The default level is DEBUG.

1. Logback configuration file

The syntax of the Logback configuration file is very flexible. Because it is flexible, it cannot be defined with a DTD or XML schema. However, the basic structure of the configuration file can be described as follows:

2, Logback the default configuration steps

(1). Try to find the file logback-test.xml under Classpath;

(2). If the file does not exist, find the file logback.xml;

(3). If none of the two files exist, Logback automatically configures itself with Bas Icconfigurator, which results in a record output to the console.

3. logback.xml file

Under Src/main/resources, add the Logback.xml directory as follows:

The contents are as follows:

<?xml version= "1.0" encoding= "UTF-8"?>
<configuration Debug="false">
<!--define the storage address of the log file do not use relative paths in Logback configuration
< property name="Log_home" value="/test/log" />
<!--console output --
<appender name= "STDOUT" class=" Ch.qos.logback.core.ConsoleAppender ">
<encoder class="Ch.qos.logback.classic.encoder.PatternLayoutEncoder" >
<!--formatted output:%d represents the date,%thread represents the thread name,%-5level: the level displays 5 character widths from the left%msg: Log messages,%n are newline characters--
<pattern>%d{yyyy-mm-dd HH:mm:ss. SSS} [%thread]%-5level%logger{50}-%msg%n</pattern>
</encoder>
</appender>
<!--generate log files per day --
<appender name= "FILE"class=" Ch.qos.logback.core.rolling.RollingFileAppender ">
<rollingpolicy class="Ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!--log file output file name--
<filenamepattern>${log_home}/testweb.log.%d{yyyy-mm-dd}.log</filenamepattern>
<!--log file retention days --
<maxhistory>30</maxhistory>
</rollingpolicy>
<encoder class="Ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<!--formatted output:%d represents the date,%thread represents the thread name,%-5level: the level displays 5 character widths from the left%msg: Log messages,%n are newline characters--
<pattern>%d{yyyy-mm-dd HH:mm:ss. SSS} [%thread]%-5level%logger{50}-%msg%n</pattern>
</encoder>
<!--The maximum size of the log file--
<triggeringpolicy class="Ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy" >
<maxfilesize>10MB</maxfilesize>
</triggeringpolicy>
</appender>
<!--show parameters for Hibernate SQL is custom-designed for Hibernate-
<logger name="Org.hibernate.type.descriptor.sql.BasicBinder"level = "TRACE" />
<logger name="Org.hibernate.type.descriptor.sql.BasicExtractor"level ="DEBUG" />
<logger name="Org.hibernate.SQL" level ="DEBUG" / >
<logger name="Org.hibernate.engine.QueryParameters" level ="DEBUG" />
<logger name= "Org.hibernate.engine.query.HQLQueryPlan" level =" DEBUG " />


<!--myibatis log configure-->
<logger name="Com.apache.ibatis" level ="TRACE"/ >
<logger name="Java.sql.Connection" level ="DEBUG"/>
<logger name="Java.sql.Statement" level ="DEBUG"/>
<logger name="Java.sql.PreparedStatement" level ="DEBUG" />

<!--log output level -
<root level ="INFO">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
</root>
<!--log asynchronously to a database --
<!--<appender name= "DB" class= "Ch.qos.logback.classic.db.DBAppender" >-->
<!--&lt;! &ndash; log asynchronous to database &ndash;&gt;-->
<!--<connectionsource class= "Ch.qos.logback.core.db.DriverManagerConnectionSource" >-->
<!--&lt;! &ndash; Connection Pool &ndash;&gt;-->
<!--<datasource class= "Com.mchange.v2.c3p0.ComboPooledDataSource" >-->
<!--<driverClass>com.mysql.jdbc.Driver</driverClass>-->
<!--<url>jdbc:mysql://127.0.0.1:3306/databaseName</url>-->
<!--<user>root</user>-->
<!--<password>root</password>-->
<!--</dataSource>-->
<!--</connectionSource>-->
<!--</appender>-->
</configuration>

In the program with reference Logback:

Package com.example.web;
ImportOrg.slf4j.Logger;
ImportOrg.slf4j.LoggerFactory;
Importorg.springframework.web.bind.annotation.RequestMapping;
ImportOrg.springframework.web.bind.annotation.RestController
;@Restcontroller
Public class Example {
Private Final Static LoggerLogger =loggerfactory. GetLogger (Example.class);
@requestmapping("/")
Public StringHome () {
Logger.info ("Logback Access Hello");
Logger.error ("Logback Access Hello");
return "Hello world!";
}
}

By running the program and accessing: http://localhost:8080, you can get the following log output:

Springboot logback Log Configuration

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.