Skip to content

Commit

Permalink
Merge pull request #39 from tilfin/feature/readable-formatter
Browse files Browse the repository at this point in the history
Update Readable formatter extendable easily
  • Loading branch information
tilfin authored Sep 21, 2017
2 parents 0ac14b1 + 5857672 commit 92ac81c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
29 changes: 19 additions & 10 deletions lib/ougai/formatters/readable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,26 @@ def initialize(opts = {})
@trace_indent = opts[:trace_indent] || 4
@plain = opts[:plain] || false
@excluded_fields = opts[:excluded_fields] || []
load_awesome_print
load_dependent
end

def call(severity, time, progname, data)
msg = data.delete(:msg)
level = @plain ? severity : colored_level(severity)
strs = ["[#{format_datetime(time)}] #{level}: #{msg}"]
if err_str = create_err_str(data)
strs.push(err_str)
end
dt = format_datetime(time)
err_str = create_err_str(data)

@excluded_fields.each { |f| data.delete(f) }
unless data.empty?
strs.push(data.ai({ plain: @plain }))
end
data_str = create_data_str(data)
format_log_parts(dt, level, msg, err_str, data_str)
end

protected

def format_log_parts(datetime, level, msg, err, data)
strs = ["[#{datetime}] #{level}: #{msg}"]
strs.push(err) if err
strs.push(data) if data
strs.join("\n") + "\n"
end

Expand Down Expand Up @@ -59,9 +65,12 @@ def create_err_str(data)
err_str
end

private
def create_data_str(data)
return nil if data.empty?
data.ai({ plain: @plain })
end

def load_awesome_print
def load_dependent
require 'awesome_print'
rescue LoadError
puts 'You must install the awesome_print gem to use this output.'
Expand Down
2 changes: 1 addition & 1 deletion lib/ougai/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Ougai
VERSION = "1.5.3"
VERSION = "1.5.4"
end

0 comments on commit 92ac81c

Please sign in to comment.