spec/formatters/pino_spec.rb in ougai-1.6.3 vs spec/formatters/pino_spec.rb in ougai-1.6.4

- old
+ new

@@ -8,130 +8,156 @@ method: 'GET', path: '/' } end + let(:stack) { "error1.rb\n error2.rb" } + let(:err) do { name: 'DummyError', message: 'it is dummy.', - stack: "error1.rb\n error2.rb" + stack: stack } end let(:formatter) { described_class.new } - context '#initialize' do - let(:appname) { 'dummy app name' } + include_examples 'formatter#initialize', + default_opts: { + trace_indent: 4, + trace_max_lines: 100, + serialize_backtrace: true, + jsonize: true, + with_newline: true + }, + options: { + jsonize: false, + with_newline: false + } - it 'suceeds with arguments' do - fmt = described_class.new(appname) - expect(fmt.app_name).to eq(appname) - end - end + describe '#call' do + let!(:time_epoc_msec) { 1518710101026 } - context 'jsonize is true and with_newline is true' do - subject { formatter.call('DEBUG', Time.now, nil, data) } + before { Timecop.freeze(Time.at(time_epoc_msec / 1000.0)) } + after { Timecop.return } - it 'includes valid strings' do - expect(subject).to end_with("\n") - result = JSON.parse(subject.chomp, symbolize_names: true) - expect(result).to include(data.merge(level: 20)) - expect(result[:time]).to be > 1518710101026 - end - end + subject { formatter.call(severity, Time.now, nil, data) } - context 'jsonize is false' do - before do - formatter.jsonize = false + context 'jsonize is true and with_newline is true' do + let!(:severity) { 'DEBUG' } + + it 'includes valid strings' do + expect(subject).to end_with("\n") + result = JSON.parse(subject.chomp, symbolize_names: true) + expect(result).to include(data.merge(pid: $$, level: 20, time: time_epoc_msec, v: 1)) + end end - context 'when severity is TRACE' do - subject { formatter.call('TRACE', Time.now, nil, data) } + context 'jsonize is false' do + let!(:time) { Time.at(time_epoc_msec / 1000.0) } - it 'includes valid hash' do - expect(subject).to include(data.merge(level: 10)) - expect(subject[:time]).to be_an_instance_of(Time) + before do + formatter.jsonize = false end - end - context 'when severity is DEBUG' do - subject { formatter.call('DEBUG', Time.now, nil, data) } + context 'when severity is TRACE' do + let!(:severity) { 'TRACE' } - it 'includes valid hash' do - expect(subject).to include(data.merge(level: 20)) - expect(subject[:time]).to be_an_instance_of(Time) + it 'includes valid hash' do + expect(subject).to include(data.merge(pid: $$, level: 10, time: time, v: 1)) + end end - end - context 'when severity is INFO' do - subject { formatter.call('INFO', Time.now, nil, data) } + context 'when severity is DEBUG' do + let!(:severity) { 'DEBUG' } - it 'includes valid hash' do - expect(subject).to include(data.merge(level: 30)) - expect(subject[:time]).to be_an_instance_of(Time) + it 'includes valid hash' do + expect(subject).to include(data.merge(pid: $$, level: 20, time: time, v: 1)) + end end - end - context 'when severity is WARN' do - subject { formatter.call('WARN', Time.now, nil, data) } + context 'when severity is INFO' do + let!(:severity) { 'INFO' } - it 'includes valid hash' do - expect(subject).to include(data.merge(level: 40)) - expect(subject[:time]).to be_an_instance_of(Time) + it 'includes valid hash' do + expect(subject).to include(data.merge(pid: $$, level: 30, time: time, v: 1)) + end end - end - context 'when severity is ERROR' do - subject { - data.delete(:msg) - formatter.call('ERROR', Time.now, nil, data.merge({ err: err })) - } + context 'when severity is WARN' do + let!(:severity) { 'WARN' } - it 'includes valid hash' do - expect(subject).to include({ - level: 50, type: 'Error', - msg: 'it is dummy.', - stack: "DummyError: it is dummy.\n error1.rb\n error2.rb" - }) - expect(subject[:time]).to be_an_instance_of(Time) + it 'includes valid hash' do + expect(subject).to include(data.merge(pid: $$, level: 40, time: time, v: 1)) + end end - end - context 'when severity is FATAL' do - subject { formatter.call('FATAL', Time.now, nil, { msg: 'TheEnd', err: err }) } + context 'when severity is ERROR' do + let!(:severity) { 'ERROR' } - it 'includes valid hash' do - expect(subject).to include({ - level: 60, type: 'Error', - msg: 'TheEnd', - stack: "DummyError: it is dummy.\n error1.rb\n error2.rb" - }) - expect(subject[:time]).to be_an_instance_of(Time) + before do + data.delete(:msg) + data.merge!({ err: err }) + end + + it 'includes valid hash' do + expect(subject).to include({ + pid: $$, level: 50, time: time, v: 1, + type: 'Error', + msg: 'it is dummy.', + stack: "DummyError: it is dummy.\n #{stack}" + }) + end end - end - context 'when severity is UNKNOWN' do - subject { formatter.call('ANY', Time.now, nil, { msg: 'unknown msg' }) } + context 'when severity is FATAL and trace_indent = 2' do + let!(:severity) { 'FATAL' } - it 'includes valid hash' do - expect(subject).to include(level: 70, msg: 'unknown msg') + let!(:data) do + { msg: 'TheEnd', err: err } + end + + before do + formatter.trace_indent = 2 + stack.gsub!(/ /, ' ') + end + + it 'includes valid hash' do + expect(subject).to include({ + pid: $$, level: 60, time: time, v: 1, + type: 'Error', + msg: 'TheEnd', + stack: "DummyError: it is dummy.\n #{stack}", + }) + end end - end - end - context 'with_newline is false' do - before do - formatter.with_newline = false + context 'when severity is UNKNOWN' do + let!(:severity) { 'ANY' } + + let!(:data) do + { msg: 'unknown msg' } + end + + it 'includes valid hash' do + expect(subject).to include(pid: $$, level: 70, time: time, msg: 'unknown msg') + end + end end - subject { formatter.call('INFO', Time.now, nil, data) } + context 'with_newline is false' do + let!(:severity) { 'INFO' } - it 'includes valid strings' do - expect(subject).not_to end_with("\n") - result = JSON.parse(subject, symbolize_names: true) - expect(result).to include(data.merge(level: 30)) - expect(result[:time]).to be > 1518710101026 + before do + formatter.with_newline = false + end + + it 'includes valid strings' do + expect(subject).not_to end_with("\n") + result = JSON.parse(subject, symbolize_names: true) + expect(result).to include(data.merge(level: 30, time: time_epoc_msec)) + end end end describe '#datetime_format' do it 'is not supported' do