log4j vs. java 1.4 logging performance

J

Jeff

I'm using log4j, but OptimizeIt (Borland's code profiler) complains that
log4j is creating too many strings. This problem occurs when I turn logging
to it's lowest levels (i.e. generate the minimum set of messages).

Clearly, I can delete log4j messages from the source code to reduce that
load, but I would rather leave the logging in place and find another
optimization.

Most of my logging is for debugging so I want to turn those messages off in
normal Production, but have the ability to use them for problems.

Does anyone know if Java's built in logging is lower overhead than log4j?
Any other suggestions?

Thanks

Jeff
 
C

Christophe Vanfleteren

Jeff said:
I'm using log4j, but OptimizeIt (Borland's code profiler) complains that
log4j is creating too many strings. This problem occurs when I turn
logging to it's lowest levels (i.e. generate the minimum set of messages).

Clearly, I can delete log4j messages from the source code to reduce that
load, but I would rather leave the logging in place and find another
optimization.

Most of my logging is for debugging so I want to turn those messages off
in normal Production, but have the ability to use them for problems.

Does anyone know if Java's built in logging is lower overhead than log4j?
Any other suggestions?

Thanks

Jeff

From the log4j manual at http://logging.apache.org/log4j/docs/manual.html:

logger.debug("Entry number: " + i + " is " + String.valueOf(entry));

incurs the cost of constructing the message parameter, i.e. converting both
integer i and entry to a String, and concatenating intermediate strings,
regardless of whether the message will be logged or not. This cost of
parameter construction can be quite high and it depends on the size of the
parameters involved.

To avoid the parameter construction cost write:

if(logger.isDebugEnabled() {
logger.debug("Entry number: " + i + " is " +
String.valueOf(entry));
}

Follow that advice, and you wont be creating lots of strings when not
running at the debug level.
 
D

Dale King

Jeff said:
I'm using log4j, but OptimizeIt (Borland's code profiler) complains that
log4j is creating too many strings. This problem occurs when I turn logging
to it's lowest levels (i.e. generate the minimum set of messages).

Clearly, I can delete log4j messages from the source code to reduce that
load, but I would rather leave the logging in place and find another
optimization.

Most of my logging is for debugging so I want to turn those messages off in
normal Production, but have the ability to use them for problems.

Does anyone know if Java's built in logging is lower overhead than log4j?
Any other suggestions?


One thing might be how you are doing your logging. If you have something
like:

myLog.log( Priority.INFO, "The value is " + theValue );

This will create a new string even if you don't do the logging. One way to
avoid this is to use a MessageFormat string and the values as parameters for
that MessageFormat. This will avoid string creation unless it is actually
logged. Both log4j and 1.4 logger have ways to do that, but the support for
it is not as great in log4j and requires a resource bundle. It's a pity that
they do not have variants of the methods like info that work this way too.
You would do something like:

myLog.l7dlog( Priority.INFO, "value.message", new Object[] { new
Integer( theValue ) } );

For the JDK1.4 logging API you might do something like:

myLog.log( Level.INFO, "The value is {0}", new Integer( theValue ) );

That will create Integers but not Strings unless logging is turned on. With
the changes coming in 1.5 ( varargs, autoboxing, and concise array literals)
the syntax of these can be simplified.

You might also want to look at the Jakarata Commons logging API. It is an
API that sits atop either of the two so your logging is independent of which
one you choose. If you want to switch between the two you can do so by
changing one line and even can do it at runtime. That would make comparison
easier and allows compatibility with pre-1.4 VM's.

http://jakarta.apache.org/commons/logging.html

Unfortunately, the Commons Logging API has no support for parameterized
messages whatsoever.
 
S

Scott Ellsworth

Jeff said:
I'm using log4j, but OptimizeIt (Borland's code profiler) complains that
log4j is creating too many strings. This problem occurs when I turn logging
to it's lowest levels (i.e. generate the minimum set of messages).

You can always use guards, if the arguments are a problem.

public class DoSomething {
private static Logger log =
Logger.getLogger(DoSomething.class.getName());
protected Collection processNodes(Collection nodes){
if (log.isDebugEnabled())
log.debug("processNodes("+getSortedNodeIDList(nodes)+")");
// Do the actual processing
}
}

This snippet does not create the "processNodes(1, 3, 8, 9)" string used
for the debug output unless it would actually get logged.

Scott
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,782
Latest member
ThomasGex

Latest Threads

Top