logback日志文件的使用

时间:2022-12-24 04:45:52

1、引入Jar包,Maven pom.xml

                 <!-- Logging with SLF4J & LogBack -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.7.</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
<version>1.7.</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.1.</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-access</artifactId>
<version>1.1.</version>
</dependency>

2、配置日志文件 logback.xml

 <?xml version="1.0" encoding="UTF-8"?>
<configuration>
<contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator">
<resetJUL>true</resetJUL>
</contextListener>
<!-- To enable JMX Management -->
<jmxConfigurator /> <!-- 打印到控制台 -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoder 默认配置为PatternLayoutEncoder -->
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{} - %msg%n</Pattern>
</layout>
</appender> <!-- 打印日志到文件 -->
<!-- 所有日志文件 -->
<appender name="AllLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>log/shiro/all/%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory></maxHistory>
</rollingPolicy>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{} - %msg%n</pattern>
</encoder>
</appender>
<!-- Task日志文件 -->
<appender name="Tasklog" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>log/shiro/task/%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory></maxHistory>
</rollingPolicy>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{} - %msg%n</pattern>
</encoder>
</appender> <!-- 为莫个包下的所有类指定Appender 这里也可以指定类名称例如:com.mcs.task.ClassName -->
<logger name="cn.mcs.test" level="DEBUG" >
<appender-ref ref="STDOUT" />
</logger>
<logger name="cn.mcs.task" level="DEBUG" >
<appender-ref ref="Tasklog" />
</logger> <logger name="org.springframework" level="INFO" />
<logger name="org.springframework.beans" level="DEBUG" /> <!-- 常用级别:ERROR 、WARN 、INFO 、DEBUG 、TRACE -->
<root level="WARN">
<appender-ref ref="STDOUT" />
<!-- <appender-ref ref="AllLog" /> -->
</root> </configuration>

3、在程序中使用

 import org.slf4j.Logger;
import org.slf4j.LoggerFactory; public class LogbackTest {
private static Logger log = LoggerFactory.getLogger(LogbackTest .class);
public static void main(String[] args) {
log.trace("======trace");
log.debug("======debug");
log.info("======info");
log.warn("======warn");
log.error("======error");
}
}