log4j2分层输出日志

时间:2022-07-23 21:49:33

在java mvc框架开发过程中,我们经常的将代码分为类似controller(控制层)、service(业务层)、rpc(远程接口调用层)、dao(数据层)等层级,如果将所有层级的日志全部都打到一个文件,一个是导致单个日志文件过大,另外不方便查看,所以下面考虑用log4j2根据不同层级生成对应的log文件:

1. maven配置

<dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-1.2-api</artifactId>
            <version>2.10.0</version>
        </dependency>           

2. log4j2配置文件

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="30">
    <Properties>
        <!-- 文件输出格式 -->
        <Property name="pattern">%d{yyyy-MM-dd HH:mm:ss.SSS} |-%-5level [LOGID:%X{logId}] [%thread] %C#%M [%L] -| %msg%n</Property>
        <Property name="filePath">/export/Logs/Domains/myapp</Property>
    </Properties>

    <Appenders>
        <Console name="console" target="system_out">
            <PatternLayout pattern="${pattern}" />
        </Console>
        <RollingRandomAccessFile name="rpcFile" fileName="${filePath}/rpc.log" filePattern="${filePath}/rpc-%d{yyyy-MM-dd}.log">
            <PatternLayout pattern="${pattern}"/>
            <Policies>
                <TimeBasedTriggeringPolicy modulate="true" interval="1"/>
            </Policies>
        </RollingRandomAccessFile>

        <RollingRandomAccessFile name="serviceFile" fileName="${filePath}/service.log" filePattern="${filePath}/service-%d{yyyy-MM-dd}.log">
            <PatternLayout pattern="${pattern}"/>
            <Policies>
                <TimeBasedTriggeringPolicy modulate="true" interval="1"/>
            </Policies>
        </RollingRandomAccessFile>
        <RollingRandomAccessFile name="controllerFile" fileName="${filePath}/controller.log" filePattern="${filePath}/controller-%d{yyyy-MM-dd}.log">
            <PatternLayout pattern="${pattern}"/>
            <Policies>
                <TimeBasedTriggeringPolicy modulate="true" interval="1"/>
            </Policies>
        </RollingRandomAccessFile>

    </Appenders>

    <Loggers>
        <!--过滤信息-->
        <logger name="org.springframework" level="WARN"></logger>
        <logger name="RPC" level="INFO" >
            <AppenderRef ref="rpcFile" />
        </logger>
        <logger name="SERVICE" level="INFO" >
            <AppenderRef ref="serviceFile" />
        </logger>
        <logger name="CONTROLLER" level="INFO" >
            <AppenderRef ref="controllerFile" />
        </logger>
        <Root level="INFO">
            <AppenderRef ref="console" />
            <AppenderRef ref="exceptionFile" />
        </Root>
    </Loggers>

</Configuration>

3. 输出日志工具类

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LogUtil {

    /**
     * RPC层日志记录
     */
    public static final Logger RPC = LoggerFactory.getLogger("RPC");

    /**
     * Service业务层日志记录
     */
    public static final Logger SERVICE = LoggerFactory.getLogger("SERVICE");

    /**
     * Controller业务层日志记录
     */
    public static final Logger CONTROLLER = LoggerFactory.getLogger("CONTROLLER");

}

4. 输出日志

LogUtil.CONTROLLER.error("error message={}", e);
LogUtil.SERVICE.warn("warn message={}", msg);
....