clstaudt
clstaudt

Reputation: 22438

Turn off logging with log4cxx for minimum overhead

I use log4cxx for logging. However, logging may introduce some performance overhead which I need to minimize.

How do I turn off logging (at runtime or compile time) so that the overhead is minimized - short of removing all log statements from the code?

The documentation states that

The user should be aware of the following performance issues.

Logging performance when logging is turned off.

When logging is turned off entirely or just for a set of levels, the cost of a log request consists of a method invocation plus an integer comparison. The LOG4CXX_DEBUG and similar macros suppress unnecessary expression evaluation if the request is not enabled.

But how to turn it off entirely? Is this the minimum overhead that can be achieved?

Upvotes: 3

Views: 1889

Answers (1)

jmetcalfe
jmetcalfe

Reputation: 1316

If you really want to disable all logging at compile time, just redefine the log4cxx macros to nothing i.e.

#define LOG4CXX_TRACE(logger, expression)    
#define LOG4CXX_DEBUG(logger, expression)    
#define LOG4CXX_INFO(logger, expression)   
#define LOG4CXX_WARN(logger, expression)    
#define LOG4CXX_ERROR(logger, expression)    
#define LOG4CXX_FATAL(logger, expression) 

Zero overhead. For the runtime case, you always have to incur some cost. From a quick look, I actually make the overhead slightly larger than their documentation says, usually involving an extra virtual method call. This is essentially the minimum overhead using this library, though you might get some very small improvements using link time optimisation etc to remove the method invocation overhead.

Upvotes: 5

Related Questions