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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]