views:

383

answers:

3

Hi, In the log4j.properties file I've set the Level to ERROR. For certain users I need to set Level to DEBUG. I was able to change the Logging Level at run time; but this will be enabled for all the users accessing the application at the same time. Is there any other method by which we can enable logging for selected users? Any help will be greatly appreciated.

Thank you.

A: 

Yes, but.

You could make your loggers named with the class + user involved, rather than just the class, and create the logger on the method (or if you want to get fancy, cache loggers in some kind of pool keyed by user), and then configure logging appropriately.

It is very messy and intrusive to the code, but since the user is a runtime property, I don't see how (short of AspectJ or its cousins) how you avoid that kind of mess.

Another option is to specially format your log messages and include the user name in the log message, and then parse the logs afterwards. This would enable debugging for everyone (which may be a performance issue, obviously) but if the concern is more about isolating a users logging rather than limiting the amount of debug calls, that may be a solution.

Yishai
+4  A: 

I presume you have a web application or similar, and multiple identifiable users accessing it simultaneously ?

You can't easily alter configuration per user in Log4j. However I would consider the following (this assumes a web server or similar, with each user request being on a separate thread):

  1. identify the user making a server call as soon as they make that call. Store that user data in a MDC
  2. Implement a custom Log4J appender. For each incoming call, you can inspect the user stored in the MDC, and adjust the severity/logging as required.

That's a little bit of work (given the above assumptions) but should work. Obviously it's not valid if the assumptions I've made about your architecture are incorrect.

Brian Agnew
Don't create a custom appender. You'd have a new type for every kind of appender. Instead, create a single Filter that can be composed with any appender.
erickson
I'd vaguely envisaged a custom appender delegating to further sets of appenders. But filters look like a very suitable solution in this instance.
Brian Agnew
A: 

This problem is solved in logback (log4j's successor) with TurboFilters. See the example entitled "MDCFilter and MarkerFilter configuration". If you need further help, contact the logback-user mailing list.

Ceki