Enable Log4j logging for specific users
In the log4j.properties file, I set the level to ERROR. For some users, I need to set the level to DEBUG. I was able to change the logging level at runtime, but this will be available to all users accessing the application at the same time. Is there any other way that we can enable logging for selected users? Any help would be greatly appreciated.
I'm assuming you have a web application or similar and multiple identifiable users accessing it at the same time?
You cannot easily change the configuration for each user in Log4j. However, I would consider the following (this assumes a web server or similar, with each user request on a separate thread):
- identify the user making the server call as soon as they make that call. Store this user data in MDC
- Implement custom Log4J appender . For every incoming call, you can check the user stored in MDC and adjust the severity / logging as needed.
This is a bit of work (assuming the above assumptions), but should work. Obviously this is not true if the assumptions I made about your architecture are wrong.
a source to share
This issue is addressed in logback (log4j successor) with TurboFilters . See MDCFilter and MarkerFilter Configuration Example. If you need further help, please contact the journal-user mailing list.
a source to share
Yes, but.
You could make your logs with a user specified class +, not just a class, and create a logger in the method (or if you want fancy, cache loggers in some user-entered pool) and then set up logging accordingly.
This is very messy and obtrusive code, but since the user is a property of the runtime, I don't see how (not coming from AspectJ or its cousins) how you avoid this kind of mess.
Another option is to specifically format your log messages and include the username in the log message and then analyze the logs. This will allow debugging for everyone (which might be a performance issue, obviously), but if the problem is more about isolating user registration rather than limiting the number of debug calls, this might be the solution.
a source to share