Commit 334ee107 authored by Robert Speicher's avatar Robert Speicher

Merge branch 'sh-bring-json-logger-to-ce' into 'master'

Bring JsonLogger to CE

See merge request gitlab-org/gitlab-ce!20755
parents 2ec7b518 477f9ed7
module Gitlab
class JsonLogger < ::Gitlab::Logger
def self.file_name_noext
raise NotImplementedError
end
def format_message(severity, timestamp, progname, message)
data = {}
data[:severity] = severity
data[:time] = timestamp.utc.iso8601(3)
case message
when String
data[:message] = message
when Hash
data.merge!(message)
end
data.to_json + "\n"
end
end
end
# coding: utf-8
require 'spec_helper'
describe Gitlab::JsonLogger do
subject { described_class.new('/dev/null') }
let(:now) { Time.now }
describe '#format_message' do
it 'formats strings' do
output = subject.format_message('INFO', now, 'test', 'Hello world')
data = 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')
end
it 'formats hashes' do
output = subject.format_message('INFO', now, 'test', { hello: 1 })
data = 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
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment