0、前言
日志记录对系统来说必不可少,spring boot中常用的日志组件有log4j、logback、log4j2,其中logback是spring boot默认的,已自带;选用log4j2就可以,其他的不必去关注;
一、集成log4j2日志组件
1、pom依赖中去除默认的logback日志框架:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!-- 7-1 去掉自带的日志依赖--> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency>
2、添加依赖
<!-- 7-2、集成log4j2 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency> <!-- 额外添加disruptor依赖,用于解决log4j2日志版本较低报错问题--> <dependency> <groupId>com.lmax</groupId> <artifactId>disruptor</artifactId> <version>3.4.2</version> </dependency>
3、增加日志配置文件log4j2-dev.xml
<?xml version="1.0" encoding="UTF-8"?> <!--设置log4j2的自身log级别为warn--> <!--日志级别以及优先级排序: OFF > FATAL > ERROR > WARN > INFO > DEBUG > TRACE > ALL --> <!--Configuration后面的status,这个用于设置log4j2自身内部的信息输出,可以不设置, 当设置成trace时,你会看到log4j2内部各种详细输出--> <!--monitorInterval:Log4j能够自动检测修改配置 文件和重新配置本身,设置间隔秒数--> <Configuration status="INFO" monitorInterval="30"> <Properties> <!-- 输出路径 --> <Property name="logpath">/Log4j/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"/> <!-- 每个文件最大50M --> <SizeBasedTriggeringPolicy size="50 MB"/>\ </Policies> <DefaultRolloverStrategy max="30"> <Delete basePath="${logpath}/error" maxDepth="1"> <IfFileName glob="erp_error_*.log"/> <!-- 设置最大保存时间为15天--> <IfLastModified age="15d"/> </Delete> </DefaultRolloverStrategy> </RollingFile> </Appenders> <!--切换输出级别--> <Loggers> <Root level="info"> <AppenderRef ref="Console"/> <AppenderRef ref="debug"/> <AppenderRef ref="info"/> <AppenderRef ref="warn"/> <AppenderRef ref="error"/> </Root> </Loggers> </Configuration>
配置文件中增加配置:
#################### 7 log4j2 ################### logging.config=classpath:log4j2-dev.xml logging.level.org.springframework=INFO
4、使用
private static final Logger logger = LoggerFactory.getLogger(UserController.class); logger.info("未知异常!原因是:info"); logger.error("未知异常!原因是:error"); logger.warn("未知异常!原因是:warn");
简单用法,完毕~
微服务中,服务众多,分布在各个服务器中,我们一般不这样使用,我们一般使用ELK来对日志进行统一收集和分析处理,ELK后面再说