log4j2异步日志模板

该日志模板生成的日志如下:当天日志dwh.ERROR,dwh.INFO,dwh.WARN。往期的所示:.log文件

log4j2异步日志模板,可以直接使用,异步日志需要依赖disruptor 框架,需要添加maven如下配置:

<dependency>
    <groupId>com.lmax</groupId>
    <artifactId>disruptor</artifactId>
    <version>3.4.2</version>
</dependency>

RollingRandomAccessFile 相比RollingFileAppender有很大的性能提升。

log4j2.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!-- monitorInterval:指定log4j自动重新配置的监测间隔时间 -->
<Configuration status="INFO" monitorInterval="10">
    <Properties>
        <Property name="layoutpattern">%d %5p %30.30c{1.} : %m %ex%n</Property>
    </Properties>
    <Appenders>

        <Console name="STDOUT" target="SYSTEM_OUT">
            <PatternLayout pattern="${layoutpattern}"/>
        </Console>

        <RollingRandomAccessFile name="Error"
                                 fileName="/data/appLog/Logs/zkDemo.ERROR"
                                 filePattern="/data/appLog/Logs/zkDemo.ERROR.%d{yyyy-MM-dd}.log">
            <PatternLayout pattern="${layoutpattern}"/>
            <ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY"/>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
            </Policies>
            <DefaultRolloverStrategy>
                <Delete basePath="/data/appLog/Logs/" maxDepth="1">
                    <IfFileName glob="zkDemo.ERROR.*.log" />
                    <IfLastModified age="10d" />
                </Delete>
            </DefaultRolloverStrategy>
        </RollingRandomAccessFile>

        <RollingRandomAccessFile name="Warn"
                                 fileName="/data/appLog/Logs/zkDemo.WARN"
                                 filePattern="/data/appLog/Logs/zkDemo.WARN.%d{yyyy-MM-dd}.log">
            <PatternLayout pattern="${layoutpattern}"/>
            <ThresholdFilter level="WARN" onMatch="ACCEPT" onMismatch="DENY"/>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
            </Policies>
            <DefaultRolloverStrategy>
                <Delete basePath="/data/appLog/Logs/" maxDepth="1">
                    <IfFileName glob="zkDemo.WARN.*.log" />
                    <IfLastModified age="10d" />
                </Delete>
            </DefaultRolloverStrategy>
        </RollingRandomAccessFile>

        <RollingRandomAccessFile name="Info"
                                 immediateFlush="false"
                                 append="true"
                                 fileName="/data/appLog/Logs/zkDemo.INFO"
                                 filePattern="/data/appLog/Logs/zkDemo.INFO.%d{yyyy-MM-dd}.log">
            <PatternLayout pattern="${layoutpattern}"/>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
            </Policies>
            <DefaultRolloverStrategy>
                <Delete basePath="/data/appLog/Logs/" maxDepth="1">
                    <IfFileName glob="zkDemo.INFO.*.log" />
                    <IfLastModified age="10d" />
                </Delete>
            </DefaultRolloverStrategy>
        </RollingRandomAccessFile>
    </Appenders>
    <Loggers>
        <asyncRoot level="INFO" additivity="false">
            <AppenderRef ref="Error"/>
            <AppenderRef ref="Warn"/>
            <AppenderRef ref="Info"/>
            <AppenderRef ref="STDOUT"/>
        </asyncRoot>
    </Loggers>
</Configuration>

 

最后附上springboot+zookeeper+slf4j+log4j2 的java demo, github: https://github.com/liweiDiao/zookeeperDemo

 

发布了13 篇原创文章 · 获赞 15 · 访问量 1万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览