2

I am using log4j-core-2.5 for my logging and I would like to Discard items when the logging queue is full. I have determined that this could be applied by using the log4j2.asyncQueueFullPolicy configuration property in log4j.

Could somone please help me with how to apply this configuration in my application.

I am using a file configuration like so...

Configurator.initialize("", "log4j-node.xml");

My config file looks somthing like this

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="debug">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <ThresholdFilter level="DEBUG" onMatch="ACCEPT" onMismatch="DENY"/>
            <PatternLayout pattern="[%d{dd-MMM HH:mm:ss.SSS} %logger{1}.%t %-5level] %msg%n"/>
        </Console>

        <RollingFile name="DefaultFile" fileName="../logs/app.log"
                 filePattern="../logs/app-%d{MM-dd-yyyy}-%i.log" bufferedIO="true" bufferedSize="32000" immediateFlush="false">
            <ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
            ...

        </RollingFile>

        <Async name="AsyncFile">
            <AppenderRef ref="DefaultFile"/>
            <LinkedTransferQueue/>
        </Async>

        <Async name="AsyncConsole">
            <AppenderRef ref="Console"/>
            <LinkedTransferQueue/>
        </Async>

    </Appenders>

    <logger name="org.hibernate">
        <level value="debug" />
    </logger>

    <Loggers>
        <Root level="TRACE">
            <AppenderRef ref="AsyncConsole"/>
            <AppenderRef ref="AsyncFile"/>
        </Root>
        <Logger name="org.hibernate.SQL" level="error" />
        <!-- Log all JDBC parameters -->
        <Logger name="org.hibernate.type" level="error" />
        <!-- Log all SQL DDL statements as they are executed -->
        <Logger name="org.hibernate.tool.hbm2ddl" level="error" />
        <!-- Log the state of all entities (max 20 entities) associated with the session at flush time -->
        <Logger name="org.hibernate.pretty" level="error" />
        <!-- Log all second-level cache activity -->
        <Logger name="org.hibernate.cache" level="error" />
        <!-- Log transaction related activity -->
        <Logger name="org.hibernate.transaction" level="error" />
        <!-- Log all JDBC resource acquisition -->
        <Logger name="org.hibernate.jdbc" level="error" />
        <!-- Log HQL and SQL ASTs during query parsing -->
        <Logger name="org.hibernate.hql.ast.AST" level="error" />
        <!-- Log all JAAS authorization requests -->
        <Logger name="org.hibernate.secure" level="error" />
        <!-- Log everything (a lot of information, but very useful for troubleshooting) -->
        <Logger name="org.hibernate" level="error" />

    </Loggers>
</Configuration>

Cheers

1 Answer 1

5

First, please upgrade to Log4j 2.10, which fixed an important issue in this area where log events no longer appear out of order in the log file when the queue is full.

You can configure Log4j2's behaviour via system property log4j2.AsyncQueueFullPolicy. See AsyncQueueFullPolicy and AsyncQueueFullPolicyFactory. The "Discard" policy with some log level may be of interest.

Property log4j2.AsyncQueueFullPolicy controls the routing behaviour. If this property is not specified or has value "Default", this factory creates DefaultAsyncQueueFullPolicy objects.

If this property has value "Discard", this factory creates DiscardingAsyncQueueFullPolicy objects. By default, this router discards events of level INFO, DEBUG and TRACE if the queue is full. This can be adjusted with property log4j2.DiscardThreshold (name of the level at which to start discarding).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.