0

I changed my logging from sync to async but I am not sure how to put policies on it. I want to apply my sync logging settings to async logging. Please see below.

// I switched from this 

<RollingFile name="fileLogger" fileName="${logPath}/log.log"
                 filePattern="${logPath}/log-%d{yyyy-MM-dd-hh}-%i.log">
        <PatternLayout>
            pattern="${logPattern}"/>
        </PatternLayout>
        <Policies>
            <OnStartupTriggeringPolicy/>
            <SizeBasedTriggeringPolicy size="10 MB"/>
        </Policies>
        <DefaultRolloverStrategy max="5"/>
    </RollingFile>
</Appenders>    

//to this 

<File name="prodLog" fileName="${logPath}/log.log">
        <PatternLayout
                pattern="${logPattern}"/>
    </File>
    <Async name="asyncLogger" includeLocation="true">
        <AppenderRef ref="prodLog"/>
        <ArrayBlockingQueue/>
    </Async>

1 Answer 1

1

I dont think you can use the same policies for the File appender. To achieve an asynchronous behavior for a rolling file, you could use the RollingRandomAccessFile, just like the following example:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="debug">
    <Appenders>
        <Console name="Console-Appender" target="SYSTEM_OUT">
            <PatternLayout>
                <pattern>
                    [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
                </pattern>>
            </PatternLayout>
        </Console>
        <RollingRandomAccessFile name="Rolling-Random-Access-File-Appender"
                                 fileName="logs/rollingrandomaccessfile.log"
                                 filePattern="archive/logs/rollingrandomaccessfile.log.%d{yyyy-MM-dd-hh-mm}.gz">
            <PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"/>
            <Policies>
                <SizeBasedTriggeringPolicy size="1 KB"/>
            </Policies>
            <DefaultRolloverStrategy max="30"/>
        </RollingRandomAccessFile>

    </Appenders>
    <Loggers>
        <AsyncLogger  name="guru.springframework.blog.log4j2async" level="debug">
            <AppenderRef ref="Rolling-Random-Access-File-Appender"/>
        </AsyncLogger>
        <Root level="debug">
            <AppenderRef ref="Console-Appender"/>
        </Root>
    </Loggers>
</Configuration>

More on this in this post: https://springframework.guru/asynchronous-logging-with-log4j-2/

Regards

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.