diff options
author | Robert Speicher <robert@gitlab.com> | 2018-07-21 00:49:08 +0300 |
---|---|---|
committer | Robert Speicher <robert@gitlab.com> | 2018-07-21 00:49:08 +0300 |
commit | 334ee10727540c8e16d47075781564fd21e8a9cb (patch) | |
tree | 97c0d2bf76b9ba2a5551086b9334126a66310247 | |
parent | 2ec7b5182de8716876438786dc30d19f114eb878 (diff) | |
parent | 477f9ed78f8a50afe8ca824436ab7c0b4475e930 (diff) |
Merge branch 'sh-bring-json-logger-to-ce' into 'master'
Bring JsonLogger to CE
See merge request gitlab-org/gitlab-ce!20755
-rw-r--r-- | lib/gitlab/json_logger.rb | 22 | ||||
-rw-r--r-- | spec/lib/gitlab/json_logger_spec.rb | 29 |
2 files changed, 51 insertions, 0 deletions
diff --git a/lib/gitlab/json_logger.rb b/lib/gitlab/json_logger.rb new file mode 100644 index 00000000000..28e258196ca --- /dev/null +++ b/lib/gitlab/json_logger.rb @@ -0,0 +1,22 @@ +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 diff --git a/spec/lib/gitlab/json_logger_spec.rb b/spec/lib/gitlab/json_logger_spec.rb new file mode 100644 index 00000000000..0a62785f880 --- /dev/null +++ b/spec/lib/gitlab/json_logger_spec.rb @@ -0,0 +1,29 @@ +# 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 |