TCM
TCM

Reputation: 16920

A beginner's log4J question

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 :)

Upvotes: 0

Views: 324

Answers (2)

Trevor Tippins
Trevor Tippins

Reputation: 2847

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.

Upvotes: 1

Brian Agnew
Brian Agnew

Reputation: 272417

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.

Upvotes: 1

Related Questions