Pergunta

I have tried below config and found that the log rotation happens as expected and a new log file created for every rotation.

However when there are multiple instances of servers are running pointing to same log file. Once the log reaches the limit, the log gets backuped up, but instead of new file (0KB), new logs gets written to the same old file thus incresing the file size.

Looks like in multiple instances, where the file is locked by both instances for writing, the rollback is not proper.

Any workaround/suggestion? well having a new file for each instance is not really an option.

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="" packages="">
    <Appenders>
        <RollingRandomAccessFile name="RollingRandomAccessFile"
            fileName="../logs/ws_new.log" append="false" filePattern=".../logs/ws_new-%d{MM-dd-yyyy}-%i.log">
            <PatternLayout>
                <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="50 KB" />
            </Policies>
            <DefaultRolloverStrategy max="3"/>
        </RollingRandomAccessFile>
    </Appenders>
    <Loggers>
        <root level="error">
            <AppenderRef ref="RollingRandomAccessFile" />
        </root>

         <Logger name="org.exm.test" level="trace" additivity="false">
            <AppenderRef ref="RollingRandomAccessFile"/>
        </Logger>


    </Loggers>


</Configuration>
Foi útil?

Solução

Log4j is not designed to be used that way. If multiple processes try to write to the same log file the results cannot be guaranteed. You may end up losing log messages.

I strongly recommend you have separate log files for your processes.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top