Skip to content

Commit

Permalink
Merge pull request #22 from tilfin/feature/readable-formatter-excluded
Browse files Browse the repository at this point in the history
Add excluded_fields to Formatters::Readble
  • Loading branch information
tilfin authored Jul 24, 2017
2 parents 7f5c729 + b228988 commit c8aa76c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
17 changes: 12 additions & 5 deletions lib/ougai/formatters/readable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,24 @@
module Ougai
module Formatters
class Readable < Base
attr_accessor :plain, :trace_indent
attr_accessor :plain, :trace_indent, :excluded_fields

def initialize(opts = {})
super(opts[:app_name], opts[:hostname])
@trace_indent = opts[:trace_indent] || 4
@plain = opts[:plain] || false
@excluded_fields = opts[:excluded_fields] || []
load_awesome_print
end

def call(severity, time, progname, data)
msg = data.delete(:msg)
level = @plain ? severity : colored_level(severity)
strs = ["[#{time.iso8601(3)}] #{level}: #{msg}"]
if data.key?(:err)
err = data.delete(:err)
err_str = " #{err[:name]} (#{err[:message]}):"
err_str += "\n " + err[:stack] if err.key?(:stack)
if err_str = create_err_str(data)
strs.push(err_str)
end
@excluded_fields.each { |f| data.delete(f) }
unless data.empty?
strs.push(data.ai({ plain: @plain }))
end
Expand All @@ -44,6 +43,14 @@ def colored_level(severity)
"\e[#{color}m#{severity}\e[0m"
end

def create_err_str(data)
return nil unless data.key?(:err)
err = data.delete(:err)
err_str = " #{err[:name]} (#{err[:message]}):"
err_str += "\n " + err[:stack] if err.key?(:stack)
err_str
end

private

def load_awesome_print
Expand Down
2 changes: 1 addition & 1 deletion lib/ougai/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ def initialize(*args)
super(*args)
@default_message = 'No message'
@exc_key = :err
@formatter = create_formatter
@with_fields = {}
@formatter = create_formatter
end

def debug(message = nil, ex = nil, data = nil, &block)
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.0.0"
VERSION = "1.1.0"
end
14 changes: 14 additions & 0 deletions spec/formatters/readable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,18 @@
expect(subject.gsub(/\e\[([;\d]+)?m/, '')).to include("error1.rb\n error2.rb")
end
end

context 'when logger has excluded_fields' do
subject do
described_class.new(excluded_fields: [:status, :method]).call('DEBUG', Time.now, nil, data)
end

it 'includes valid strings' do
expect(subject).to include("\e[0;37mDEBUG\e[0m: Log Message!")
plain_subject = subject.gsub(/\e\[([;\d]+)?m/, '')
expect(plain_subject).to include(':path => "/"')
expect(plain_subject).not_to include(':status => 200')
expect(plain_subject).not_to include(':method => "GET"')
end
end
end

0 comments on commit c8aa76c

Please sign in to comment.