tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Bill Wolosek <>
Subject RE: how to create log files user level
Date Thu, 16 Jun 2016 13:03:15 GMT
Hi Vijay,

We have accomplished user level logging using log4j's MDC feature along with a custom interceptor
in Struts 2.  The interceptor injects the user name into the MDC context before any struts
actions fire and then removes it from the MDC context after all actions have fired for a specific
web request.  This approach is thread safe and we have had great success with it.


public class MDCLogInterceptor implements Interceptor {

	private static final long serialVersionUID = 1L;
	private static final String USER_NAME_KEY = "userName";

	public String intercept(ActionInvocation invocation) throws Exception {
		try {
			String userName = //either get this from the session if using application authentication
or remoteUserId if using tomcat authentication;			
			if (!StringUtils.isBlank(userName)){
				MDC.put(USER_NAME_KEY, userName);
			return invocation.invoke();
		} finally {			
			if (MDC.get(USER_NAME_KEY) != null){

	public void destroy() {
		// TODO Auto-generated method stub		

	public void init() {
		// TODO Auto-generated method stub		

Don't forget to add the interceptor to your interceptor stack in your struts config.  We have
it listed first.

Add the MDC value to your log4j config:
<appender name="file" class="org.apache.log4j.DailyRollingFileAppender">
	<param name="File" value="${logs}/YourLogName.log" />
		<layout class="org.apache.log4j.PatternLayout">
		<param name="ConversionPattern" value="%d{MM/dd/yyyy HH:mm:ss.SSS} [%t] %-5p %x User[%X{userName}]
%C{1}.%M: %m%n" />

The "User[%X{userName}] " is the MDC part that adds the username to the log files from the
value inserted by the interceptor.  You can move it to anywhere in the appender pattern you

Thank you,

-----Original Message-----
From: Mark Thomas [] 
Sent: Thursday, June 16, 2016 3:18 AM
To: Tomcat Users List
Subject: Re: how to create log files user level

On 16/06/2016 09:05, Vijay Kumar wrote:
> Hi Andre,
> Thanks for the update.
> Could you please give me some information whether it's possible or not 
> to generate user level log because i have tried using log4j but it failed.
> Appreciate your help on this.

Either per user logging or per session logging should be possible. One of potentially many
ways is via a separate Logger for each session/user.
If you give each Logger a unique name based on the user name or session ID (both of which
should be unique) and use a common prefix you should be able to direct all logs with that
prefix to a file. With in the file you then use the Logger name to identify the source of
the log message.


> Thanks,
> Vijay G
> On Thu, Jun 16, 2016 at 12:59 PM, André Warnier (tomcat) 
> <>
> wrote:
>> Thanks. The rest below, as preferred on this mailing list.
>>> Thanks,
>>> Vijay G
>>> On Thu, Jun 16, 2016 at 12:40 PM, André Warnier (tomcat) 
>>> <>
>>> wrote:
>>> On 16.06.2016 08:50, Vijay Kumar wrote:
>>>> Hi Team,
>>>>> We have a requirement to create a log either at User level or at 
>>>>> User session level.
>>>>> Please provide your inputs on this.
>>>>> User level means a separate log to be created for that User and 
>>>>> the same should be used always for writing the log.
>>>>> or
>>>>> There will be one log file and within that log there should be a 
>>>>> context information to identify the user which we will do but want 
>>>>> to know which approach we have to use
>>>>> Session level means, is it possible to turn the logger mode to 
>>>>> INFO for a particular session and the log to be written in one of 
>>>>> the file at server/client side?
>>>>> Our application is built with Struts 1.2.
>>>>> Webserver used is Tomcat.
>>>>> Currently using Tomcat JULI Logging.
>>>>> Tomcat version ?
>> On 16.06.2016 09:18, Vijay Kumar wrote:
>>> We are using Tomcat 7.0.33 and 7.0.62
>>> 7.0.33 when Customer is on Java 1.7
>>> 7.0.62 when our customer is on 1.8
>>> But we can upgrade to 8 if required to achieve the logging support
>> I don't think that there is anything "standard" that will allow to 
>> create user-specific logfiles.
>> What you want is more an application-level thing, so it would have to 
>> be resolved at the application level.
>> You can create your own logger and do what you want. Any application 
>> module can call getRemoteUser(), and use the result to insert it in 
>> all the log messages that it issues.
>> Of course this will only work in code running within an application, 
>> and only when there is an authenticated user.
>> To avoid a lot of overhead (also at the system administration level), 
>> I would recommend to keep using the standard logfiles, and just 
>> insert a user-id "marker" in the log messages of interest.  This 
>> standard logfile can then be filtered a-posteriori by an external 
>> log-processing utility, which can then generate any individual files 
>> that you want.  That is likely to be a lot simpler to do than 
>> generating user-specific logfiles at the Tomcat level, and also would have a lot
less impact on performance.
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail:
>> For additional commands, e-mail:

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message