Spring Boot 日志管理
程序员文章站
2022-04-20 09:01:12
...
Spring Boot 日志管理 TRACE < DEBUG < INFO < WARN < ERROR 门面抽象框架.
Spring Boot在所有内部日志中使用Commons Logging,但是默认配置也提供了对常用日志的支持,如:Java Util Logging,Log4J, Log4J2和Logback。
1、application.properties
只对默认的日志系统Logback起作用的
logging.level.root=WARN
logging.level.org.springframework.web=DEBUG
logging.file=/log/log/my.log
logging.pattern.console=%d{yyyy/MM/dd-HH:mm:ss} [%thread] %-5level %logger- %msg%n
logging.pattern.file=%d{yyyy/MM/dd-HH:mm} [%thread] %-5level %logger- %msg%n
自定义日志配置
Logback:logback-spring.xml, logback-spring.groovy, logback.xml, logback.groovy
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoder 默认配置为PatternLayoutEncoder -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<appender name="WARN_OUT" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>/var/www/logs/warn.log</file>
<!-- 日志格式 -->
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%class:%line] %-5level %logger - %msg%n</pattern>
</encoder>
<!-- 日志级别过滤器 -->
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<!-- 过滤的级别 -->
<level>WARN</level>
<!-- 匹配时的操作:接收(记录) -->
<onMatch>ACCEPT</onMatch>
<!-- 不匹配时的操作:拒绝(不记录) -->
<onMismatch>DENY</onMismatch>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 按天回滚 daily -->
<fileNamePattern>/var/www/logs/warn-%d{yyyy-MM-dd HH:mm:ss}.log</fileNamePattern>
<!-- 最大保存时间:30天-->
<maxHistory>30</maxHistory>
</rollingPolicy>
</appender>
<logger name="logback"/>
<logger name="logback.LogbackDemo" level="INFO" additivity="false">
<appender-ref ref="WARN_OUT"/>
</logger>
<root level="ERROR">
<appender-ref ref="STDOUT" />
</root>
</configuration>
注意:
logback将控制logback包下的所有类的日志的打印,但是并没用设置打印级别,所以继承他的上级<root>的日志级别“DEBUG”;
没有设置addtivity,默认为true,将此loger的打印信息向上级传递;
没有设置appender,此loger本身不打印任何信息;
2、Log4j:
log4j-spring.properties, log4j-spring.xml, log4j.properties, log4j.xml
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
<version>1.3.8.RELEASE</version>
</dependency>
[<!ENTITY log_home "./collectLog">]
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<!-- Appenders -->
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p: %c - %m%n" />
</layout>
</appender>
<appender name="minaLog" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="&log_home;/mina.log"/>
<param name="Append" value="true"/>
<param name="MaxFileSize" value="10485760"/>
<param name="MaxBackupIndex" value="10"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n"/>
</layout>
</appender>
<appender name="otherLog" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="&log_home;/other.log"/>
<param name="Append" value="true"/>
<param name="MaxFileSize" value="10485760"/>
<param name="MaxBackupIndex" value="10"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n"/>
</layout>
</appender>
<logger name="com.cpit.collect.mina" additivity="false">
<level value="info" />
<appender-ref ref="minaLog" />
</logger>
<root>
<priority value="warn"/>
<appender-ref ref="otherLog" />
</root>
</log4j:configuration>
3、Log4j2:log4j2-spring.xml, log4j2.xml
4、Java Util Logging:logging.properties
上一篇: Spring Boot 日志管理
下一篇: Word2003为整篇文档指定数字样式
推荐阅读
-
SpringBoot 源码解析 (三)----- Spring Boot 精髓:启动时初始化数据
-
idea创建一个入门Spring Boot项目(controller层)使用Moven代码管理
-
Spring Boot引入依赖包Druid
-
Spring Boot 开发个人博客--后台登录
-
将spring boot部署到tomcat上
-
Spring Boot面试题(2020最新版)
-
MySQL 日志管理详解
-
解决spring boot1.5以上版本@ConfigurationProperties提示“Spring Boot Configuration Annotation Processor not.."
-
什么是Spring Boot
-
Spring-Boot 集成Solr客户端的详细步骤