log4j2 hierarchical output log

In the development process of java mvc framework, we often divide the code into levels like controller (control layer), service (business layer), rpc (remote interface call layer), dao (data layer). If we print all the logs of all levels into one file, one is that the single log file is too large, and it is not convenient to view, so we consider using log4j2 according to the following Generate corresponding log files at the same level:

1. maven configuration

<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 configuration file

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="30">
    <Properties>
        <!-- File output format -->
        <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>
        <!--Filtering information-->
        <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. Output log tool class

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

public class LogUtil {

    /**
     * RPC Layer logging
     */
    public static final Logger RPC = LoggerFactory.getLogger("RPC");

    /**
     * Service Business level logging
     */
    public static final Logger SERVICE = LoggerFactory.getLogger("SERVICE");

    /**
     * Controller Business level logging
     */
    public static final Logger CONTROLLER = LoggerFactory.getLogger("CONTROLLER");

}

4. Output log

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

Keywords: Java log4j Maven Apache

Added by curt3006 on Sun, 08 Dec 2019 01:48:22 +0200