lib/ougai/formatters/pino.rb in ougai-1.6.3 vs lib/ougai/formatters/pino.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 pino - # @attr [Boolean] jsonize Whether log should converts JSON - # @attr [Boolean] with_newline Whether tailing NL should be appended class Pino < 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 (4) 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) @trace_indent = opts.fetch(:trace_indent, 4) @serialize_backtrace = true end def datetime_format=(val) @@ -43,12 +37,10 @@ time: time, v: 1 }.merge(data)) end - private - def flat_err(data) return unless data.key?(:err) err = data.delete(:err) msg = err[:message] data[:type] ||= 'Error' @@ -56,18 +48,11 @@ stack = "#{err[:name]}: #{msg}" stack += "\n" + (" " * @trace_indent) + err[:stack] if err.key?(:stack) data[:stack] ||= stack end - 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] = data[:time].to_i * 1000 - str = Oj.dump(data, OJ_OPTIONS) - str << "\n" if @with_newline - str + def convert_time(data) + data[:time] = (data[:time].to_f * 1000).to_i end end end end