views:

59

answers:

2

Currently I have a library that is logging certain information as ERROR. If I change my log4j settings like this:

log4j.logger.com.company.theirpackage.foo=OFF

that will completely disable the library's logging altogether. However, what I'd really like is to still see the information, but have it logged at a WARN or INFO level. In other words, when that particular code calls log.error(), I want it to be as if they had called log.warn() or log.info() instead.

Is there a way to do this with log4j?

+1  A: 

Not directly, but you could write a custom appender that intercepts the calls, checks the levels, and then prints them at whatever level you want. Or, you could do some aspect oriented programming and intercept/change their calls.

But why would you want to change the level they log it at?

Jeff Storey
Our service treats an ERROR as a really bad event, whereas this library logs all sorts of unimportant things as ERROR. I could change our log monitoring to ignore ERRORs from this package, but I'd rather this library just not log such unimportant things as ERRORs.
Ross
Got it. Are you using any application frameworks like Spring? They make aspect weaving pretty easy.
Jeff Storey
+1  A: 

I think this is possible, by implementing a Filter, in which the level inside a LoggingEvent is changed when it matches certain conditions.

Confusion
Can you change the level? I didn't think you could.
Jeff Storey
There isn't a setLevel() method, but you can use reflection to set the field directly.
Confusion