# Log4r - context (NDC)

**URL:** <https://www.ruby-forum.com/t/log4r-context-ndc/77505>\
**Category:** Ruby\
**Created:** [March 6, 2006, 11:30pm UTC](https://www.ruby-forum.com/t/log4r-context-ndc/77505 "2006-03-06T23:30:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lstrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lstrecv](https://www.ruby-forum.com/u/lstrecv)\
**Post date:** [March 6, 2006, 11:30pm UTC](https://www.ruby-forum.com/t/log4r-context-ndc/77505/1 "2006-03-06T23:30:51Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![lstrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lstrecv](https://www.ruby-forum.com/u/lstrecv)\
**Post date:** [April 25, 2006, 11:11am UTC](https://www.ruby-forum.com/t/log4r-context-ndc/77505/2 "2006-04-25T11:11:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![lstrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lstrecv](https://www.ruby-forum.com/u/lstrecv)\
**Post date:** [April 25, 2006, 1:41pm UTC](https://www.ruby-forum.com/t/log4r-context-ndc/77505/3 "2006-04-25T13:41:20Z")

</div>

On 4/25/06, Ed Syrett [removed\_email\_address@domain.invalid](mailto: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/](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”
