Forum: Ruby log4r - context (NDC)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
List R. (Guest)
on 2006-03-07 00:30
(Received via mailing list)
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?
Ed Syrett (Guest)
on 2006-04-25 13:11
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.
Lyndon S. (Guest)
on 2006-04-25 15:41
(Received via mailing list)
On 4/25/06, Ed Syrett <removed_email_address@domain.invalid> 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"
This topic is locked and can not be replied to.