Logback is another open source logging component designed by the log4J founders. Logback is currently divided into three modules: logback-core, logback-classic, and logback-access. Logback-core is the base module for the other two modules. Logback-classic is an improved version of Log4J. In addition, logback-classic fully implements the SLF4J API so that you can easily switch to other Logging systems such as Log4J or JDK14 Logging. The logback-Access access module integrates with the Servlet container to provide log access over Http.
vlogback,slf4j,log4j
Logback, SLf4J,log4j
Slf4j, short for The Simple Logging Facade for Java, is a Simple Logging Facade abstraction framework that itself provides only a log Facade API and a Simple Logging class implementation. It is commonly used in conjunction with Log4j, LogBack, Use Java. Util. Logging. Slf4j as the application layer Log access, the program can dynamically adjust the underlying Log implementation framework according to the actual application scenarios (Log4j/LogBack/JdkLog…) ;
LogBack and Log4j are both open source journaling libraries. LogBack is an improved version of Log4j with more features and performance improvements than Log4j.
V Add reference
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
Copy the code
If the IDEA tool prompts error messages, you can download the Lombok plug-in
VLogback. XML configuration
Logback is the default value
If neither the logback-test.xml nor logback.xml configuration files exist, logback by default calls BasicConfigurator to create a minimal configuration. The minimize configuration consists of a ConsoleAppender associated with the root Logger. The output is formatted with PatternLayoutEncoder with mode % D {HH:mm: ss.sss} [%thread] %-5level % Logger {36} – % MSG %n. The default logger level is DEBUG.
The default Logback configuration steps are as follows:
Logback-test. XML file in classpath;
If the file does not exist, look for the file logback.xml.
③ If neither file exists, Logback automatically configures itself with the Bas icConfigurator, which results in records being output to the console.
Add Logback. XML
<? The XML version = "1.0" encoding = "utf-8"? > <configuration debug="false"> <! <property name="LOG_HOME" value="E:\work\log" /> <! - the console output - > < appender name = "STDOUT" class = "ch. Qos. Logback. Core. ConsoleAppender" > < encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <! Format output: %d indicates the date, %thread indicates the thread name, %-5level indicates the level from the left 5 character width % MSG: Log messages, SSS} [%thread] %-5level % Logger {50} - % MSG %n</pattern> </encoder> </appender> <! Generated according to the daily log FILE - > < appender name = "FILE" class = "ch. Qos. Logback. Core. Rolling. RollingFileAppender" > < rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <! ${LOG_HOME}/log.%d{YYYY-MM-DD}. Log </FileNamePattern> <! -- Log file retention days --> <MaxHistory>30</MaxHistory> </rollingPolicy> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <! Format output: %d indicates the date, %thread indicates the thread name, %-5level indicates the level from the left 5 character width % MSG: -> <pattern>%d{YYYY-MM-DD HH: MM :ss.SSS} [%thread] %-5level % Logger {50} - % MSG %n</pattern> </ coder> <! -- the largest size in the log file -- > < triggeringPolicy class = "ch. Qos. Logback. Core. Rolling. SizeBasedTriggeringPolicy" > <MaxFileSize>10MB</MaxFileSize> </triggeringPolicy> </appender> <! - show the parameters for SQL for hibernate hibernate custom - > < logger name = ". Org. Hibernate. The 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"/> <! --> <root level="ERROR"> <appender-ref ref="STDOUT" /> <appender-ref ref="FILE" /> </root> <! -- Log asynchrony to database --> <! --<appender name="DB" class="ch.qos.logback.classic.db.DBAppender">--> <! --< ! – Asynchronous log to database & ndASH; > -- > <! --<connectionSource class="ch.qos.logback.core.db.DriverManagerConnectionSource">--> <! --< ! – The connection pool & ndash; > -- > <! --<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>Copy the code
Adding test code
Effect of log
Other references:
- Spring Boot Reference Guide docs. Spring. IO/Spring – Boot…
- Spring the boot – based using logback article www.cnblogs.com/lixuwu/p/58…
- Spring Boot SLF4J Logging examplewww.mkyong.com/spring-boot…
- Spring boot slf4j diary records configuration, rounding blog.csdn.net/liuweixiao5…
- Logging with Spring Boot – Logback SLF4j and LOG4j2 – Spring Boot Tutorial www.springboottutorial.com/logging-wit…
V Source code address
Github.com/toutouge/ja…
About the author: Focus on basic platform project development. If you have any questions or suggestions, please feel free to comment! Copyright notice: The copyright of this article belongs to the author and the blog garden, welcome to reprint, but without the consent of the author must retain this statement, and give the original text link in a prominent place on the page of the article. For the record: all comments and messages will be answered as soon as possible. You are welcome to correct your mistakes and make progress together. Or direct private message I support the blogger: if you think the article is helpful to you, you can click on the lower right corner of the article [recommendation]. Your encouragement is the author to adhere to the original and continuous writing of the biggest power! \