springBoot集成logBack日志

2019-04-30 01:47:27  卢浮宫  阅读量:136  版权声明:本文为站长原创文章,转载请写明出处


一、今天早作日志功能时,使用到了logbck,这里简单记录下(PS:感谢Kenny大神的一发支持)


二、首先是maven引用

        

    <dependency>
             <groupId>ch.qos.logback</groupId>
             <artifactId>logback-core</artifactId>
             <version>1.2.3</version>
         </dependency>
         <dependency>
             <groupId>ch.qos.logback</groupId>
             <artifactId>logback-classic</artifactId>
             <version>1.2.3</version>
         </dependency>

 
三、logback.xml配置文件(这里放在resources下)
 
 
<?xml version="1.0" encoding="UTF-8"?>
 <configuration scan="true">
 
     <property name="log.root.level" value="INFO" /> <!-- 日志级别 -->
     <property name="log.other.level" value="DEBUG" /> <!-- 其他日志级别 -->
     <property name="log.moduleName" value="tongcheng" />  <!-- 模块名称, 影响日志配置名,日志文件名 -->
     <!--日志文件的保存路径,首先查找系统属性-Dlog.dir,如果存在就使用其;否则,在当前目录下创建名为logs目录做日志存放的目录 -->
     <property name="log.base" value="${log.dir:-logs}/${log.moduleName}" />
     <property name="log.max.size" value="100MB" /> <!-- 日志文件大小,超过这个大小将被压缩 -->
 
     <!--控制台输出 -->
     <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
         <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
             <Pattern>%date{yyyy-MM-dd HH:mm:ss.SSS} %-5level[%thread]%logger{56}.%method:%L -%msg%n</Pattern>
         </encoder>
     </appender>
 
     <!-- 只用保存输出error级别的日志 -->
     <appender name="file.error"
               class="ch.qos.logback.core.rolling.RollingFileAppender">
         <File>${log.base}/${log.moduleName}_err.log</File>
         <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
             <FileNamePattern>${log.base}/archive/${log.moduleName}_err_%d{yyyy-MM-dd}.%i.log.zip
             </FileNamePattern>
             <timeBasedFileNamingAndTriggeringPolicy
                     class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                 <maxFileSize>${log.max.size}</maxFileSize>
             </timeBasedFileNamingAndTriggeringPolicy>
         </rollingPolicy>
         <layout class="ch.qos.logback.classic.PatternLayout">
             <pattern>%date{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{56}.%method:%L - %msg%n</pattern>
         </layout>
         <!-- 下面为配置只输出error级别的日志 -->
         <filter class="ch.qos.logback.classic.filter.LevelFilter">
             <level>ERROR</level>
             <onMatch>ACCEPT</onMatch>
             <onMismatch>DENY</onMismatch>
         </filter>
     </appender>
 
     <!-- 只用保存输出error级别的日志 -->
     <appender name="file.info"
               class="ch.qos.logback.core.rolling.RollingFileAppender">
         <File>${log.base}/${log.moduleName}_info.log</File>
         <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
             <FileNamePattern>${log.base}/archive/${log.moduleName}_info_%d{yyyy-MM-dd}.%i.log.zip
             </FileNamePattern>
             <timeBasedFileNamingAndTriggeringPolicy
                     class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                 <maxFileSize>${log.max.size}</maxFileSize>
             </timeBasedFileNamingAndTriggeringPolicy>
         </rollingPolicy>
         <layout class="ch.qos.logback.classic.PatternLayout">
             <pattern>%date{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{56}.%method:%L - %msg%n</pattern>
         </layout>
         <!-- 下面为配置只输出error级别的日志 -->
         <filter class="ch.qos.logback.classic.filter.LevelFilter">
             <level>INFO</level>
             <onMatch>ACCEPT</onMatch>
             <onMismatch>DENY</onMismatch>
         </filter>
     </appender>
 
     <!-- root将级别为${log.root.level}及大于${log.root.level}的日志信息交给已经配置好的名为“Console”的appender处理,“Console”appender将信息打印到Console,其它同理 -->
     <root level="${log.root.level}">
         <appender-ref ref="stdout" /> <!--  标识这个appender将会添加到这个logger -->
         <appender-ref ref="file.error" />
         <appender-ref ref="file.info" />
     </root>
 </configuration>

最新评论:
1 2019-05-11 00:01:48
1111111111111111111111111111111111111111111111111111111111111111111111111111111111 2019-05-11 00:01:35
.. 2019-05-11 00:01:23
0 0 2019-05-11 00:01:20