log4j2分層輸出日誌

aixw發表於2018-12-28

在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);
....

相關文章