Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
385 views
in Technique[技术] by (71.8m points)

java - Log4j2 Async Logging RollingFile with JsonLayout not working

I want to include in my project a rolling logging file but with json inside instead of typical PatternLayout.

It worked but for performances matters, I wanted to make my JSON logger async. So I have changed the Logger to AsyncLogger but the result is that I have no more logs in my file.

I don't know if I have missed something in the documentation about incompatibility between AsyncLogging and JsonLayout Rolling File. So here I am, asking you for help !

Find below my log4j2.xml configuration for my project :

<Configuration>
    <Properties>
        <Property name="LOG_PATTERN">
            %d{yyyy-MM-dd HH:mm:ss} [%t] %-5level %c{1.} - %msg - [${sys:hostName}] %n
        </Property>
    </Properties>
    <Appenders>
        <!-- Rolling File Appender -->
        <RollingFile name="File" fileName="@log4jFileValue@/report-business.log"
            filePattern="@log4jFileValue@/report-business.log-%d{yyyy-MM-dd}-%i.gz">
            <PatternLayout>
                <Pattern>${LOG_PATTERN}</Pattern>
            </PatternLayout>
            <Policies>
                <!-- To rollover logs on daily basis, set interval to 1 in TimeBasedTriggeringPolicy -->
                <TimeBasedTriggeringPolicy interval="1" />
                <!-- To rollover log files based on log file size -->
                <SizeBasedTriggeringPolicy size="100MB" />
            </Policies>
            <!-- Historical logs day save -->
            <DefaultRolloverStrategy max="90" />
        </RollingFile>

        <!-- Rolling File Appender -->
        <RollingFile name="JsonFile" fileName="@log4jFileValue@/report-json.log"
            filePattern="@log4jFileValue@/report-json.log-%d{yyyy-MM-dd}-%i.gz">
            <JSONLayout  compact="true"
                         complete="false"
                         objectMessageAsJsonObject="true"
                         includeStacktrace="false"
                         includeNullDelimiter="false"
                         eventEol="true">
                <KeyValuePair key="kind" value="DLR"/>
            </JSONLayout>
            <Policies>
                <!-- To rollover logs on daily basis, set interval to 1 in TimeBasedTriggeringPolicy -->
                <TimeBasedTriggeringPolicy interval="1" />
                <!-- To rollover log files based on log file size -->
                <SizeBasedTriggeringPolicy size="100MB" />
            </Policies>
            <!-- Historical logs day save -->
            <DefaultRolloverStrategy max="90" />
        </RollingFile>
    </Appenders>
    <Loggers>
        <AsyncLogger name="LOGGER_JSON" additivity="false" level="info" includeLocation="true">
            <AppenderRef ref="JsonFile"/>
        </AsyncLogger>
        <Root level="@logLevel@" includeLocation="true">
            <AppenderRef ref="File" />
            <AppenderRef ref="MailAppender" />
        </Root>
    </Loggers>
</Configuration>

Just a quick note that "@log4jFileValue@" is a variable depending of build profile defined in filters files.

Thanks in advance for your help

PS : Sorry for my awful english level from France !

question from:https://stackoverflow.com/questions/65848741/log4j2-async-logging-rollingfile-with-jsonlayout-not-working

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...