涨姿势:Java 分业务、分级别实现自定义日志打印

时间:2023-12-12 20:21:20

自定义日志级别

通常的日志框架都有以下几个级别,从低到高TRACE,DEBUG,INFO,WARN,ERROR,FATAL.

默认情况,假如我们定义日志打印级别INFO,它会把大于等于INFO级别的日志信息打印出来.这样各个类别的日志都打印在一起不利于日志查看.

按照业务类型打印日志,我们希望info日志文件里只打印info信息,warn日志文件里只打印warn信息,error日志文件只打印error信息,不同的业务模块的日志打印到单独日志文件.

注:本篇使用的日志框架是log4j.实现日志文件只打印指定日志级别是通过Filters实现的,

如下:

    <!--告警信息打印-->
<RollingFile name="RollingFileWarnLog" fileName="log/warn.log"
filePattern="log/$${date:yyyy-MM}/warn-%d{yyyy-MM-dd-HH}-%i.log.gz">
<!--过滤掉其他level-->
<Filters>
<!--默认情况会打印大于等于当前级别的日志,因此,需要把error级别的过滤掉-->
<ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="warn" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters> <PatternLayout pattern="%d{yyyy-MM-dd 'at' HH:mm:ss z} %-5level %class{36} %L %M - %msg%xEx%n"/>
<Policies>
<TimeBasedTriggeringPolicy interval="8" modulate="true"/>
<SizeBasedTriggeringPolicy size="50 MB"/>
</Policies>
<DefaultRolloverStrategy max="100"/>
</RollingFile>

因此实现按业务类型打印日志也很简单,只需要自定义日志级别,然后把大于等于它级别的日志过滤掉就可以了.

下面是个样例配置

