summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/json_logger_spec.rb
blob: 801de357ddcef3d4bdc40e40c65c29ecb989d0c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::JsonLogger do
  subject { described_class.new('/dev/null') }

  let(:now) { Time.now }

  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

  describe '#format_message' do
    before do
      allow(Labkit::Correlation::CorrelationId).to receive(:current_id).and_return('new-correlation-id')
    end

    it 'formats strings' do
      output = subject.format_message('INFO', now, 'test', 'Hello world')
      data = Gitlab::Json.parse(output)

      expect(data['severity']).to eq('INFO')
      expect(data['time']).to eq(now.utc.iso8601(3))
      expect(data['message']).to eq('Hello world')
      expect(data['correlation_id']).to eq('new-correlation-id')
    end

    it 'formats hashes' do
      output = subject.format_message('INFO', now, 'test', { hello: 1 })
      data = Gitlab::Json.parse(output)

      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
      expect(data['correlation_id']).to eq('new-correlation-id')
    end
  end
end