You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-user@logging.apache.org by Steven Keens <ke...@pcigeomatics.com> on 2008/08/04 20:28:01 UTC

NDC, MDC, or log.debug( new MyLogObj( "my message", context ) )?

Hello all:
I'm looking for opinions on whether to use an NDC, MDC, or use something like log.debug( new MyLogObj( "", context) ).

I guess I should provide some background:
I have a database that stores the states of "jobs" that ran on the system.  Each job has a unique identifier.  The database can also store log messages related to the job via a table named "joblogs" that has a foreign key (column named "job_id") to the "jobs" table.

I have created an appender subclass that uses Hibernate to add the log messages to the "joblogs" table.  The problem is that my appender needs to know the "job_id" (so that the foreign key "joblogs.job_id" can be filled in).

I can think of 4 approaches to pass the job_id through to the appender.
1) Create loggers for each identifier.  I rejected this one pretty quickly because of the accumulation of loggers and the requirement to parse the logger name. For example:
	Logger log = Logger.getLogger( " job.id.1" );

2) Create a new class (named something like JobLog) that is passed as the first parameter in the debug(), info(), warn(), and error() functions.  This is the one I implemented before looking deeply into NDCs and MDCs.  For example:
	
	private static final Logger log = Logger.getLogger( "job" );
	int jobId = 1;
	log.warn( new JobLogMsg( "Avoid doing that!", jobId ) );

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" );

4) Use NDC.  I think using MDCs is better than NDCs but I'm still open to being convinced otherwise.

My preference is for 2 (because it's already implemented and works and the code is shorter) but I wonder if 3 might provide a better solution for reasons that I can't think of at the moment.

Cheers 
--
Steven Keens
 
"In preparing for battle I have always found that plans are useless, but planning is indispensable."
          -- Dwight D. Eisenhower


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


Re: NDC, MDC, or log.debug( new MyLogObj( "my message", context ) )?

Posted by Paul Smith <ps...@aconex.com>.
>
> 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