Java应用中,日志通常分为如下5个级别:web
Spring Boot使用Apache的Commons Logging做为内部的日志框架,其仅仅是一个日志接口,在实际应用中须要为该接口来指定相应的日志实现。spring
SpringBt默认的日志实现是Java Util Logging,是JDK自带的日志包,此外SpringBt固然也支持Log4J、Logback这类很流行的日志实现。apache
统一将上面这些日志实现统称为日志框架编程
下面咱们来实践一下!c#
注: 本文首发于 My 公众号 CodeSheep ,可 长按 或 扫描 下面的 当心心 来订阅 ↓ ↓ ↓
logging.level.root=INFO
package com.hansonwang99.controller; import com.hansonwang99.K8sresctrlApplication; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/testlogging") public class LoggingTestController { private static Logger logger = LoggerFactory.getLogger(K8sresctrlApplication.class); @GetMapping("/hello") public String hello() { logger.info("test logging..."); return "hello"; } }
因为将日志等级设置为INFO,所以包含INFO及以上级别的日志信息都会打印出来springboot
这里能够看出,不少大部分的INFO日志均来自于SpringBt框架自己,若是咱们想屏蔽它们,能够将日志级别统一先所有设置为ERROR,这样框架自身的INFO信息不会被打印。而后再将应用中特定的包设置为DEBUG级别的日志,这样就能够只看到所关心的包中的DEBUG及以上级别的日志了。服务器
application.yml中改配置app
logging: level: root: error com.hansonwang99.controller: debug
很明显,将root日志级别设置为ERROR,而后再将com.hansonwang99.controller
包的日志级别设为DEBUG,此即:即先禁止全部再容许个别的 设置方法框架
package com.hansonwang99.controller; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/testlogging") public class LoggingTestController { private Logger logger = LoggerFactory.getLogger(this.getClass()); @GetMapping("/hello") public String hello() { logger.info("test logging..."); return "hello"; } }
可见框架自身的INFO级别日志所有藏匿,而指定包中的日志按级别顺利地打印出来spring-boot
logging: level: root: error com.hansonwang99.controller: debug file: ${user.home}/logs/hello.log
使用Spring Boot Logging,咱们发现虽然日志已输出到文件中,但控制台中依然会打印一份,发现用org.slf4j.Logger
是没法解决这个问题的
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency>
log4j2.xml
文件,内容以下:<?xml version="1.0" encoding="UTF-8"?> <configuration> <appenders> <File name="file" fileName="${sys:user.home}/logs/hello2.log"> <PatternLayout pattern="%d{HH:mm:ss,SSS} %p %c (%L) - %m%n"/> </File> </appenders> <loggers> <root level="ERROR"> <appender-ref ref="file"/> </root> <logger name="com.hansonwang99.controller" level="DEBUG" /> </loggers> </configuration>
运行程序发现控制台没有日志输出,而hello2.log文件中有内容,这符合咱们的预期:
并且日志格式和pattern="%d{HH:mm:ss,SSS} %p %c (%L) - %m%n"
格式中定义的相匹配
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency>
<?xml version="1.0" encoding="UTF-8"?> <configuration status="warn"> <properties> <Property name="app_name">springboot-web</Property> <Property name="log_path">logs/${app_name}</Property> </properties> <appenders> <console name="Console" target="SYSTEM_OUT"> <PatternLayout pattern="[%d][%t][%p][%l] %m%n" /> </console> <RollingFile name="RollingFileInfo" fileName="${log_path}/info.log" filePattern="${log_path}/$${date:yyyy-MM}/info-%d{yyyy-MM-dd}-%i.log.gz"> <Filters> <ThresholdFilter level="INFO" /> <ThresholdFilter level="WARN" onMatch="DENY" onMismatch="NEUTRAL" /> </Filters> <PatternLayout pattern="[%d][%t][%p][%c:%L] %m%n" /> <Policies> <!-- 归档天天的文件 --> <TimeBasedTriggeringPolicy interval="1" modulate="true" /> <!-- 限制单个文件大小 --> <SizeBasedTriggeringPolicy size="2 MB" /> </Policies> <!-- 限制天天文件个数 --> <DefaultRolloverStrategy compressionLevel="0" max="10"/> </RollingFile> <RollingFile name="RollingFileWarn" fileName="${log_path}/warn.log" filePattern="${log_path}/$${date:yyyy-MM}/warn-%d{yyyy-MM-dd}-%i.log.gz"> <Filters> <ThresholdFilter level="WARN" /> <ThresholdFilter level="ERROR" onMatch="DENY" onMismatch="NEUTRAL" /> </Filters> <PatternLayout pattern="[%d][%t][%p][%c:%L] %m%n" /> <Policies> <!-- 归档天天的文件 --> <TimeBasedTriggeringPolicy interval="1" modulate="true" /> <!-- 限制单个文件大小 --> <SizeBasedTriggeringPolicy size="2 MB" /> </Policies> <!-- 限制天天文件个数 --> <DefaultRolloverStrategy compressionLevel="0" max="10"/> </RollingFile> <RollingFile name="RollingFileError" fileName="${log_path}/error.log" filePattern="${log_path}/$${date:yyyy-MM}/error-%d{yyyy-MM-dd}-%i.log.gz"> <ThresholdFilter level="ERROR" /> <PatternLayout pattern="[%d][%t][%p][%c:%L] %m%n" /> <Policies> <!-- 归档天天的文件 --> <TimeBasedTriggeringPolicy interval="1" modulate="true" /> <!-- 限制单个文件大小 --> <SizeBasedTriggeringPolicy size="2 MB" /> </Policies> <!-- 限制天天文件个数 --> <DefaultRolloverStrategy compressionLevel="0" max="10"/> </RollingFile> </appenders> <loggers> <root level="info"> <appender-ref ref="Console" /> <appender-ref ref="RollingFileInfo" /> <appender-ref ref="RollingFileWarn" /> <appender-ref ref="RollingFileError" /> </root> </loggers> </configuration>
package com.hansonwang99.controller; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/testlogging") public class LoggingTestController { private final Logger logger = LogManager.getLogger(this.getClass()); @GetMapping("/hello") public String hello() { for(int i=0;i<10_0000;i++){ logger.info("info execute index method"); logger.warn("warn execute index method"); logger.error("error execute index method"); } return "My First SpringBoot Application"; } }
日志会根据不一样的级别存储在不一样的文件,当日志文件大小超过2M之后会分多个文件压缩存储,生产环境的日志文件大小建议调整为20-50MB。
做者更多的SpringBt实践文章在此:
若是有兴趣,也能够抽点时间看看做者一些关于容器化、微服务化方面的文章: