lib/ougai/logger.rb in ougai-1.1.0 vs lib/ougai/logger.rb in ougai-1.2.0

- old
+ new

@@ -1,55 +1,19 @@ -require 'ougai/formatters/bunyan' -require 'logger' - module Ougai class Logger < ::Logger - attr_accessor :default_message, :exc_key, :with_fields + include Logging + attr_accessor :default_message, :exc_key + def initialize(*args) super(*args) @default_message = 'No message' @exc_key = :err @with_fields = {} @formatter = create_formatter end - def debug(message = nil, ex = nil, data = nil, &block) - return true if level > DEBUG - args = block ? yield : [message, ex, data] - add(DEBUG, build_log(args)) - end - - def info(message = nil, ex = nil, data = nil, &block) - return true if level > INFO - args = block ? yield : [message, ex, data] - add(INFO, build_log(args)) - end - - def warn(message = nil, ex = nil, data = nil, &block) - return true if level > WARN - args = block ? yield : [message, ex, data] - add(WARN, build_log(args)) - end - - def error(message = nil, ex = nil, data = nil, &block) - return true if level > ERROR - args = block ? yield : [message, ex, data] - add(ERROR, build_log(args)) - end - - def fatal(message = nil, ex = nil, data = nil, &block) - return true if level > FATAL - args = block ? yield : [message, ex, data] - add(FATAL, build_log(args)) - end - - def unknown(message = nil, ex = nil, data = nil, &block) - args = block ? yield : [message, ex, data] - add(UNKNOWN, build_log(args)) - end - def self.broadcast(logger) Module.new do |mdl| ::Logger::Severity.constants.each do |severity| method_name = severity.downcase.to_sym @@ -59,20 +23,26 @@ end end end end + def chain(severity, args, fields) + write(severity, args, merge_fields(@with_fields, fields)) + end + protected + def append(severity, args) + write(severity, args, @with_fields) + end + def create_formatter Formatters::Bunyan.new end - private - - def build_log(args) - @with_fields.merge(to_item(args)) + def write(severity, args, fields) + add(severity, merge_fields(fields, to_item(args))) end def to_item(args) msg, ex, data = args @@ -84,9 +54,11 @@ create_item_with_3args(msg, ex, data) else # No args { msg: @default_message } end end + + private def create_item_with_1arg(msg) item = {} if msg.is_a?(Exception) item[:msg] = msg.to_s