1. Preparation of components before use:
The maven project uses logback:
Add to pom file:
<dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>1.1.5</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.1.5</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-access</artifactId> <version>1.1.5</version> </dependency>
Components can also be added manually:
logback-access-1.1.5.jar
logback-classic-1.1.5.jar
logback-core-1.1.5.jar
slf4j-api-1.7.16.jar
Note: If you use open source frameworks such as struts, spring, hibernate, etc. in combination with frameworks, many of these frameworks use log4j to record logs, so log4j-over-slf4j-1.6.6.jar is required
2. Start using
1. Use logback to print the log to the console:
Sample code:
/** * @author zw Date: 2016/10/9 Time:15:04. */ public class TestDemo { private static Logger log = LoggerFactory.getLogger(TestDemo.class); public static void main(String[]args){ /** * The log log is divided into five levels of trace debug info warn error * level rank order trace < debug < info < warn < error */ log.trace("----this log is trace level"); log.debug("----this log is debug level"); log.info("----this log is info level"); log.warn("----this log is warn level"); log.error("----this log is error level"); String name = "logback"; String message = "demo"; String[] params = { "logback", "logback" }; // The printing method provided by logback that can use variables log.info("##########hello,{}!", name); // Use placeholders to output log information log.info("##########hello {}! this is {}!", name, message); // can pass an array log.info("##########hello {}! this is {}", params); } }
logback.xml configuration file
<?xml version="1.0" encoding="UTF-8"?> <!--configuration attribute description: scan attribute: used to set whether to reload when the configuration file changes, the value is true\false, the default is true, that is, reload when the configuration changes scanPeriod attribute: used to set how often to check whether the configuration changes, the default is 1 minute, the default unit is milliseconds debug attribute: used to set whether to print out the internal log of logback, that is, output configuration loading information at startup. The value is true\false and the default is false--> <configuration scan="true" scanPeriod="60 seconds" debug="false"> <!--Define log output format--> <property name="enPattern" value="%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n" /> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder > <pattern>${enPattern}</pattern> </encoder> </appender> <!-- The default level of root is DEBUG and can contain multiple <appender-ref> elements --> <root level="TRACE"> <appender-ref ref="STDOUT" /> </root> </configuration>
2. Record the log to a file:
Sample code: Same as the sample code in 1.
logback configuration file modification:
In configuration add:
<!--Define log output file directory--> <property name="outFilePath" value="E:/个人/demo/logs/demo.log" />
and replace the appender node,
<appender name="STDOUT" class="ch.qos.logback.core.FileAppender"> <file>${outFilePath}</file> <append>true</append> <encoder charset="UTF-8"> <pattern>${enPattern}</pattern> </encoder> </appender>
3. Output to file and console at the same time:
The sample code is the same as 1.
The configuration file is as follows:
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder charset="UTF-8"> <pattern>${enPattern}</pattern> </encoder> </appender> <appender name="toFile" class="ch.qos.logback.core.FileAppender"> <file>${outFilePath}</file> <append>true</append> <encoder charset="UTF-8"> <pattern>${enPattern}</pattern> </encoder> </appender> <!-- The default level of root is DEBUG and can contain multiple <appender-ref> elements --> <root level="debug"> <appender-ref ref="STDOUT" /> <appender-ref ref="toFile" /> </root>
4. Output the log to a file and wait for a file every day or every hour:
Sample code: same as 1:
Configuration file:
On the basis of 3, modify. Increase:
<!--Define the log output file before today--> <property name="outOldFilePath" value="E:/个人/demo/logs/demo-%d{yyyy-MM-dd}.log" />
And modify the appender whose name is toFile, after modification as follows:
<!--Define log output format--> <property name="enPattern" value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level {%C.java:%L} - %msg%n" /> <!--Define log output file directory--> <property name="outFilePath" value="E:/个人/demo/logs/demo.log" /> <!--Define the log output file before today--> <property name="outOldFilePath" value="E:/个人/demo/logs/demo-%d{yyyy-MM-dd}.log" /> <!--Configuration with loger, do not specify level, do not specify appender output to console --> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder charset="UTF-8"> <pattern>${enPattern}</pattern> </encoder> </appender> <!--Today's log is output to outFilePath, and the log before today is in --> <appender name="toFile" class="ch.qos.logback.core.rolling.RollingFileAppender"> <!--Log file for today's log records--> <file>${outFilePath}</file> <append>true</append> <!--Set yesterday's log file--> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!--The log file of yesterday's log--> <fileNamePattern>${outOldFilePath}</fileNamePattern> <!--Keep logs for 30 days--> <maxHistory>30</maxHistory> </rollingPolicy> <encoder charset="UTF-8"> <pattern>${enPattern}</pattern> </encoder> </appender> <!-- The default level of root is DEBUG and can contain multiple <appender-ref> elements --> <root level="debug"> <appender-ref ref="STDOUT" /> <appender-ref ref="toFile" /> </root>
You can also output files by minutes or hours, adjust
<property name="outOldFilePath" value="E:/个人/demo/logs/demo-%d{yyyy-MM-dd}.log" />
for:
a. By hour:
<property name="outOldFilePath" value="E:/个人/demo/logs/demo-%d{yyyyMMddHH}.log" />
b. In minutes:
<property name="outOldFilePath" value="E:/个人/demo/logs/demo-%d{yyyyMMddHHMM}.log" />
Please correct me if I misunderstand, thank you