lib/ougai.rb in ougai-0.1.2 vs lib/ougai.rb in ougai-0.2.0

- old
+ new

@@ -4,24 +4,18 @@ require 'time' require 'json' module Ougai class Logger < Logger + attr_accessor :default_message, :app_name + def initialize(*args) super(*args) - run_filename = File.basename($0, ".rb") - hostname = Socket.gethostname - @formatter = proc do |severity, time, progname, data| - JSON.generate({ - name: progname || run_filename, - hostname: hostname, - pid: $$, - level: to_level(severity), - time: time.iso8601(3), - v: 0 - }.merge(data)) + "\n" - end + @default_message = 'No message' + @app_name = File.basename($0, ".rb") + @hostname = Socket.gethostname + @formatter = create_formatter end def debug(message, ex = nil, data = {}) super(to_item(message, ex, data)) end @@ -40,10 +34,25 @@ def fatal(message, ex = nil, data = {}) super(to_item(message, ex, data)) end + protected + + def create_formatter + proc do |severity, time, progname, data| + JSON.generate({ + name: progname || @app_name, + hostname: @hostname, + pid: $$, + level: to_level(severity), + time: time.iso8601(3), + v: 0 + }.merge(data)) + "\n" + end + end + private def to_item(msg, ex, data) item = {} if ex.nil? && msg.is_a?(Exception) @@ -55,9 +64,12 @@ item.merge!(ex) elsif ex.is_a?(Exception) item[:err] = serialize_ex(ex) item.merge!(data) end + elsif msg.is_a?(Hash) + item[:msg] = @default_message unless msg.key?(:msg) + item.merge!(msg) else item[:msg] = msg item.merge!(data) end item