Having the following config file:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="[%d{HH:mm:ss.SSS} %-5level] %logger{36} - %msg%n"/>
</Console>
<File name="File" fileName="error.log">
<PatternLayout pattern="[%d{ISO8601} %-5level] %logger{36} - %msg%n"/>
</File>
</Appenders>
<Loggers>
<Logger name="errors" level="error">
<AppenderRef ref="File"/>
</Logger>
<Root level="all">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
only writes (all) logging output to the console. I however had the intention to write anything above error
to a file named error.log
with a slightly different format. However, running my application results in everything being written to the console, leaving an empty file behind (which gets created, just not filled).
Somehow it seems like the Root logger catches everything because I had also tried this:
<Logger name="errors" level="error">
<AppenderRef ref="Console"/>
</Logger>
which does not log twice. I'm out of ideas really, I even copied an example from the docs (sample #2 from here) and that aswell leaves an empty file.
I suspect that the reason there's no log file is that Log4j2 didn't see the configuration file. By default Log4j2 looks for the configuration file in the classpath.
Option 1: use the Log4j 1. x bridge (log4j-1.2-api) You may be able to convert an application to Log4j 2 without any code changes by replacing the Log4j 1. x jar file with Log4j 2's log4j-1.2-api.
RollingFileAppender extends FileAppender to backup the log files when they reach a certain size. The log4j extras companion includes alternatives which should be considered for new deployments and which are discussed in the documentation for org. apache. log4j.
Log file rolling offers the following benefits: It defines an interval over which log analysis can be performed. It keeps any single log file from becoming too large and assists in keeping the logging system within the specified space limits.
Yes, the root logger level is ALL so it will receive all events. One option is to do this:
<Loggers>
<Root level="all">
<AppenderRef ref="Console" level="trace" />
<AppenderRef ref="File" level="error" />
</Root>
</Loggers>
This way you only have one logger, so you don't need to worry about additivity and in your code you can just write LogManager.getLogger(MyClass.class)
to get a Logger
instance. (If you use a named Logger, you would need to use the logger name in your code: LogManager.getLogger("error")
.)
Ah, I was being stupid. The attribute name
seems to be a filter for which classes will use that logger. Changing name
to my top level package resolved the issue.
Log4J2 doesn't write to file until the process has completed. By default the buffer isn't flushed with each call to the logger. For example,
<File name="File" fileName="LogfileName.log" immediateFlush="false" append="false">
<PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</File>
will not log to the file immediately. Change the values of immediateFlush to true to force the buffer to flush on each call to logger.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With