Spring Boot对全部内部日志记录使用了Commons Logging,可是底层日志实现是开放的。能够为 Java Util日志、Log4J2和Logback。对于每种日志都预先配置为使用控制台输出和可选的文件输出。默认为Logbackweb
经过将相应的库添加到classpath能够激活各类日志系统,而后在classpath根目录下提供合适的配置文件能够进一步定制日志系统,配置文件也能够经过Spring Environment的logging.config属性指定。spring
如下文件会根据你选择的日志系统进行加载:api
日志系统 | 定制配置 |
---|---|
Logback | logback-spring.xml ,logback-spring.groovy ,logback.xml 或logback.groovy |
Log4j | log4j.properties 或log4j.xml |
Log4j2 | log4j2-spring.xml 或log4j2.xml |
JDK (Java Util Logging) | logging.properties |
注 若是可能的话,建议你使用-spring
变种形式定义日志配置(例如,使用logback-spring.xml
而不是logback.xml
)。若是你使用标准的配置路径,Spring可能不可以彻底控制日志初始化。tomcat
注 Java Util Logging从可执行jar运行时会致使一些已知的类加载问题,咱们建议尽量不使用它。app
如下是从Spring Envrionment
转换为System properties的一些有助于定制的配置属性:ide
Spring Environment | System Property | Comments |
---|---|---|
logging.exception-conversion-word |
LOG_EXCEPTION_CONVERSION_WORD |
记录异常使用的关键字 |
logging.file |
LOG_FILE |
若是指定就会在默认的日志配置中使用 |
logging.path |
LOG_PATH |
若是指定就会在默认的日志配置中使用 |
logging.pattern.console |
CONSOLE_LOG_PATTERN |
日志输出到控制台(stdout)时使用的模式(只支持默认的logback设置) |
logging.pattern.file |
FILE_LOG_PATTERN |
日志输出到文件时使用的模式(若是LOG_FILE启用,只支持默认的logback设置) |
logging.pattern.level |
LOG_LEVEL_PATTERN |
用来渲染日志级别的格式(默认%5p ,只支持默认的logback设置) |
PID |
PID |
当前的处理进程(process)ID(可以找到,且尚未用做OS环境变量) |
全部支持的日志系统在解析配置文件时都能获取系统属性的值,具体能够参考spring-boot.jar
中的默认配置。spring-boot
一份史诗级配置idea
<Configuration status="INFO" monitorInterval="30"> <Properties> <Property name="logpath">/home/logs/log/dev</Property> </Properties> <Appenders> <Console name="Console" target="SYSTEM_OUT"> <PatternLayout pattern="[%d][%-5p][%t] %m (%F:%L)%n" /> </Console> <RollingFile name="debug" fileName="${logpath}/debug/erp_debug.log" filePattern="${logpath}/debug/erp_debug_%d{yyyy-MM-dd}-%i.log"> <Filters> <ThresholdFilter level="info" onMatch="DENY" onMismatch="NEUTRAL"/> <ThresholdFilter level="debug" onMatch="ACCEPT" onMismatch="DENY"/> </Filters> <PatternLayout pattern="[%d][%-5p][%t] %m (%F:%L)%n" /> <Policies> <TimeBasedTriggeringPolicy interval="24" modulate="true"/> <SizeBasedTriggeringPolicy size="50 MB"/>\ </Policies> <DefaultRolloverStrategy max="30"> <Delete basePath="${logpath}/debug" maxDepth="1"> <IfFileName glob="erp_debug_*.log"/> <IfLastModified age="15d"/> </Delete> </DefaultRolloverStrategy> </RollingFile> <RollingFile name="info" fileName="${logpath}/info/erp_info.log" filePattern="${logpath}/info/erp_info_%d{yyyy-MM-dd}-%i.log"> <Filters> <ThresholdFilter level="warn" onMatch="DENY" onMismatch="NEUTRAL"/> <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/> </Filters> <PatternLayout pattern="[%d][%-5p][%t] %m (%F:%L)%n" /> <Policies> <TimeBasedTriggeringPolicy interval="24" modulate="true"/> <SizeBasedTriggeringPolicy size="50 MB"/>\ </Policies> <DefaultRolloverStrategy max="30"> <Delete basePath="${logpath}/info" maxDepth="1"> <IfFileName glob="erp_info_*.log"/> <IfLastModified age="15d"/> </Delete> </DefaultRolloverStrategy> </RollingFile> <RollingFile name="warn" fileName="${logpath}/warn/erp_warn.log" filePattern="${logpath}/warn/erp_warn_%d{yyyy-MM-dd}-%i.log"> <Filters> <ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL"/> <ThresholdFilter level="warn" onMatch="ACCEPT" onMismatch="DENY"/> </Filters> <PatternLayout pattern="[%d][%-5p][%t] %m (%F:%L)%n" /> <Policies> <TimeBasedTriggeringPolicy interval="24" modulate="true"/> <SizeBasedTriggeringPolicy size="50 MB"/>\ </Policies> <DefaultRolloverStrategy max="30"> <Delete basePath="${logpath}/warn" maxDepth="1"> <IfFileName glob="erp_warn_*.log"/> <IfLastModified age="15d"/> </Delete> </DefaultRolloverStrategy> </RollingFile> <RollingFile name="error" fileName="${logpath}/error/erp_error.log" filePattern="${logpath}/error/erp_error_%d{yyyy-MM-dd}-%i.log"> <Filters> <ThresholdFilter level="fatal" onMatch="DENY" onMismatch="NEUTRAL"/> <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/> </Filters> <PatternLayout pattern="[%d][%-5p][%t] %m (%F:%L)%n" /> <Policies> <TimeBasedTriggeringPolicy interval="24" modulate="true"/> <SizeBasedTriggeringPolicy size="50 MB"/>\ </Policies> <DefaultRolloverStrategy max="30"> <Delete basePath="${logpath}/error" maxDepth="1"> <IfFileName glob="erp_error_*.log"/> <IfLastModified age="15d"/> </Delete> </DefaultRolloverStrategy> </RollingFile> </Appenders> <Loggers> <Root level="debug"> <AppenderRef ref="Console"/> <AppenderRef ref="debug"/> <AppenderRef ref="info"/> <AppenderRef ref="warn"/> <AppenderRef ref="error"/> </Root> </Loggers> </Configuration>
各个文件输出到不一样级别的目录spa
设置最大保存时间为15天debug
每一个文件最大50M
在properties文件添加以下配置:
server.tomcat.basedir=/home/logs/log-api/tomcat-logs server.tomcat.accesslog.enabled=true server.tomcat.accesslog.pattern=%t %a "%r" %s %D (%D ms) server.use-forward-headers=true
使用lombok的@Slf4j 注解,省去配置声明log的繁琐,提升开发效率。
Grep Console 自定义设置控制台输出颜色,这样控制台就能比较明显的看到警告或者错误的信息,方便查找问题
视频地址:https://space.bilibili.com/313762729/#/