tags:

views:

60

answers:

2

Hello,

i configured log4j for basic purpose usin the conversion pattern :-

log4j.appender.rollingFile.layout.ConversionPattern=%p %t %c - %m%n

But now i want to log the class name from which the error came as well as the username(available in session object) as well as the date and time when that event occurs. How do i do this? What changes do i need to make in format string?

Thanks in advance :)

+1  A: 

Take a look at the PatternLayout docs for most of what you want.

The headache you face is getting the user name from the session (Log4j can't do this automatically). I would perhaps investigate NDCs or MDCs, and populate these from the session (perhaps in a servlet filter?). They are per-thread, so assuming your user has the same scope then this may help.

Brian Agnew
Hi Brian, should i directly specify the username in user message (custom message) that we pass, rather than using MDC or NDC?
Ankit Rathod
If you can do that, yes. But will you need to pass it to each method that requires a log message with that info in it ?
Brian Agnew
+1  A: 

To get the class name out you can use %l, but you'll take a bit of performance hit. To get the username out, you'll need to use a mapped or nested diagnostic context and then specificy %X or %x respectively in the pattern string.

Check the PatternLayout javdocs.

Trevor Tippins