How to do log4j configuration to include the HttpServlet session variables

How to do log4j configuration to include the HttpServlet session variables to any log by default.

Assume i have 3 variables - session id, user name, user id, company id

I want all these variables to be printed in the log with date and logger severity for any log.

How to configure it to print all the above values ?

Thanks.
Software ProgrammerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Amitkumar PSr. ConsultantCommented:
In this case, you will need to write custom appender by extending AppenderSkeleton which will extract information from HttpSession nad then you can use it in log statements.

Refer

FAQs at http://logging.apache.org/log4j/1.2/faq.html

Example Implementation at http://www.wideskills.com/log4j-tutorial/10-custom-appender-and-layout-in-log4j
Software ProgrammerAuthor Commented:
Instead of the above, can we put the values in MDC and configure the values in log4j directly.

example,

MDC.put("userName", userName);

%userName in log4j.properties file

What is the advantage of the above than having an appender in a file ?
girionisCommented:
Functionally it is the same thing. But with MDC it is easier and faster to do what you want. On the other hand a customer appender gives you more flexibility, since you can put business logic in there if you need to.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.