The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

Passing data to logger class

I'm trying to reduce or eliminate the global variables in my web application. In most cases, it make sense to pass the appropriate data to only the classes that need it.

But the one exception seems to be logging. Every class needs the ability to write to the log, and the logger needs to write session information with each entry. This seems to require that I pass around a data object to every class, in case it might need to pass it to the logger.

Any better ideas?
Friday, July 06, 2007
Log4J has a thing called the 'Nested Diagnostic Context' which lets you push info onto a stack that it maintains in a ThreadLocal. There is also a 'Message Diagnostic Context' for holding client-specific stuff. The contents of these contexts can be referred to in the conversion patterns specified in the logging context.

Have a look at the docs for whatever logging framework your using and see if it has a similar feature.

If not it probably shouldn't be too hard to make something yourself using your languages equivalent to a ThreadLocal to pass the info around and keep it out of the method signatures.
Friday, July 06, 2007
>>specified in the logging context<<

"specified in the logging configuration" I meant to say
Friday, July 06, 2007
Obviously the answer depends on the specifics, but an approach I may take would be to pass a reference to session context to the logging object's constructor (or initialization function).

BTW since in most cases you have only a single logging object, I would use the Singleton Design Pattern rather than pass a reference to the log all over the place.
Dan Shappir Send private email
Saturday, July 07, 2007
Some options:

1. Pass just values and have a max number of value slots. Usually necessary only in very high performance code.
2. Format the log data into a string in your thread and then queue the log string to another string for logging. This is surprisingly efficient and gets rid of most the nasty logging issues.
son of parnas
Saturday, July 07, 2007
Thanks folks. I think I'll take the approach of passing in session data when I initialize the logger object, and whenever the session data changes.
Saturday, July 07, 2007

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
Powered by FogBugz