Sha256: b8caeab77de13839c5ccb7943ba5935c7798d29ed67fc426b30541bfde16fd2d

Contents?: true

Size: 1.92 KB

Versions: 1

Compression:

Stored size: 1.92 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
    # @attr [Boolean] with_newline Whether tailing NL should be appended
    class Bunyan < Base
      include ForJson

      attr_accessor :jsonize, :with_newline

      # Intialize a formatter
      # @param [String] app_name application name (execution program name if nil)
      # @param [String] hostname hostname (hostname if nil)
      # @param [Hash] opts the initial values of attributes
      # @option opts [String] :trace_indent (2) the value of trace_indent attribute
      # @option opts [String] :trace_max_lines (100) the value of trace_max_lines attribute
      # @option opts [String] :serialize_backtrace (true) the value of serialize_backtrace attribute
      # @option opts [String] :jsonize (true) the value of jsonize attribute
      # @option opts [String] :with_newline (true) the value of with_newline attribute
      def initialize(app_name = nil, hostname = nil, opts = {})
        aname, hname, opts = Base.parse_new_params([app_name, hostname, opts])
        super(aname, hname, opts)
        @jsonize = opts.fetch(:jsonize, true)
        @with_newline = opts.fetch(: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

      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.3 lib/ougai/formatters/bunyan.rb