日志是程序中必不可少的内容。依据日志我们可以有效诊断程序bug,统计用户访问和各主要功能的使用频率时间段等信息。因此我们会需要不同package使用不同的日志级别,以及不同业务的日志输出到不同的文件。下面本文简要概述如何使用logback将不同包的不同级别日志输出到info1.txt中。
本文的代码在这里(https://github.com/yqbjtu/springboot/tree/1.0/LogbackDemo),欢迎加星,fork。
1, springboot配置日志logback日志的方式
官方文档在https://docs.spring.io/spring-boot/docs/1.5.12.RELEASE/reference/html/boot-features-logging.html#boot-features-logging-format
我们一般只需要在application.properties中设置类似如下的配置即可。
logging.config=classpath:logback-spring2.xml
2,程序的基本结构
程序是标准的springboot程序,基于springboot 1.5.12, 使用swagger访问rest。包含service包和controller包。
3,logback.xml配置
简要介绍,logback配置文件中有3个appender, 分别为, a,控制台(级别为debug)输出到console中,b,FILE(没有定义级别)输出到logs/info1.log中,自动归档每天的日志,修改为2018-11-10.txt这样保存起来。只保留最近7天的日志。c,ERROR_FILE与FILE类似,只记录error级别的日志。
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<!-- 控制台输出 -->
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>DEBUG</level> <!-- 日志过滤级别 -->
</filter>
<encoder>
<pattern>%date %-5level [${HOSTNAME} %thread] %caller{1}%msg%n</pattern><!-- 格式化输出 -->
<charset>utf8</charset> <!-- 输出编码 -->
</encoder>
</appender>
<!-- 文件输出 -->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<encoder>
<pattern>%date %-5level [${HOSTNAME} %thread] %caller{1}%msg%n</pattern><!-- 格式化输出 -->
<charset>utf8</charset> <!-- 输出编码 -->
</encoder>
<file>./logs/info1.log</file><!-- 文件存放路径 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>/logs/info/%d{yyyy-MM-dd}.log</fileNamePattern><!-- 每天归档 -->
<maxHistory>7</maxHistory><!-- 日志存放周期(天) -->
</rollingPolicy>
</appender>
<!-- 错误日志文件输出 -->
<appender name="ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level> <!-- 日志过滤级别 -->
</filter>
<encoder>
<pattern>%date %-5level [${HOSTNAME} %thread] %caller{1}%msg%n</pattern><!-- 格式化输出 -->
<charset>utf8</charset> <!-- 输出编码 -->
</encoder>
<file>./logs/error1.log</file><!-- 文件存放路径 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>/logs/error/%d{yyyy-MM-dd}.log</fileNamePattern><!-- 每天归档 -->
<maxHistory>7</maxHistory><!-- 日志存放周期(天) -->
</rollingPolicy>
</appender>
<!-- 基础日志输出级别 -->
<root level="DEBUG">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="ERROR_FILE"/>
</root>
<logger name="com.yq.controller" level="trace" additivity="false">
<appender-ref ref="FILE" />
</logger>
<logger name="com.yq.service" level="info" additivity="false">
<appender-ref ref="FILE" />
</logger>
<logger name="org.springframework.beans" level="debug" additivity="false">
<appender-ref ref="FILE" />
</logger>
</configuration>
xml文件解释
com.yq.controller包trace级别及其以上的日志,com.yq.service包info级别及其以上的日志,org.springframework.beans包debug级别及其以上的日志会记录在FILE中,也就是记录到info1.txt中
启动程序后访问http://127.0.0.1:9090/swagger-ui.html#/user-controller/getUserUsingGET, 点击getUser方法。
然后查看控制台日志,程序所在目录下的logs目录中的info1.txt和error1.txt。
结果,按照如上配置,
info1.txt记录了很多springboot的日志包括debug级别的日志,
com.yq.controller包下面的所有trace级别以上信息
com.yq.service包下面的所有info级别以上的信息
org.springframework.beans包下面的所有debug级别以上的信息
error1.txt只记录com.yq.LogbackApplication的错误日志,没有com.yq.controller和com.yq.controller的错误
console中controller包和servcie包都没有记录, 只有springboot的com.yq.LogbackApplication的日志
info1.txt内容为
Returning cached instance of singleton bean ‘mvcViewResolver’
2018-11-10 15:23:26,769 DEBUG [DESKTOP-8S2E5H7 http-nio-9090-exec-1] Caller+0 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251)
Returning cached instance of singleton bean ‘defaultViewResolver’
2018-11-10 15:23:26,769 DEBUG [DESKTOP-8S2E5H7 http-nio-9090-exec-1] Caller+0 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251)
Returning cached instance of singleton bean ‘viewResolver’
2018-11-10 15:23:26,769 DEBUG [DESKTOP-8S2E5H7 http-nio-9090-exec-1] Caller+0 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251)
Returning cached instance of singleton bean ‘thymeleafViewResolver’
2018-11-10 15:23:26,770 DEBUG [DESKTOP-8S2E5H7 http-nio-9090-exec-1] Caller+0 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449)
Creating instance of bean ‘org.springframework.web.servlet.support.SessionFlashMapManager’
2018-11-10 15:23:26,776 DEBUG [DESKTOP-8S2E5H7 http-nio-9090-exec-1] Caller+0 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:485)
Finished creating instance of bean ‘org.springframework.web.servlet.support.SessionFlashMapManager’
2018-11-10 15:23:26,807 DEBUG [DESKTOP-8S2E5H7 http-nio-9090-exec-1] Caller+0 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251)
Returning cached instance of singleton bean ‘userController’
2018-11-10 15:23:26,829 ERROR [DESKTOP-8S2E5H7 http-nio-9090-exec-1] Caller+0 at com.yq.service.impl.UserServiceImpl.getById(UserServiceImpl.java:40)
error find user=2 by id=User(id=2, name=Tom2, mail=qq2@163.com, regDate=Sat Nov 10 15:23:18 CST 2018)
2018-11-10 15:23:26,830 WARN [DESKTOP-8S2E5H7 http-nio-9090-exec-1] Caller+0 at com.yq.service.impl.UserServiceImpl.getById(UserServiceImpl.java:41)
warn find user=2 by id=User(id=2, name=Tom2, mail=qq2@163.com, regDate=Sat Nov 10 15:23:18 CST 2018)
2018-11-10 15:23:26,830 INFO [DESKTOP-8S2E5H7 http-nio-9090-exec-1] Caller+0 at com.yq.service.impl.UserServiceImpl.getById(UserServiceImpl.java:42)
info find user=2 by id=User(id=2, name=Tom2, mail=qq2@163.com, regDate=Sat Nov 10 15:23:18 CST 2018)
2018-11-10 15:23:26,830 ERROR [DESKTOP-8S2E5H7 http-nio-9090-exec-1] Caller+0 at com.yq.controller.UserController.getUser(UserController.java:37)
error rest get user=User(id=2, name=Tom2, mail=qq2@163.com, regDate=Sat Nov 10 15:23:18 CST 2018) by id=2
2018-11-10 15:23:26,830 WARN [DESKTOP-8S2E5H7 http-nio-9090-exec-1] Caller+0 at com.yq.controller.UserController.getUser(UserController.java:38)
warn rest get user=User(id=2, name=Tom2, mail=qq2@163.com, regDate=Sat Nov 10 15:23:18 CST 2018) by id=2
2018-11-10 15:23:26,830 INFO [DESKTOP-8S2E5H7 http-nio-9090-exec-1] Caller+0 at com.yq.controller.UserController.getUser(UserController.java:39)
info rest get user=User(id=2, name=Tom2, mail=qq2@163.com, regDate=Sat Nov 10 15:23:18 CST 2018) by id=2
2018-11-10 15:23:26,830 DEBUG [DESKTOP-8S2E5H7 http-nio-9090-exec-1] Caller+0 at com.yq.controller.UserController.getUser(UserController.java:40)
debug rest get user=User(id=2, name=Tom2, mail=qq2@163.com, regDate=Sat Nov 10 15:23:18 CST 2018) by id=2
2018-11-10 15:23:26,830 TRACE [DESKTOP-8S2E5H7 http-nio-9090-exec-1] Caller+0 at com.yq.controller.UserController.getUser(UserController.java:41)
trace rest get user=User(id=2, name=Tom2, mail=qq2@163.com, regDate=Sat Nov 10 15:23:18 CST 2018) by id=2
error1.txt内容为
2018-11-10 15:23:20,428 ERROR [DESKTOP-8S2E5H7 restartedMain] Caller+0 at com.yq.LogbackApplication.main(LogbackApplication.java:19)
error LogbackApplication Start done.
控制台的内容为
Starting application com.yq.LogbackApplication with URLs [file:/D:/E/workspaceGitub/springboot/LogbackDemo/target/classes/]
2018-11-10 15:23:20,428 INFO [DESKTOP-8S2E5H7 restartedMain] Caller+0 at org.springframework.boot.StartupInfoLogger.logStarted(StartupInfoLogger.java:57)
Started LogbackApplication in 6.774 seconds (JVM running for 7.439)
2018-11-10 15:23:20,428 ERROR [DESKTOP-8S2E5H7 restartedMain] Caller+0 at com.yq.LogbackApplication.main(LogbackApplication.java:19)
error LogbackApplication Start done.
2018-11-10 15:23:20,428 WARN [DESKTOP-8S2E5H7 restartedMain] Caller+0 at com.yq.LogbackApplication.main(LogbackApplication.java:20)
warn LogbackApplication Start done.
2018-11-10 15:23:20,428 INFO [DESKTOP-8S2E5H7 restartedMain] Caller+0 at com.yq.LogbackApplication.main(LogbackApplication.java:21)
info LogbackApplication Start done.
2018-11-10 15:23:20,428 DEBUG [DESKTOP-8S2E5H7 restartedMain] Caller+0 at com.yq.LogbackApplication.main(LogbackApplication.java:22)
debug LogbackApplication Start done.
2018-11-10 15:23:26,749 DEBUG [DESKTOP-8S2E5H7 http-nio-9090-exec-1] Caller+0 at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:149)
Initializing servlet ‘dispatcherServlet’
作者:russle
来源:CSDN
原文:https://blog.csdn.net/russle/article/details/83932651
版权声明:本文为博主原创文章,转载请附上博文链接!