I'm working on a set of web services and we'd like to have a daily rotated log.
I'm trying to get org.apache.log4j.rolling.RollingFileAppender
from the log4j extras companion working, since the documentation suggests this is best for production environments.
I have both the main log4J library (log4j-1.2.15.jar
) and the log4j extras library (apache-log4j-extras-1.1.jar
) on the classpath.
I have the following configuration for the appender in the log4j.properties
file:
### SOAP Request Appender
log4j.appender.request=org.apache.log4j.rolling.RollingFileAppender
log4j.appender.request.File=SOAPmessages.log
log4j.appender.request.RollingPolicy=org.apache.log4j.rolling.TimeBasedRollingPolicy
log4j.appender.request.RollingPolicy.ActiveFileName =SOAPmessages-%d.log
log4j.appender.request.RollingPolicy.FileNamePattern=SOAPmessages-%d.log.zip
log4j.appender.request.layout = org.apache.log4j.PatternLayout
log4j.appender.request.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
However, when I start the web service with log4j in debug mode I get these error messages:
log4j: Parsing appender named "request".
log4j: Parsing layout options for "request".
log4j: Setting property [conversionPattern] to [%d{ABSOLUTE} %5p %c{1}:%L - %m%n].
log4j: End of parsing for "request".
log4j: Setting property [file] to [/logs/SOAPmessages.log].
log4j:WARN Failed to set property [rollingPolicy] to value "org.apache.log4j.rolling.TimeBasedRollingPolicy".
log4j:WARN Please set a rolling policy for the RollingFileAppender named 'request'
log4j: Parsed "request" options.
I've found documentation about how to configure this appender a little sparse, so can anyone help me fix my configuration?
EDIT0: Added debug mode output, rather than just the standard warnings
Faced more issues while making this work. Here are the details:
apache-log4j-extras-1.1.jar
in the classpath, didn't notice this at first.RollingFileAppender
should beorg.apache.log4j.rolling.RollingFileAppender
instead oforg.apache.log4j.RollingFileAppender
. This can give the error:log4j:ERROR No output stream or file set for the appender named [file].
log4j-1.2.14.jar
tolog4j-1.2.16.jar
.Below is the appender configuration which worked for me:
Update: at least as early as 2013 (see Mubashar's comment) this started working.
According to Log4jXmlFormat you cannot configure it with log4j.properties, but only using the XML config format:
Unfortunately, the example log4j.xml they provide doesn't work either:
You have a bad package name
The correct one is:
I had a similar problem and just found a way to solve it (by single-stepping through
log4j-extras
source, no less...)The good news is that, unlike what's written everywhere, it turns out that you actually CAN configure TimeBasedRollingPolicy using log4j.properties (XML config not needed! At least in versions of log4j >1.2.16 see this bug report)
Here is an example:
BTW the
${instanceId}
bit is something I am using on Amazon's EC2 to distinguish the logs from all my workers -- I just need to set that property before callingPropertyConfigurator.configure()
, as follow:In Log4j2, the "extras" lib is not mandatory any more. Also the configuration format has changed.
An example is provided in the Apache documentation
Toolbear74 is right log4j.XML is required. In order to get the XML to validate the
<param>
tags need to be BEFORE the<rollingPolicy>
I suggest setting a logging threshold<param name="threshold" value="info"/>
When Creating a Log4j.xml file don't forget to to copy the log4j.dtd into the same location.
Here is an example:
Considering that your setting a
FileNamePattern
and anActiveFileName
I think that setting aFile
property is redundant and possibly even erroneousTry renaming your log4j.properties and dropping in a log4j.xml similar to my example and see what happens.