lib/logstasher.rb in logstasher-0.8.0 vs lib/logstasher.rb in logstasher-0.8.2

- old
+ new

@@ -149,19 +149,37 @@ def custom_fields=(val) Thread.current[:logstasher_custom_fields] = val end - def log(severity, msg) + # Log an arbitrary message. + # + # Usually invoked by the level-based wrapper methods defined below. + # + # Examples + # + # LogStasher.info("message") + # LogStasher.info("message", tags:"tag1") + # LogStasher.info("message", tags:["tag1", "tag2"]) + # LogStasher.info("message", timing:1234) + # LogStasher.info(custom1:"yes", custom2:"no") + def log(severity, message, additional_fields={}) if self.logger && self.logger.send("#{severity}?") + data = {'level' => severity} - if msg.respond_to?(:to_hash) - data.merge!(msg.to_hash) + if message.respond_to?(:to_hash) + data.merge!(message.to_hash) else - data['message'] = msg + data['message'] = message end - self.logger << build_logstash_event(data, ['log']).to_json + "\n" + + # tags get special handling + tags = Array(additional_fields.delete(:tags) || 'log') + + data.merge!(additional_fields) + self.logger << build_logstash_event(data, tags).to_json + "\n" + end end def build_logstash_event(data, tags) ::LogStash::Event.new(data.merge('source' => self.source, 'tags' => tags)) @@ -187,11 +205,11 @@ end end %w( fatal error warn info debug unknown ).each do |severity| eval <<-EOM, nil, __FILE__, __LINE__ + 1 - def #{severity}(msg) - self.log(:#{severity}, msg) + def #{severity}(message=nil, additional_fields={}) + self.log(:#{severity}, message, additional_fields) end EOM end def enabled?