rudionrails / yell-adapters-gelf

Graylog2 adapter for Yell
MIT License
3 stars 4 forks source link

Support for custom fields #1

Closed kke closed 12 years ago

kke commented 12 years ago

How about adding support for custom fields?

Perhaps something like this in greylog.rb : _datagrams = datagrams( {'version' => '1.0', ... ... }.merge(Thread.current["custom_log_fields"] || {}) )

rudionrails commented 12 years ago

Hi, what is your current use case for this? Might be worth looking into adding such functionality if you can provide me with an example (but would rather not go for a Thread.current solution). Might need to extend upon the Yell::Event class...

kke commented 12 years ago

There's two things: 1) custom fields such as id of the item we were working with, so that if customer asks what happened to certain transaction, we can find everything related to that from greylog. 2) we're using logID's to group certain blocks like : transaction "stuff" do # generates an UUID for the duration of this block do_some_stuff logger.info "did some stuff" end

vkoivula commented 12 years ago

In addition to kke's comments about transactions. You can also set defaults, that are to be used in subsequent log messages during the transaction. logger_trx(_customer_id => "123", _foo => "bar"). logger.info(:short_message => "blaah", _another_custom_field => "bar"). In that way you don't need to repeat fields which you like to have in every log message!

Also I have used that graylog2 short message as console message, so that you can write logging in graylog2 extended log format, and use only those short messages for other outputs.

rudionrails commented 12 years ago

OK, So I've just committed a possible solution. However, this required to adapt the core yell gem. If you would be so kind to test this...

Please add the following to your Gemfile:

gem 'yell', :git =>  'git://github.com/rudionrails/yell.git'
gem 'yell-adapters-gelf', :git => 'git://github.com/rudionrails/yell-adapters-gelf.git'

I have not published those in gem format yet, that's why you need to get the latest git source. Once you've updated the sources you may use Yell like so:

logger = Yell.new :gelf

logger.info "Hello World"
# Sends usual attribtues the GELF: 
#   { "short_message" => "Hello World" }

logger.error StandardError.new( "Ths is an error" )
# Along with the other attributes, the gelf adapter converts Exceptions into: 
#   { "short_message" => "StandardError: This is an error", 
#     "long_message" => "back\ntrace" }

logger.info "Hello World", "_user_id" => current_user.id
# Sends the usual attributes to GELF, accompanied by: 
#   { "short_message" => "Hello World", "_user_id" => 123 }

Please let me know if this works for you and I will release a new gem version.

P.S. I am not adding transactions or some sort of scopes. I believe this would totally go into the wrong direction like ActiveSupport logger scopes - they are just wrong. However, if you strongly feel about this to being something you want for your project, please write an addition that sits on top of yell. If that gets some tracton, I might reconsider adding that to the core.

vkoivula commented 12 years ago

Hi,

In yell-adapters-gelf.gemspec

s.add_runtime_dependency "yell", "0.9.0"

So installing with bundler does not work.

rudionrails commented 12 years ago

I'm at a different computer now and you are right, the source is not updated. Looks like I did the changes, but forgot to actually push the code for the adapter. I did, however, push it for he core gem I believe. So, I apologize and will update the source as soon as I am back on my laptop this evening.

rudionrails commented 12 years ago

Alright, pushed it...

rudionrails commented 12 years ago

I've released a new gem version of the gelf adapter with the above mentioned changes. If there are additional amendmepts, please file a new issue.

vkoivula commented 12 years ago

I will test these next week and probably replace my own wrapper with this.