1, why use Logback?
--System.out is not recommended for development because a large amount of usage increases the consumption of resources. Since the use of System.out is performed on the current thread, writing to the file is also done before proceeding with the following program. While using the log tool can not only control the log output, how to output, its processing mechanism is also a notification to write the log, continue to execute the following code does not have to wait for the log to finish writing.
--The individual recommends using the Logback of slf4j (simple Logging fa?ade for Java) to output the log, which is much more efficient than log4j.
--spring Boot provides a set of logging systems, Logback is the most preferred choice.
2,the relationship between logback,slf4j,log4j
SLF4J is simply Logging facade for Java, is a simple log façade abstract framework, which itself only provides the log facade API and a simple log class implementation, usually with Log4j,logback, Java.util.logging use. SLF4J as the application layer log access, the program can dynamically adjust the underlying log implementation framework (Log4j/logback/jdklog ...) according to the actual application scenario . ;
Logback and log4j are open-source journaling libraries, and Logback is an improved version of log4j, with more features than log4j and a great performance boost.
3, The structure of Logback
Logback is divided into 3 components, Logback-core, Logback-classic and logback-access.
Logback-core provides the core functionality of the Logback, which is the basis for the other two components.
Logback-classic implements the SLF4J API, so when you want to work with SLF4J, you need to add logback-classic to classpath.
Logback-access is prepared for the integration of the servlet environment and provides a http-access log interface;
Spring-boot Default Support Logback , so there is no need to refer to anything since it is only needed, configure application.properties If you want to have more features, you can configure the Logback.xml .
4. Why spring Boot recommends using Logback-spring.xml instead of Logback.xml to configure Logback log problem analysis
5, the specific configuration
Configuration in Application.yml:
Configuration in the Logback-spring.xml;
Multi-Environment configuration:
The multi-environment log output defines different logging outputs according to different environments (prod: Production environment, Test: Testing environment, Dev: development environment), and is defined using Springprofile nodes in Logback-spring.xml as follows: " Note that the file name is not logback.xml and you want to use spring extended profile support to name Logback-spring.xml "<?xml version=" 1.0 "encoding=" UTF-8 "?>< configuration> <include resource= "Org/springframework/boot/logging/logback/base.xml"/> <logger name= "O Rg.springframework.web "level=" INFO "/> <logger name=" org.springboot.sample "level=" TRACE "/> <!--test ring Environment + development environments. Use commas to separate multiple. --<springprofile name= "Test,dev" > <logger name= "org.springframework.web" level= "INFO"/> <logger name= "Org.springboot.sample" level= "info"/> <logger name= "Com.kfit" level= "info"/> </s Pringprofile> <!--production environment. --<springprofile name= "prod" > <logger name= "org.springframework.web" level= "ERROR"/> < ; Logger name= "org.springboot.sample" level= "error"/> <logger name= "com.kfit" level= "error"/> </springprofile> </configuration>
6, the level of the log
The default output logs above this level, in order from high to the end, Error--warn--info--debug--trace
7, how to use in the Java class, Springboot is the Convention is greater than the configuration, so we use annotations to configure the way
How to output variables in the log:
8, Logback-spring.xml of the specific configuration we can refer to the following blog
76565810
http://412887952-qq-com.iteye.com/blog/2307244
<?xml version= "1.0" encoding= "UTF-8"? ><configuration scan= "true" > <!--define the storage address of the log file do not Logback The configuration uses a relative path--<property name= "Log_name" value= "Home" ></property> <!--%m output information,%p log level,%t thread name,%d date, Full name of the%c class,,,,--> <appender name= "STDOUT" class= "Ch.qos.logback.core.ConsoleAppender" > <encoder> <!--<pattern>%d%p (%file:%line\)-%m%n</pattern>--> <!--formatted output:%d: Represents the date%thread: Represents the thread name%-5level: The level displays 5 character widths from the left%msg: Log message%n: Line break--<pattern>1-%d{yyyy-mm-dd HH:MM:SS} [%thread] %-5level%logger-%msg%n</pattern> <charset>GBK</charset> </encoder> </ap pender> <!--<include resource= "Org/springframework/boot/logging/logback/base.xml"/>--> < contextname>restapi</contextname> <property name= "Log_path" value= ". Logs"/> <!--Setting the system log directory-- <property name= "Appdir" value= "app"/> <!--Description: 1, log level and file log records with a hierarchical record, level and log file name corresponds to, different levels of log information recorded in different log files for example: Error level Record to Log_error_xxx.log or Log_error.log (the file is the log file of the current record), and Log_error_xxx.log as the archive log, log file by date, the same day, if the log file size is equal to or greater than 2M, then press 0, 1 , 2 ... the order is named for example Log-level-2013-12-21.0.log other levels of logs. 2, file path if you are developing, testing, running the project in Eclipse, go to the installation path of Eclipse to find the logs folder to the relative path: /logs. If deployed under Tomcat, in the logs file under Tomcat 3, Appender fileerror corresponds to the error level, and the file name is named in log-error-xxx.log form F Ilewarn corresponds to the Warn level, the file name is named FileInfo corresponding to the info level in log-warn-xxx.log form, and the file name is log-info-xxx.log named Filedebug corresponding de Bug level, file name in Log-debug-xxx.log format console logs information to control, for easy development test use-<!--logger, date scrolling record--and Lt;appender name= "Fileerror" class= "Ch.qos.logback.core.rolling.RollingFileAppender" > <!--the path and file name of the log file being logged- <file>${LOG_PATH}/${APPDIR}/log_error.log</file> <!--The Logger's scrolling strategy, by date, by size--<rollingpolicy class= "Ch.qos.logback.core.rolling.TimeBasedRollingPolicy" > <!--the path to the archived log file, for example, today is the 2013-12-21 log, the current write log file path is specified by the file node, you can set this to a different path with the file specified path, thereby placing the current log file or archive log file in a different directory. The 2013-12-21 log file is specified by Filenamepattern. %D{YYYY-MM-DD} Specifies the date format,%i the specified index--<filenamepattern>${log_path}/${appdir}/error/log-error-%d{yyyy-mm-dd}. %i.log</filenamepattern> <!--In addition to logging, the log file can not exceed 2M, if more than 2M, the log file will start at index 0, named log files, such as Log-error -2013-12-21.0.log--<timebasedfilenamingandtriggeringpolicy class= "Ch.qos.logback.core.rolling.SizeAndT IMEBASEDFNATP "> <maxFileSize>2MB</maxFileSize> </timebasedfilenamingandtrigger Ingpolicy> </rollingPolicy> <!--append logging--<append>true</append> <!--log file format-<encoder class= "Ch.qos.logback.classic.encoder.PatternLayoutEncoder" > <pattern>===%d{yyyy-mm-dd HH:mm:ss. SSS}%-5level%logger line:%-3l-%msg%n</pattern> <charset>utf-8</charset> </encod er> <!--This log file only records the info level--<filter class= "Ch.qos.logback.classic.filter.LevelFilter" > <level>error</level> <onMatch>ACCEPT</onMatch> <onmismatch>deny& Lt;/onmismatch> </filter> </appender> <!--logger, date scrolling--<appender name= "Filewa RN "class=" Ch.qos.logback.core.rolling.RollingFileAppender "> <!--the path and file name of the log file that is being logged--<file> ${log_path}/${appdir}/log_warn.log</file> <!--The logger's scrolling strategy, by date, by size--<rollingpolicy class= "Ch.qos.logback.core.rolling.TimeBasedRollingPolicy" > <!--the path to the archived log file, for example, today is the 2013-12-21 log, and the log file path for the current write is for the file node Specifies that this file can be set to a different path from the file specified path, thereby placing the current log file or archive log file in a different directory. And the 2013-12-21 log file was in the FilenamepatteRN designation. %D{YYYY-MM-DD} Specifies the date format,%i the specified index--<filenamepattern>${log_path}/${appdir}/warn/log-warn-%d{yyyy-mm-dd}.%i .log</filenamepattern> <!--In addition to logging, the log file can not exceed 2M, if more than 2M, the log file will start at index 0, named log files, such as Log-error-2 013-12-21.0.log--<timebasedfilenamingandtriggeringpolicy class= "Ch.qos.logback.core.rolling.SizeAndTim EBASEDFNATP "> <maxFileSize>2MB</maxFileSize> </timebasedfilenamingandtriggerin Gpolicy> </rollingPolicy> <!--additional logging--<append>true</append> & lt;! --Log file format--<encoder class= "Ch.qos.logback.classic.encoder.PatternLayoutEncoder" > <patter N>===%d{yyyy-mm-dd HH:mm:ss. SSS}%-5level%logger line:%-3l-%msg%n</pattern> <charset>utf-8</charset> </encod er> <!--This log file records only the info level--<filter class= "Ch.qos.logback.classic.filter.LevelFIlter "> <level>warn</level> <onMatch>ACCEPT</onMatch> <onmi Smatch>deny</onmismatch> </filter> </appender> <!--logger, date scrolling--<append Er name= "FILEINFO" class= "Ch.qos.logback.core.rolling.RollingFileAppender" > <!--the path and file name of the log file that is being recorded-- <file>${LOG_PATH}/${APPDIR}/log_info.log</file> <!--The logger's scrolling strategy, by date, by size-<rolli Ngpolicy class= "Ch.qos.logback.core.rolling.TimeBasedRollingPolicy" > <!--the path to the archived log files, for example, today is the 2013-12-21 log, the current The log file path that is written is specified by the file node, which can be set to a different path from the file specified path, thereby placing the current log file or archive log file in a different directory. The 2013-12-21 log file is specified by Filenamepattern. %D{YYYY-MM-DD} Specifies the date format,%i the specified index--<filenamepattern>${log_path}/${appdir}/info/log-info-%d{yyyy-mm-dd}.%i .log</filenamepattern> <!--In addition to logging, the log file can not exceed 2M, if more than 2M, the log file will start at index 0, named log files, such as Log-error-2 013-12-21.0.log-- <timebasedfilenamingandtriggeringpolicy class= "Ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP" > <maxFileSize>2MB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> </ROLLINGP Olicy> <!--Append way Logging--<append>true</append> <!--log file format--< ; Encoder class= "Ch.qos.logback.classic.encoder.PatternLayoutEncoder" > <pattern>===%d{yyyy-mm-dd hh:mm: Ss. SSS}%-5level%logger line:%-3l-%msg%n</pattern> <charset>utf-8</charset> </encod er> <!--This log file only records the info level--<filter class= "Ch.qos.logback.classic.filter.LevelFilter" > <level>info</level> <onMatch>ACCEPT</onMatch> <ONMISMATCH>DENY&L T;/onmismatch> </filter> </appender> <!--log asynchronous to database--<!--<appender Name= "dbap PENDER "class=" Ch.qos. Logback.classic.db.DBAppender ">--> <!--<connectionsource class=" Ch.qos.logback.core.db.DataSourceConnectionSource ">--> <!--<datasource class=" Com.zaxxer.hikari.HikariDataSource ">--> <!--<driverclassname>com.mysql.jdbc.driver</ Driverclassname>--> <!--<jdbcUrl>jdbc:mysql://localhost:3306/albedo-new?useUnicode=true& Characterencoding=utf8&usessl=false</jdbcurl>--> <!--<username>root</username>-- > <!--<password>123456</password>--> <!--<poolname>hikaripool-logback</poolname >--> <!--</dataSource>--> <!--</connectionSource>--> <!--<!– This log file only records info levels –>--> <!--<filter class= "Ch.qos.logback.classic.filter.LevelFilter" >--> <!--<level> Warn</level>--> <!--<onMatch>ACCEPT</onMatch>--> <!--<onmismatch>deny</ Onmismatch>--> <!--</filter>--> <!--<!– This log file records only the info level –>--> <!--<filter class= "ch.qos.logback.classic.f Ilter. Levelfilter ">--> <!--<level>error</level>--> <!--<onmatch>accept</onmatch >--> <!--<onMismatch>DENY</onMismatch>--> <!--</filter>--> <!--</append er>--> <appender name= "CONSOLE" class= "Ch.qos.logback.core.ConsoleAppender" > <encoder> <pattern>%d{yyyy-mm-dd HH:mm:ss. SSS}%-5level%logger line:%-3l-%msg%n</pattern> <!--<charset>GBK</charset>--> </encoder> </appender> <logger name= "Com.minlia" level= "DEBUG"/> <logger name= "ORG.SPRINGFR Amework.data.mybatis "level=" DEBUG "/> <logger name=" org.springframework.aop.aspectj "level=" ERROR "/> <l Ogger name= "javax.activation" level= "WARN"/> <logger name= "Javax.mail" level= "WARN"/> <loGger name= "Javax.xml.bind" level= "WARN"/> <logger name= "Ch.qos.logback" level= "INFO"/> <logger name= "com . Codahale.metrics "level=" WARN "/> <logger name=" Com.ryantenney "level=" WARN "/> <logger name=" Com.sun "le Vel= "WARN"/> <logger name= "com.zaxxer" level= "WARN"/> <logger name= "Io.undertow" level= "WARN"/> &L T;logger name= "Net.sf.ehcache" level= "WARN"/> <logger name= "Org.apache" level= "WARN"/> <logger name= "org . Apache.catalina.startup.DigesterFactory "level=" OFF "/> <logger name=" Org.bson "level=" WARN "/> <logger N Ame= "Org.hibernate.validator" level= "WARN"/> <logger name= "org.hibernate" level= "WARN"/> <logger name= "O Rg.hibernate.ejb.HibernatePersistence "level=" OFF "/> <logger name=" Org.springframework.web "level=" INFO "/ > <logger name= "org.springframework.security" level= "WARN"/> <logger name= "Org.springframework.cache" le Vel= "WARN"/> <logger naMe= "Org.thymeleaf" level= "WARN"/> <logger name= "Org.xnio" level= "WARN"/> <logger name= "Springfox" level= "WARN"/> <logger name= "Sun.rmi" level= "WARN"/> <logger name= "liquibase" level= "WARN"/> <logger Name= "Sun.rmi.transport" level= "WARN"/> <logger name= "jdbc.connection" level= "ERROR"/> <logger name= "jdb C.resultset "level=" ERROR "/> <logger name=" jdbc.resultsettable "level=" INFO "/> <logger name=" Jdbc.audit " level= "Error"/> <logger name= "jdbc.sqltiming" level= "error"/> <logger name= "jdbc.sqlonly" level= "INFO"/ > <!--<contextlistener class= "Ch.qos.logback.classic.jul.LevelChangePropagator" >--> <!--< resetjul>true</resetjul>--> <!--</contextListener>--> <springprofile name= "Production" > <root level= "DEBUG" > <!--<appender-ref ref= "Fileerror"/>--> <!--< ; Appender-ref ref= "Filewarn"/>-<!--<appender-ref ref= "FILEINFO"/>--> <!--<appender-ref ref= "Dbappender"/&G t;--> <appender-ref ref= "STDOUT"/> </root> </springProfile> <springprofile Name= "Dev" > <root level= "DEBUG" > <!--<appender-ref ref= "Fileerror"/>--> <!--<appender-ref ref= "Filewarn"/>--> <!--<appender-ref ref= "FILEINFO"/>--> <!--<appender-ref ref= "Dbappender"/>--> <appender-ref ref= "CONSOLE"/> </root> </springProfile></configuration>
Springboot Log frame slf4j (use Logback to output logs and use)