Here is a list of commonly encountered problems when using log4j:
The reason for observing duplicates in log4j output is either due
to having added the same appender multiple times to the same category
(typically root) or having added the same appender to different
categories ignoring the fact that appenders are inherited
cumulatively.
log4j does not eliminate appender duplicates. In other words, if
you add the same appender to a category n times, that appender
will be invoked n times to append to its target.
A slightly different cause is adding different appenders all
sharing the same underlying output target to some category. In the
most common occurrence of this phenomenon, the
BasicConfigurator.configure() method is invoked multiple times. Each
time it is invoked, this method adds an appender with a
One other common mistake is to forget that appenders are inherited
cumulatively from the hierarchy. For example, if you add an appender,
say The spaces in the value, i.e. In log4j version 0.9.0, all spaces are removed from both
ends of option values. In version 0.9.1 log4j reverted to the old
behavior where option values are not all automatically trimmed.
You can remedy this problem by disabling the JIT compiler and by
compiling the code without the -O option.
This exception is thrown because log4j does not support
homonyms. For example, the following will systematically throw a
By default, the To address this problem, the The Naturally you should check the classpath. But you should also
be aware of the presence of multiple classloaders in the JVM:
If you place log4j.jar in the Servlet, JSP and EJB containers inside of application servers
usually have their own special classloaders in addition to the
three mentioned above. While this provides for a greater
degree of separation for different webapps, EJB containers and the
application server runtime itself, it can provide headaches to the
uninitiated.
Classloaders are usually hierarchically related. The bootstrap
loader forms the root with the extension loader as its child. The
application loader is the child of the extension loader and it's
this "app loader" that we use by default when we write standalone
Java programs.
Upon receiving a class load request, the classloader usually
delegates it to the parent before attempting to service the request.
This allows the bootstrap and extension loaders to deliver any classes
that are part of the JDK or its extensions. Only after this delegation
fails will the classloader attempt to find the class itself. Note that
classloaders do not delegate requests to children.
Application servers often use the application loader for its runtime
classes and create separate classloaders for its webapp and EJB
containers. These additional classloaders may descend directly
from the app server's runtime classloader. If log4j is placed in the
classpath of a webapp classloader, another webapp classloader will not
necessarily see it. EJBs wouldn't see it either. If log4j is intended
to be made available to all objects participating in the app server, it
should be included in the classpath of a classloader high enough in the
classloader hierarchy to be seen by all classloaders.
A good article on classloaders with examples using IBM's WebSphere
application server can be found
here
in PDF format.
I cannot log to syslogd under linux.
If you are trying to log to the Unix syslog under Linux using the SyslogAppender,
then the Linux syslog daemon must be configured to accept log input
from the network. Otherwise, you will get an This can be done by adding the log4j tells me to initialize properly.
Logging output is written to a target by using an appender. If no
appenders are attached to a category nor to any of its ancestors, you
will get the following message when trying to log:
log4j: No appenders could be found for category (some.category.name).
log4j: Please initialize the log4j system properly.
Log4j does not have a default logging target. It is the
user's responsibility to ensure that all categories can inherit an
appender. This can be easily achieved by attaching an appender to the
root category.
Duplicates in log4j output.
System.out
target to the root category.
A
, to the root category, all other categories will
inherit A
as an appender. Thus, if you add A
to a category, say C
, then an enabled statement of
category C
, will print to A
twice, once
because A
is in root and once because it is in
C
.
Options are not parsed correctly.
The PropertyConfigurator relies on java.util.Properties
class to read in the configuration file. This class preserves spaces
in options. For example,
fruit=orange
is returned as an option having the key "fruit"
and the
value "orange "
.
"orange "
, are due to
invisible spaces at the end of the example shown above. Thus, some of
the options might not be interpreted correctly due to trailing
spaces.
Location information is printed as a "?" character.
Location information is extracted automatically by the PatternLayout
conversion patterns %C, %F, %M and %L. However, some just-in-time
(JIT) compilers make it impossible to extract location information. It
is also possible that the compiler that generated the byte code may
have omitted the LineNumber table as is done by -O option of javac
and jikes.
ClassCastException
when instantiating
a Category
subclasses.ClassCastException
Category c1 = Category.getInstance("bad");
MyCategory c2 = (MyCategory) MyCategory.getInstance("bad");
where MyCategory
is a subclass of
Category
. The problem occurs because the second
getInstance
invocation will retrieve the category created
in the fist invocation. This instance is a Category
object and cannot be cast to MyCategory
.
PropertyConfigurator
will create and
configure org.apache.log4j.Category
objects. Thus, if you try to
instantiate a category subclass for an already existing category, and
try to cast it to the subclass type, you will systematically get a
ClassCastException
.
PropertyConfigurator
admits
the log4j.categoryFactory
key. The value of this key will
be used as the factory to invoke when instantiating Category objects.
DOMConfigurator
has a finer grain method for
setting the class of the category object to instantiate.
Log4j class not found/defined
jre/lib/ext
directory
but place user-defined extensions to log4j in the application
classloader classpath, log4j configurators will not find them.
IOException:
connection refused
.
-r
option when starting
the daemon. Or more precesiely:
root
user
/etc/rc/init.d/syslog
case "$1" in
start)
echo -n "Starting system logger: "
daemon syslogd -r
/etc/rc/init.d/syslog restart