詳解log4j2(下) - Async/MongoDB/Flume Appender 按日誌級別區分文件輸出

1. 按日誌級別區分文件輸出

有些人習慣按日誌信息級別輸出到不一樣名稱的文件中,如info.log,error.log,warn.log等,在log4j2中可經過配置Filters來實現。java

假定需求是把INFO及如下級別的信息輸出到info.log,WARN和ERROR級別的信息輸出到error.log,FATAL級別輸出到fatal.log,配置文件以下:sql

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="300">
    <properties>
        <property name="LOG_HOME">D:/logs</property>
    </properties>
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>

        <RollingRandomAccessFile name="InfoFile"
                                 fileName="${LOG_HOME}/info.log"
                                 filePattern="${LOG_HOME}/$${date:yyyy-MM}/info-%d{yyyy-MM-dd}-%i.log">
            <Filters>
                <ThresholdFilter level="warn" onMatch="DENY" onMismatch="NEUTRAL"/>
                <ThresholdFilter level="trace" onMatch="ACCEPT" onMismatch="DENY"/>
            </Filters>
            <PatternLayout pattern="%date{yyyy-MM-dd HH:mm:ss.SSS} %level [%thread][%file:%line] - %msg%n"/>
            <Policies>
                <TimeBasedTriggeringPolicy/>
                <SizeBasedTriggeringPolicy size="10 MB"/>
            </Policies>
            <DefaultRolloverStrategy max="20"/>
        </RollingRandomAccessFile>

        <RollingRandomAccessFile name="ErrorFile"
                                 fileName="${LOG_HOME}/error.log"
                                 filePattern="${LOG_HOME}/$${date:yyyy-MM}/error-%d{yyyy-MM-dd}-%i.log">
            <Filters>
                <ThresholdFilter level="fatal" onMatch="DENY" onMismatch="NEUTRAL"/>
                <ThresholdFilter level="warn" onMatch="ACCEPT" onMismatch="DENY"/>
            </Filters>
            <PatternLayout pattern="%date{yyyy-MM-dd HH:mm:ss.SSS} %level [%thread][%file:%line] - %msg%n"/>
            <Policies>
                <TimeBasedTriggeringPolicy/>
                <SizeBasedTriggeringPolicy size="10 MB"/>
            </Policies>
            <DefaultRolloverStrategy max="20"/>
        </RollingRandomAccessFile>

        <RollingRandomAccessFile name="FatalFile"
                                 fileName="${LOG_HOME}/fatal.log"
                                 filePattern="${LOG_HOME}/$${date:yyyy-MM}/fatal-%d{yyyy-MM-dd}-%i.log">
            <Filters>
                <ThresholdFilter level="fatal" onMatch="ACCEPT" onMismatch="DENY"/>
            </Filters>
            <PatternLayout pattern="%date{yyyy-MM-dd HH:mm:ss.SSS} %level [%thread][%file:%line] - %msg%n"/>
            <Policies>
                <TimeBasedTriggeringPolicy/>
                <SizeBasedTriggeringPolicy size="10 MB"/>
            </Policies>
            <DefaultRolloverStrategy max="20"/>
        </RollingRandomAccessFile>
    </Appenders>

    <Loggers>
        <Root level="trace">
            <AppenderRef ref="Console"/>
            <AppenderRef ref="InfoFile"/>
            <AppenderRef ref="ErrorFile"/>
            <AppenderRef ref="FatalFile"/>
        </Root>
    </Loggers>
</Configuration>

測試代碼:mongodb

public static void main(String[] args) {
    Logger logger = LogManager.getLogger(Client.class);
    logger.trace("trace level");
    logger.debug("debug level");
    logger.info("info level");
    logger.warn("warn level");
    logger.error("error level");
    logger.fatal("fatal level");
}

2 異步寫日誌

