Commit 477f9ed7 authored by Stan Hu's avatar Stan Hu

Bring JsonLogger to CE

This backports a module that was in EE for Geo so that other modules
can have structured logging support.
parent 38736175
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