Sha256: d4d7df74beb3a01df6909e8e9567804fd406f464b22d2794f898f37928b2c887

Contents?: true

Size: 1.87 KB

Versions: 1

Compression:

Stored size: 1.87 KB

Contents

require "ougai/version"
require 'logger'
require 'socket'
require 'time'
require 'json'

module Ougai
  class Logger < Logger
    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
    end

    def debug(message, ex = nil, data = {})
      super(to_item(message, ex, data))
    end

    def info(message, ex = nil, data = {})
      super(to_item(message, ex, data))
    end

    def warn(message, ex = nil, data = {})
      super(to_item(message, ex, data))
    end

    def error(message, ex = nil, data = {})
      super(to_item(message, ex, data))
    end

    def fatal(message, ex = nil, data = {})
      super(to_item(message, ex, data))
    end

    private

    def to_item(msg, ex, data)
      item = {}
      if ex.nil? && msg.is_a?(Exception)
        item[:msg] = msg.to_s
        item[:err] = serialize_ex(msg)
      elsif ex
        item[:msg] = msg
        if ex.is_a?(Hash)
          item.merge!(ex)
        elsif ex.is_a?(Exception)
          item[:err] = serialize_ex(ex)
          item.merge!(data)
        end
      else
        item[:msg] = msg
        item.merge!(data)
      end
      item
    end

    def serialize_ex(ex)
      err = {
        name: ex.class.name,
        message: ex.to_s
      }
      if ex.backtrace
        err[:stack] = ex.backtrace.join("\n  ")
      end
      err
    end

    def to_level(severity)
      case severity
      when 'INFO'
        30
      when 'WARN'
        40
      when 'ERROR'
        50
      when 'FATAL'
        60
      else # DEBUG
        20
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ougai-0.1.2 lib/ougai.rb