logback日志文件的使用
1、引入Jar包,Maven pom.xml
1 <!-- Logging with SLF4J & LogBack --> 2 <dependency> 3 <groupId>org.slf4j</groupId> 4 <artifactId>jcl-over-slf4j</artifactId> 5 <version>1.7.13</version> 6 </dependency> 7 <dependency> 8 <groupId>org.slf4j</groupId> 9 <artifactId>slf4j-api</artifactId> 10 <version>1.7.13</version> 11 </dependency> 12 <dependency> 13 <groupId>org.slf4j</groupId> 14 <artifactId>jul-to-slf4j</artifactId> 15 <version>1.7.13</version> 16 </dependency> 17 <dependency> 18 <groupId>ch.qos.logback</groupId> 19 <artifactId>logback-classic</artifactId> 20 <version>1.1.3</version> 21 </dependency> 22 <dependency> 23 <groupId>ch.qos.logback</groupId> 24 <artifactId>logback-core</artifactId> 25 <version>1.1.3</version> 26 </dependency> 27 <dependency> 28 <groupId>ch.qos.logback</groupId> 29 <artifactId>logback-access</artifactId> 30 <version>1.1.3</version> 31 </dependency>
2、配置日志文件 logback.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <configuration> 3 <contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator"> 4 <resetJUL>true</resetJUL> 5 </contextListener> 6 <!-- To enable JMX Management --> 7 <jmxConfigurator /> 8 9 <!-- 打印到控制台 --> 10 <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> 11 <!-- encoder 默认配置为PatternLayoutEncoder --> 12 <layout class="ch.qos.logback.classic.PatternLayout"> 13 <Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{40} - %msg%n</Pattern> 14 </layout> 15 </appender> 16 17 <!-- 打印日志到文件 --> 18 <!-- 所有日志文件 --> 19 <appender name="AllLog" class="ch.qos.logback.core.rolling.RollingFileAppender"> 20 <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> 21 <fileNamePattern>log/shiro/all/%d{yyyy-MM-dd}.log</fileNamePattern> 22 <maxHistory>30</maxHistory> 23 </rollingPolicy> 24 <encoder> 25 <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{40} - %msg%n</pattern> 26 </encoder> 27 </appender> 28 <!-- Task日志文件 --> 29 <appender name="Tasklog" class="ch.qos.logback.core.rolling.RollingFileAppender"> 30 <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> 31 <fileNamePattern>log/shiro/task/%d{yyyy-MM-dd}.log</fileNamePattern> 32 <maxHistory>30</maxHistory> 33 </rollingPolicy> 34 <encoder> 35 <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{40} - %msg%n</pattern> 36 </encoder> 37 </appender> 38 39 <!-- 为莫个包下的所有类指定Appender 这里也可以指定类名称例如:com.mcs.task.ClassName --> 40 <logger name="cn.mcs.test" level="DEBUG" > 41 <appender-ref ref="STDOUT" /> 42 </logger> 43 <logger name="cn.mcs.task" level="DEBUG" > 44 <appender-ref ref="Tasklog" /> 45 </logger> 46 47 <logger name="org.springframework" level="INFO" /> 48 <logger name="org.springframework.beans" level="DEBUG" /> 49 50 <!-- 常用级别:ERROR 、WARN 、INFO 、DEBUG 、TRACE --> 51 <root level="WARN"> 52 <appender-ref ref="STDOUT" /> 53 <!-- <appender-ref ref="AllLog" /> --> 54 </root> 55 56 57 </configuration>
3、在程序中使用
1 import org.slf4j.Logger; 2 import org.slf4j.LoggerFactory; 3 4 public class LogbackTest { 5 private static Logger log = LoggerFactory.getLogger(LogbackTest .class); 6 public static void main(String[] args) { 7 log.trace("======trace"); 8 log.debug("======debug"); 9 log.info("======info"); 10 log.warn("======warn"); 11 log.error("======error"); 12 } 13 }