<?xml version="1.0" encoding="utf-8"?>
<configuration >
<properties>
<!-- 文件输出格式 -->
<property name="PATTERN">%d{yyyy-MM-dd HH:mm:ss.SSS} |-%-5level [%thread] %c [%L] -| %msg%n</property>
</properties> <!-- 先定义所有的appender -->
<!--appender定义日志的输出位置,格式(即patternLayout),日志等级等-->
<!--loggers定义哪些类使用哪些appender,并可以设置日志级别-->
<CustomLevels>
<!--自定义日志级别:业务1-->
<CustomLevel name="BU_ONE_INFO" intLevel="490" />
<!--自定义日志级别:业务2-->  
<CustomLevel name="BU_TWO_INFO" intLevel="480"/>
</CustomLevels>
<appenders>
<!--这个输出控制台的配置:控制台只输出level及以上级别的信息(onMatch),其他的直接拒绝(onMismatch) -->
<Console name="Console" target="system_out">
<ThresholdFilter level="debug" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n" />
</Console>
<!--告警信息打印-->
<RollingFile name="RollingFileWarnLog" fileName="log/warn.log"
filePattern="log/$${date:yyyy-MM}/warn-%d{yyyy-MM-dd-HH}-%i.log.gz">
<!--过滤掉其他level-->
<Filters>
<ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="warn" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters> <PatternLayout pattern="%d{yyyy-MM-dd 'at' HH:mm:ss z} %-5level %class{36} %L %M - %msg%xEx%n"/>
<Policies>
<TimeBasedTriggeringPolicy interval="8" modulate="true"/>
<SizeBasedTriggeringPolicy size="50 MB"/>
</Policies>
<DefaultRolloverStrategy max="100"/>
</RollingFile>
<!--普通信息打印-->
<RollingFile name="RollingFileInfoLog" fileName="log/info.log"
filePattern="log/$${date:yyyy-MM}/info-%d{yyyy-MM-dd-HH}-%i.log.gz">
<Filters>
<ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="warn" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
<PatternLayout pattern="%d{yyyy-MM-dd 'at' HH:mm:ss z} %-5level %class{36} %L %M - %msg%xEx%n"/>
<Policies>
<TimeBasedTriggeringPolicy interval="8" modulate="true"/>
<SizeBasedTriggeringPolicy size="50 MB"/>
</Policies>
<DefaultRolloverStrategy max="100"/>
</RollingFile
<!--错误信息打印-->
<RollingFile name="RollingFileErrorLog" fileName="log/error.log"
filePattern="log/$${date:yyyy-MM}/error-%d{yyyy-MM-dd-HH}-%i.log.gz">
<ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="%d{yyyy-MM-dd 'at' HH:mm:ss z} %-5level %class{36} %L %M - %msg%xEx%n"/>
<Policies>
<TimeBasedTriggeringPolicy interval="8" modulate="true"/>
<SizeBasedTriggeringPolicy size="50 MB"/>
</Policies>
<DefaultRolloverStrategy max="50"/>
</RollingFile> <RollingFile name="RollingFileBuOneLog" fileName="log/buoneinfo.log"
filePattern="log/$${date:yyyy-MM}/buoneinfo-%d{yyyy-MM-dd-HH}-%i.log.gz">
<Filters>
<ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="warn" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="info" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="BU_TWO_INFO" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="BU_ONE_INFO" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
<PatternLayout pattern="%d{yyyy-MM-dd 'at' HH:mm:ss z} %-5level %class{36} %L %M - %msg%xEx%n"/>
<Policies>
<TimeBasedTriggeringPolicy interval="8" modulate="true"/>
<SizeBasedTriggeringPolicy size="50 MB"/>
</Policies>
<DefaultRolloverStrategy max="50"/>
</RollingFile> <RollingFile name="RollingFileBuTwoLog" fileName="log/butwoinfo.log"
filePattern="log/$${date:yyyy-MM}/butwoinfo-%d{yyyy-MM-dd-HH}-%i.log.gz">
<Filters>
<ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="warn" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="info" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="BU_TWO_INFO" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
<PatternLayout pattern="%d{yyyy-MM-dd 'at' HH:mm:ss z} %-5level %class{36} %L %M - %msg%xEx%n"/>
<Policies>
<TimeBasedTriggeringPolicy interval="8" modulate="true"/>
<SizeBasedTriggeringPolicy size="50 MB"/>
</Policies>
<DefaultRolloverStrategy max="50"/>
</RollingFile> </appenders> <!-- 然后定义logger,只有定义了logger并引入的appender,appender才会生效 -->
<loggers> <root level="debug">
<!--<appender-ref ref="connectionFactoryForLog" />-->
<appender-ref ref="RollingFileInfoLog" />
<appender-ref ref="RollingFileWarnLog" />
<appender-ref ref="RollingFileErrorLog" />
<appender-ref ref="RollingFileBuTwoLog" />
<appender-ref ref="RollingFileBuOneLog" />
<!--<appender-ref ref="RollingFileTcpPackageLog" />-->
<appender-ref ref="Console" />
</root>
</loggers> </configuration>

以上就是配置文件

代码实现如下:

/**
* Desc: 自定义日志级别打印,测试类
* @author l2h
*/
@RestController
public class LogController {
private final Logger logger = LogManager.getLogger(LogController.class);
private final Level BU_TWO_INFO = Level.forName("BU_TWO_INFO",480);
private final Level BU_ONE_INFO = Level.forName("BU_ONE_INFO",490);
@GetMapping("/log")
public String log(@RequestParam("msg")String msg){
logger.log(BU_ONE_INFO,msg);
logger.log(BU_TWO_INFO,msg);
logger.info(msg);
logger.error(msg);
logger.warn(msg);
return "success";
}
}

调试结果如下:

涨姿势:Java 分业务、分级别实现自定义日志打印

注意

maven 引入的log4j stater与springboot 框架因为的有冲突,因此需要把springboot exclusion掉,否则会发现没有打印到日志文件中.

样例代码的maven配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>simos</groupId>
<artifactId>springboot-quick-start</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<artifactId>spring-boot-starter-logging</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
</dependencies>
</project>

以上配置实现分业务,分级别自定义日志打印.

talk is easy , show me the code.