配置文件:apache

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="300">
    <properties>
        <property name="LOG_HOME">D:/logs</property>
        <property name="FILE_NAME">mylog</property>
    </properties>

    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>
        <RollingRandomAccessFile name="MyFile"
                                 fileName="${LOG_HOME}/${FILE_NAME}.log"
                                 filePattern="${LOG_HOME}/$${date:yyyy-MM}/${FILE_NAME}-%d{yyyy-MM-dd HH-mm}-%i.log">
            <PatternLayout
                    pattern="%date{yyyy-MM-dd HH:mm:ss.SSS} %level [%thread][%file:%line] - %msg%n"/>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1"/>
                <SizeBasedTriggeringPolicy size="10 MB"/>
            </Policies>
            <DefaultRolloverStrategy max="20"/>
        </RollingRandomAccessFile>
        <Async name="Async">
            <AppenderRef ref="MyFile"/>
        </Async>
    </Appenders>

    <Loggers>
        <Logger name="asynclog" level="trace" additivity="false">
            <AppenderRef ref="Async"/>
        </Logger>
        <Root level="error">
            <AppenderRef ref="Console"/>
        </Root>
    </Loggers>
</Configuration>

測試代碼:windows

public static void main(String[] args) {
    Logger logger = LogManager.getLogger("asynclog");
    logger.trace("trace level");
    logger.debug("debug level");
    logger.info("info level");
    logger.warn("warn level");
    logger.error("error level");
    logger.fatal("fatal level");
}

3 輸出到MongoDB

添加依賴:app

<dependency>  
    <groupId>org.apache.logging.log4j</groupId>  
    <artifactId>log4j-nosql</artifactId>  
    <version>2.5</version>
</dependency>  
<dependency>  
    <groupId>org.mongodb</groupId>  
    <artifactId>mongo-java-driver</artifactId>  
    <version>3.2.2</version>  
</dependency>

配置文件:dom

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="300">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>

        <NoSql name="databaseAppender">
            <MongoDb databaseName="test" collectionName="errorlog"
                     server="localhost" port="27017"/>
        </NoSql>
    </Appenders>

    <Loggers>
        <Logger name="mongolog" level="trace" additivity="false">
            <AppenderRef ref="databaseAppender"/>
        </Logger>
        <Root level="error">
            <AppenderRef ref="Console"/>
        </Root>
    </Loggers>
</Configuration>

4 輸出到Flume

Flume配置(flume-conf.properties)異步

agent1.sources=source1
agent1.sinks=sink1
agent1.channels=channel1

agent1.sources.source1.type=avro
agent1.sources.source1.channels=channel1
agent1.sources.source1.bind=0.0.0.0
agent1.sources.source1.port=41414

agent1.sinks.sink1.type=file_roll
agent1.sinks.sink1.sink.directory=D:/log
agent1.sinks.sink1.channel=channel1
agent1.sinks.sink1.sink.rollInterval=86400
agent1.sinks.sink1.sink.batchSize=100
agent1.sinks.sink1.sink.serializer=text
agent1.sinks.sink1.sink.serializer.appendNewline = false

agent1.channels.channel1.type=file
agent1.channels.channel1.checkpointDir=D:/log/checkpoint
agent1.channels.channel1.dataDirs=D:/log/data

啓動Flume(注:測試環境爲windows)nosql

flume-ng.cmd agent --conf ../conf/ --conf-file ../conf/flume-conf.properties -name agent1

添加依賴:async

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-flume-ng</artifactId>
    <version>2.5</version>
</dependency>

配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="300">
    <Appenders>
        <Flume name="eventLogger" compress="false">
            <Agent host="127.0.0.1" port="41414"/>
            <RFC5424Layout enterpriseNumber="18060" includeMDC="true" appName="MyApp"/>
        </Flume>
    </Appenders>
    <Loggers>
        <Root level="trace">
            <AppenderRef ref="eventLogger"/>
        </Root>
    </Loggers>
</Configuration>

 

ss

相關文章
相關標籤/搜索