logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Paul Smith <psm...@aconex.com>
Subject Re: NDC, MDC, or log.debug( new MyLogObj( "my message", context ) )?
Date Tue, 05 Aug 2008 22:10:15 GMT
>
> 3) Use MDC.
> 	private static final Logger log = Logger.getLogger( "job" );
>
> 	Integer jobId = new Ingteger(1);
> 	MDC.put( "jobid", jobId );
> 	log.error( "Job failed!", exception );
> 	MDC.remove( "jobid" );
>
>

This would be my recommended approach from experience.  One day  
you'll find there's some other crucial bit of info you need to track  
(say, userId), and then you're forced to modify every place you  
create the custom object to take that new parameter, or start  
creating optional constructors (eww).

Instead, at the point the current Thread knows what the user is  
(usually somewhere at the start of the operation) it can set and  
forget it and no other code needs to know.    Remember this is a  
logging function (you're trying to instrument your logs with more  
detail) and you could potentially be 'polluting' all of your code  
with the concept of JobId where it has no relevance (that is, it just  
so happens some bit of code is running in the context of a particular  
JobId).

cheers,

Paul



---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Mime
View raw message