8

I use log4j2 in my project something like this:

    logger.log(Level.ERROR, this.logData);

My configuration file looks like this:

<?xml version="1.0" encoding="UTF-8"?>


<Configuration status="ERROR" DLog4jContextSelector="org.apache.logging.log4j.core.async.AsyncLoggerContextSelector">
    <Appenders>
        <!-- Async Loggers will auto-flush in batches, so switch off immediateFlush. -->
        <RandomAccessFile name="RandomAccessFile" fileName="C:\\logs\\log1.log" immediateFlush="false" append="false">
            <PatternLayout>
                <Pattern>%d %p %c{1.} [%t] %m %ex%n</Pattern>
            </PatternLayout>
        </RandomAccessFile>
    </Appenders>
    <Loggers>
        <Root level="error" includeLocation="false">
            <AppenderRef ref="RandomAccessFile"/>
        </Root>
    </Loggers>

It creates my file, I log something to it, but it's still empty. When I trying to delete this file, OS told me that it in use (if app currently working), but even if I stop application, file still empty.

So which settings should I change to make it work correctly?

Remko Popma
  • 35,130
  • 11
  • 92
  • 114
silent_coder
  • 6,222
  • 14
  • 47
  • 91

2 Answers2

5

I suspect that asynchronous logging is not switched on correctly. As of beta-9 it is not possible to switch on Async Loggers in the XML configuration, you must set the system property Log4jContextSelector to "org.apache.logging.log4j.core.async.AsyncLoggerContextSelector".

The reason you are not seeing anything in the log is that your log messages are still in the buffer and have not been flushed to disk yet. If you switch on Async Loggers the log messages will be flushed to disk automatically.

Remko Popma
  • 35,130
  • 11
  • 92
  • 114
  • You mean that `` do not work, and my logger still works in synchronous manner? – silent_coder Nov 09 '13 at 19:12
  • Yes, log4j will ignore the DLog4jContextSelector config element. – Remko Popma Nov 09 '13 at 23:04
  • Sorry for dummy question, but how I could set this settings, if it's not possible to set it in xml file? – silent_coder Nov 09 '13 at 23:07
  • You need to set this as a system property. For standalone apps you can pass -Dkey=value on the command line. In a pinch, you can also write System.setProperty("key", "value"); in your code. For web applications, there is usually a config file where you can edit the system properties. Check the docs for your webapp container in that case. – Remko Popma Nov 09 '13 at 23:27
  • Yes, my problem was caused half by delayed logging to a file, half for wrong configuration. It was 'Root level="error"' in xml, but I writing with Level.ALL So I change it level="ALL" and it start working. – silent_coder Nov 10 '13 at 00:08
5

I share a cleaner and easier solution.

https://stackoverflow.com/a/33467370/3397345

Add a file named log4j2.component.properties to your classpath. This can be done in most maven or gradle projects by saving it in src/main/resources.

Set the value for the context selector by adding the following line to the file.

Log4jContextSelector=org.apache.logging.log4j.core.async.AsyncLoggerContextSelector

Log4j will attempt to read the system property first. If the system property is null, then it will fall back to the values stored in this file by default.

Community
  • 1
  • 1
Davideas
  • 3,226
  • 2
  • 33
  • 51
  • That is correct. All log4j2 system properties can be specified in a `log4j2.component.properties` file in the classpath by design. – Remko Popma Aug 16 '16 at 11:54