spec/formatters/readable_spec.rb in ougai-1.5.0 vs spec/formatters/readable_spec.rb in ougai-1.5.2

- old
+ new

@@ -1,8 +1,10 @@ require 'spec_helper' describe Ougai::Formatters::Readable do + let!(:re_start_with_datetime) { /^\[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}(Z|[\+\-\:0-9]{4,6})]/ } + let(:data) do { msg: 'Log Message!', status: 200, method: 'GET', @@ -16,66 +18,77 @@ message: 'it is dummy.', stack: "error1.rb\n error2.rb" } end + let(:formatter) { described_class.new } + context 'when severity is TRACE' do - subject { described_class.new.call('TRACE', Time.now, nil, data) } + subject { formatter.call('TRACE', Time.now, nil, data) } it 'includes valid strings' do + expect(subject).to match(re_start_with_datetime) expect(subject).to include("\e[0;34mTRACE\e[0m: Log Message!") expect(subject.gsub(/\e\[([;\d]+)?m/, '')).to include(':status => 200') end end context 'when severity is DEBUG' do - subject { described_class.new.call('DEBUG', Time.now, nil, data) } + subject { formatter.call('DEBUG', Time.now, nil, data) } it 'includes valid strings' do + expect(subject).to match(re_start_with_datetime) expect(subject).to include("\e[0;37mDEBUG\e[0m: Log Message!") expect(subject.gsub(/\e\[([;\d]+)?m/, '')).to include(':status => 200') end end context 'when severity is INFO' do - subject { described_class.new.call('INFO', Time.now, nil, data) } + subject { formatter.call('INFO', Time.now, nil, data) } it 'includes valid strings' do + expect(subject).to match(re_start_with_datetime) expect(subject).to include("\e[0;36mINFO\e[0m: Log Message!") expect(subject.gsub(/\e\[([;\d]+)?m/, '')).to include(':method => "GET"') end end context 'when severity is WARN' do - subject { described_class.new.call('WARN', Time.now, nil, data) } + subject { formatter.call('WARN', Time.now, nil, data) } it 'includes valid strings' do + expect(subject).to match(re_start_with_datetime) expect(subject).to include("\e[0;33mWARN\e[0m: Log Message!") expect(subject.gsub(/\e\[([;\d]+)?m/, '')).to include(':path => "/"') end end context 'when severity is ERROR' do - subject { described_class.new.call('ERROR', Time.now, nil, data.merge({ err: err })) } + subject { formatter.call('ERROR', Time.now, nil, data.merge({ err: err })) } it 'includes valid strings' do + expect(subject).to match(re_start_with_datetime) expect(subject).to include("\e[0;31mERROR\e[0m: Log Message!") expect(subject.gsub(/\e\[([;\d]+)?m/, '')).to include('DummyError (it is dummy.):') end end context 'when severity is FATAL' do - subject { described_class.new.call('FATAL', Time.now, nil, { msg: 'TheEnd', err: err }) } + subject { formatter.call('FATAL', Time.now, nil, { msg: 'TheEnd', err: err }) } + it 'includes valid strings' do + expect(subject).to match(re_start_with_datetime) expect(subject).to include("\e[0;35mFATAL\e[0m: TheEnd") expect(subject.gsub(/\e\[([;\d]+)?m/, '')).to include("error1.rb\n error2.rb") end end context 'when severity is UNKNOWN' do - subject { described_class.new.call('ANY', Time.now, nil, { msg: 'unknown msg' }) } + subject { formatter.call('ANY', Time.now, nil, { msg: 'unknown msg' }) } + it 'includes valid strings' do + expect(subject).to match(re_start_with_datetime) expect(subject).to include("\e[0;32mANY\e[0m: unknown msg") end end context 'when logger has excluded_fields' do @@ -87,8 +100,24 @@ 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 + + describe '#datetime_format' do + subject do + formatter.call('DEBUG', Time.now, nil, data) + end + + context 'is time AM/PM format' do + before do + formatter.datetime_format = '%I:%M:%S %p' + end + + it 'applys output' do + expect(subject).to match(/^\[\d{2}:\d{2}:\d{2} [AP]M\]/) + end end end end