logback.xml配置示例

2019-08-09 17:17:32 浏览数 (1)

1.需要的jar如下:

代码语言:javascript复制
<dependency>
     <groupId>org.slf4j</groupId>
     <artifactId>slf4j-api</artifactId>
     <version>1.7.4</version>
 </dependency>
 <dependency>
     <groupId>ch.qos.logback</groupId>
     <artifactId>logback-classic</artifactId>
     <version>1.0.10</version>
 </dependency>
 <dependency>
     <groupId>commons-logging</groupId>
     <artifactId>commons-logging</artifactId>
     <version>1.1.1</version>
 </dependency>

2.将logback.xml放入项目src路径下即可,文件内容如下:

代码语言:javascript复制
<?xml version="1.0" encoding="UTF-8"?>
 <configuration>
 <jmxConfigurator />

 <!-- 日志添加到控制台 -->
 <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
     <encoder>
         <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
     </encoder>
 </appender>

 <!-- 滚动记录文件,先将日志记录到指定文件,当符合某个条件时,将日志记录到其他文件 -->
 <appender name="rollingFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
     <file>logs/quickstart.log</file>
     <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
         <fileNamePattern>logs/smt.%d{yyyy-MM-dd}.log</fileNamePattern>
     </rollingPolicy>
     <encoder>
         <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
     </encoder>
 </appender>

 <appender name="businessLogFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
     <!-- 按每小时滚动文件,如果一个小时内达到10M也会滚动文件, 滚动文件将会压缩成zip格式 -->
     <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
         <fileNamePattern>logs/smt-%d{yyyy-MM-dd_HH}.%i.zip</fileNamePattern>
         <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
             <maxFileSize>10MB</maxFileSize>
         </timeBasedFileNamingAndTriggeringPolicy>
     </rollingPolicy>
     <!-- 独立的pattern -->
     <encoder>
         <pattern>%d{HH:mm:ss.SSS},%msg%n</pattern>
     </encoder>
 </appender>

 <!-- project default level 本身没有指定appender不打印 传递给上级root-->
 <logger name="com.github.miemiedev.smt" level="DEBUG" />
 <logger name="org.mybatis.spring.SqlSessionFactoryBean" level="DEBUG" />



 <!--log4jdbc -->
 <!-- <logger name="jdbc.sqltiming" level="INFO"/> -->

 <root level="WARN">
     <appender-ref ref="console" />
     <appender-ref ref="rollingFile" />
 </root>
 </configuration>
 

0 人点赞