Sha256: cef898143afbda0859eeb0732c958cd801d70292160f3b0f0d1e1266b7fc5957

Contents?: true

Size: 1.41 KB

Versions: 1

Compression:

Stored size: 1.41 KB

Contents

require 'ougai/formatters/base'
require 'oj'

module Ougai
  module Formatters
    # A JSON formatter compatible with node-bunyan
    # @attr [Boolean] jsonize Whether log should converts JSON (by default this is on).
    # @attr [Boolean] with_newline Whether tailing NL should be appended (by default this is on).
    class Bunyan < Base
      attr_accessor :jsonize, :with_newline

      def initialize(*args)
        super(*args)
        @jsonize = true
        @with_newline = true
      end

      def _call(severity, time, progname, data)
        dump({
          name: progname || @app_name,
          hostname: @hostname,
          pid: $$,
          level: to_level(severity),
          time: time,
          v: 0
        }.merge(data))
      end

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

      private

      OJ_OPTIONS = { mode: :custom, time_format: :xmlschema,
                     use_as_json: true, use_to_hash: true, use_to_json: true }

      def dump(data)
        return data unless @jsonize
        data[:time] = format_datetime(data[:time])
        str = Oj.dump(data, OJ_OPTIONS)
        str << "\n" if @with_newline
        str
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ougai-1.6.1 lib/ougai/formatters/bunyan.rb