Log4r - context (NDC)

Is there anyway to set a context for log4r?

log4j has somethign like NDC

Basically, in a web app, I’d like to correlate messages for the same
client. I’d like to do something like

context = “#{user.ip} #{user.name}”

and have it automatically appended to each logging message, or, even
better, appended whenever the context changes (that is, if there are 5
messages in a row from the same user, no need to keep repeating the
context)

is this available?

List R. wrote:

Is there anyway to set a context for log4r?

log4j has somethign like NDC

Basically, in a web app, I’d like to correlate messages for the same
client. I’d like to do something like

context = “#{user.ip} #{user.name}”

and have it automatically appended to each logging message, or, even
better, appended whenever the context changes (that is, if there are 5
messages in a row from the same user, no need to keep repeating the
context)

is this available?

I had a similar issue with Log4r, and no there isn’t currently a way to
automatically log the context (i.e. the logged in user etc) However, I
enhanced our copy of Log4r to take an extra optional parameter on all
log calls (.debug, .warn etc) that appears in all log messages, and I
plan to have the logged on user appear in there. That’s probably as
good as it’d going to get; the logger object is a singleton, so you
couldn’t just set a context into the object and have it available to the
user’s session and automatically append the context to every log
message. That might be a useful enhancement though.

On 4/25/06, Ed Syrett [email protected] wrote:

log calls (.debug, .warn etc) that appears in all log messages, and I
plan to have the logged on user appear in there. That’s probably as
good as it’d going to get; the logger object is a singleton, so you
couldn’t just set a context into the object and have it available to the
user’s session and automatically append the context to every log
message. That might be a useful enhancement though.


Posted via http://www.ruby-forum.com/.

You need to use Thread local variables for this sort of behaviour.

Ruby threads support this so its a matter of tweaking log4r

Thread.current[“name”] = “username”