简体   繁体   中英

Logging with specific label in log file for every user

I have web application in Java (JSP + Servlets) and logging with log4j 1.2.17. When user login on site he take ID. I wanna see all his actions in log file with ID prefix like this:

01.05.2015 11:12:30 INFO [Roy] - login correct

But i don't want to pass ID in every calling method to logging events in that method. I want to save ID somewhere and take it when need.

What you probably want is a Mapped Diagnostic Context . This effectively gives you a stack of ThreadLocal to put data elements into that you can log later.

In java...

MDC.put('LOGIN_ID', 'Roy');

In your log pattern:

"%d %p [%X{LOGIN_ID}] %m

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM