From b07298d48a06a0a7e6e076458f3fcce4ca03b7c3 Mon Sep 17 00:00:00 2001 From: Toshimitsu Takahashi Date: Mon, 24 Jul 2017 12:43:59 +0900 Subject: [PATCH 1/3] Add excluded_fields to Readble of formatters --- lib/ougai/formatters/readable.rb | 17 ++++++++++++----- spec/formatters/readable_spec.rb | 14 ++++++++++++++ 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/lib/ougai/formatters/readable.rb b/lib/ougai/formatters/readable.rb index 68cdf81..f8b57f6 100644 --- a/lib/ougai/formatters/readable.rb +++ b/lib/ougai/formatters/readable.rb @@ -3,12 +3,13 @@ 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 @@ -16,12 +17,10 @@ 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 @@ -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 diff --git a/spec/formatters/readable_spec.rb b/spec/formatters/readable_spec.rb index 2889aa5..e0d3376 100644 --- a/spec/formatters/readable_spec.rb +++ b/spec/formatters/readable_spec.rb @@ -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 From f13421866beb54a7f75b3ae4ea69c2e3ec4524ee Mon Sep 17 00:00:00 2001 From: Toshimitsu Takahashi Date: Mon, 24 Jul 2017 12:48:42 +0900 Subject: [PATCH 2/3] Bump 1.1.0 --- lib/ougai/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ougai/version.rb b/lib/ougai/version.rb index e129fe0..3113c81 100644 --- a/lib/ougai/version.rb +++ b/lib/ougai/version.rb @@ -1,3 +1,3 @@ module Ougai - VERSION = "1.0.0" + VERSION = "1.1.0" end From b228988263299ac2ddfa6345c63f3918c19a7c12 Mon Sep 17 00:00:00 2001 From: Toshimitsu Takahashi Date: Mon, 24 Jul 2017 12:53:28 +0900 Subject: [PATCH 3/3] Init with_fields before create_formatter Because with_fields can be set in create_formatter of sub-class --- lib/ougai/logger.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ougai/logger.rb b/lib/ougai/logger.rb index b30f190..09f11af 100644 --- a/lib/ougai/logger.rb +++ b/lib/ougai/logger.rb @@ -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)