jordansissel/ruby-cabin — Issue #6: Context data
Author: schmurfy
Created: 2012-01-25 11:26:15+00:00
State: open

I discovered your library yesterday and I really like it, I hate printf logging more and more with each project but never really managed to find a nice way to wrap an alternative in a nice way.

I started playing around with the idea of using an hash like syntax instead of plain strings since I stumbled upon logstash and the closest thing I found to my needs is radar (http://radargem.com/) which does something similar but is aimed at handling errors.

There is a nice thing in radar you don't seem to have in your gem (but I admit I did not took a deep look at it yet), it is a way to have contextual informations added automatically without having to include them in every messages you log.

Here is some code to give you a better idea of what I mean:

```ruby

class AddUserInfo
def update_message(msg)
if Thread.current[:user]
msg.merge(
:user_id => Thread.current[:user].id
)
end
end
end

logger.add_extension(AddUserInfo)

logger.info("Yeah it worked", :something => 123)
# => {"message" => "Yeah it worked", "user_id" => 9}

logger.remove_extension(AddUserInfo)
```

Something like this could also be useful:

```ruby
logger.with_extension(AddUserInfo) do
logger.info(...)
end
```

What do you think of this ?
This is something I could work on but only if there is chance it can get merged in the gem.