工程下载:https://files.cnblogs.com/files/xiandedanteng/logbackCfg20200115.zip
首先创建一个maven项目,pom.xml如下书写:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com</groupId> <artifactId>logbackCfg</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.1.11</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>1.1.11</version> </dependency> </dependencies> </project>
注意上面的dependency是在我本地库既存的logback库,请你也参照自己本地库设置(查看方法:Preference->Maven->User settings->Local Respository),如果没有请在网上寻找官网通用配置。
其次,在src/main/resource放入logback.xml,内容如下:
<?xml version="1.0" encoding="UTF-8"?> <configuration debug="false"> <!-- Where are log files --> <property name="LOG_HOME" value="d:/logs" /> <!-- Output to Console --> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <!--%d:date,%thread:thread,%-5level:error/debug/info... %msg:message,%n:new line --> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} -%msg%n</pattern> </encoder> </appender> <!-- Output to File --> <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!--log file pathname --> <FileNamePattern>${LOG_HOME}/logbackCfg.log.%d{yyyy-MM-dd}.log </FileNamePattern> <!--days log files will be kept --> <MaxHistory>30</MaxHistory> </rollingPolicy> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <!--%d:date,%thread:thread,%-5level:error/debug/info... %msg:message,%n:new line --> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} -%msg%n</pattern> </encoder> <!--size --> <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> <MaxFileSize>10MB</MaxFileSize> </triggeringPolicy> </appender> <!-- log level TRACE, DEBUG, INFO, WARN, ERROR, ALL and OFF,default:DEBUG。--> <root level="ALL"> <appender-ref ref="STDOUT" /> <!-- show log on console --> <appender-ref ref="FILE" /> <!-- show log in file --> </root> </configuration>
其三,写代码测试:
package logbackCfg; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class App { private final static Logger logger = LoggerFactory.getLogger(App.class); public static void main(String[] args) { logger.info("秦时明月汉时关"); logger.error("万里长征人未还"); logger.debug("但使龙城飞将在"); logger.trace("不教胡马度阴山"); } }
控制台输出:
2020-01-15 15:45:48.409 [main] INFO logbackCfg.App -秦时明月汉时关 2020-01-15 15:45:48.412 [main] ERROR logbackCfg.App -万里长征人未还 2020-01-15 15:45:48.413 [main] DEBUG logbackCfg.App -但使龙城飞将在 2020-01-15 15:45:48.413 [main] TRACE logbackCfg.App -不教胡马度阴山
文件输出:
2020-01-15 15:44:12.310 [main] INFO logbackCfg.App -秦时明月汉时关 2020-01-15 15:44:12.313 [main] ERROR logbackCfg.App -万里长征人未还 2020-01-15 15:44:12.313 [main] DEBUG logbackCfg.App -但使龙城飞将在 2020-01-15 15:44:42.366 [main] INFO logbackCfg.App -秦时明月汉时关 2020-01-15 15:44:42.368 [main] ERROR logbackCfg.App -万里长征人未还 2020-01-15 15:44:42.369 [main] DEBUG logbackCfg.App -但使龙城飞将在 2020-01-15 15:44:42.369 [main] TRACE logbackCfg.App -不教胡马度阴山 2020-01-15 15:45:48.409 [main] INFO logbackCfg.App -秦时明月汉时关 2020-01-15 15:45:48.412 [main] ERROR logbackCfg.App -万里长征人未还 2020-01-15 15:45:48.413 [main] DEBUG logbackCfg.App -但使龙城飞将在 2020-01-15 15:45:48.413 [main] TRACE logbackCfg.App -不教胡马度阴山
参考: