2019-12-26 22:10:19 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe Gitlab::JsonLogger do
|
2018-11-18 11:00:15 +05:30
|
|
|
subject { described_class.new('/dev/null') }
|
|
|
|
|
|
|
|
let(:now) { Time.now }
|
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
describe '#file_name' do
|
|
|
|
let(:subclass) do
|
|
|
|
Class.new(Gitlab::JsonLogger) do
|
|
|
|
def self.file_name_noext
|
|
|
|
'testlogger'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises error when file_name_noext not implemented' do
|
|
|
|
expect { described_class.file_name }.to raise_error(
|
|
|
|
'JsonLogger implementations must provide file_name_noext implementation'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns log file name when file_name_noext is implemented' do
|
|
|
|
expect(subclass.file_name).to eq('testlogger.log')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
describe '#format_message' do
|
2019-02-15 15:39:39 +05:30
|
|
|
before do
|
2019-07-31 22:56:46 +05:30
|
|
|
allow(Labkit::Correlation::CorrelationId).to receive(:current_id).and_return('new-correlation-id')
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
it 'formats strings' do
|
|
|
|
output = subject.format_message('INFO', now, 'test', 'Hello world')
|
2020-05-24 23:13:21 +05:30
|
|
|
data = Gitlab::Json.parse(output)
|
2018-11-18 11:00:15 +05:30
|
|
|
|
|
|
|
expect(data['severity']).to eq('INFO')
|
|
|
|
expect(data['time']).to eq(now.utc.iso8601(3))
|
|
|
|
expect(data['message']).to eq('Hello world')
|
2019-02-15 15:39:39 +05:30
|
|
|
expect(data['correlation_id']).to eq('new-correlation-id')
|
2018-11-18 11:00:15 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'formats hashes' do
|
|
|
|
output = subject.format_message('INFO', now, 'test', { hello: 1 })
|
2020-05-24 23:13:21 +05:30
|
|
|
data = Gitlab::Json.parse(output)
|
2018-11-18 11:00:15 +05:30
|
|
|
|
|
|
|
expect(data['severity']).to eq('INFO')
|
|
|
|
expect(data['time']).to eq(now.utc.iso8601(3))
|
|
|
|
expect(data['hello']).to eq(1)
|
|
|
|
expect(data['message']).to be_nil
|
2019-02-15 15:39:39 +05:30
|
|
|
expect(data['correlation_id']).to eq('new-correlation-id')
|
2018-11-18 11:00:15 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|