3

There is huge web app. When user interact with web application, most of functions are called only once, so there are no performance issues. I wrote new "import from file" method which calls many times different methods from system. I don't want to modify methods of other people nor disable log4j. Import from file will be used rarely and is treated like add-on, not major functionality. I run into performance problems, because of this. After profiling I found out that almost 22% of time is used by log4j.

Is it possible to programmatically disable log4j for one method and methods it will call?

Igor S.
  • 3,332
  • 1
  • 25
  • 33

1 Answers1

6

I think for performance reasons you should replace calls like this:

 log.debug("some informantion: " + objA);

with this:

if(log.isDebugEnabled()) {
   log.debug("some informantion: " + objA);
}

OR else you can use a FormatLogger class like this: https://stackoverflow.com/a/105908/548225

EDIT

To programmatically disable logging, you can use code like this:

Logger.getLogger("your.logger.name").setLevel(Level.OFF);
Logger.getRootLogger().setLevel(Level.OFF);
Community
  • 1
  • 1
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • I don't want to modify methods of other people. Is it really big system and it will impact all it functionality just because of rarely used import method. – Igor S. Mar 28 '13 at 10:08
  • Pls see my EDIT section to disable logging programmatically. – anubhava Mar 28 '13 at 10:13
  • 1
    For performance reasons ? Logger.debug() already do the `isDebugEnabled()` check so for performance reasons, you should NOT do the check youself ;-) . See http://grepcode.com/file/repo1.maven.org/maven2/log4j/log4j/1.2.14/org/apache/log4j/Category.java#Category.debug%28java.lang.Object%29 – lmo May 13 '16 at 09:14
  • 1
    @Imo Using `if` you do not need to evaluate variables/methods and the whole string concat. Same reason why Log4J2 now has support for lambdas to delay the evaluation – thatsIch Oct 08 '16 at 10:28