lib/ougai/formatters/bunyan.rb in ougai-1.6.3 vs lib/ougai/formatters/bunyan.rb in ougai-1.6.4
- old
+ new
@@ -1,18 +1,13 @@
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
@@ -21,12 +16,11 @@
# @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)
+ init_opts_for_json(opts)
end
def _call(severity, time, progname, data)
dump({
name: progname || @app_name,
@@ -36,20 +30,11 @@
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
+ def convert_time(data)
data[:time] = format_datetime(data[:time])
- str = Oj.dump(data, OJ_OPTIONS)
- str << "\n" if @with_newline
- str
end
end
end
end