summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJacob Vosmaer <jacob@gitlab.com>2018-03-15 11:28:38 +0100
committerJacob Vosmaer <jacob@gitlab.com>2018-03-15 11:28:38 +0100
commitb46eccf0e6be0e6621806c1062dc452dec9d5645 (patch)
tree396a2c30719075b4c2dfadb7dfae200968f84933
parentbf46dfe316a7d03e90489f0ab95440fb9039f725 (diff)
downloadgitlab-shell-b46eccf0e6be0e6621806c1062dc452dec9d5645.tar.gz
Emulate logrus quoting rules
-rw-r--r--lib/gitlab_logger.rb6
-rw-r--r--spec/gitlab_logger_spec.rb30
2 files changed, 20 insertions, 16 deletions
diff --git a/lib/gitlab_logger.rb b/lib/gitlab_logger.rb
index ecc3dfd..eba592c 100644
--- a/lib/gitlab_logger.rb
+++ b/lib/gitlab_logger.rb
@@ -13,6 +13,10 @@ rescue NameError
end
class GitlabLogger
+ # Emulate the quoting logic of logrus
+ # https://github.com/sirupsen/logrus/blob/v1.0.5/text_formatter.go#L143-L156
+ SHOULD_QUOTE = /[^a-zA-Z0-9\-._\/@^+]/
+
LEVELS = {
Logger::INFO => 'info'.freeze,
Logger::DEBUG => 'debug'.freeze,
@@ -81,7 +85,7 @@ class GitlabLogger
def format_key_value(key, value)
value_string = value.to_s
- value_string = value_string.inspect unless %i[level pid].include?(key)
+ value_string = value_string.inspect if SHOULD_QUOTE =~ value_string
"#{key}=#{value_string}"
end
diff --git a/spec/gitlab_logger_spec.rb b/spec/gitlab_logger_spec.rb
index 4c48b80..48e3623 100644
--- a/spec/gitlab_logger_spec.rb
+++ b/spec/gitlab_logger_spec.rb
@@ -33,16 +33,16 @@ describe GitlabLogger do
let(:level) { Logger::ERROR }
it 'does nothing' do
- subject.info('hello')
+ subject.info('hello world')
expect(output.string).to eq('')
end
end
it 'logs data' do
- subject.info('hello', foo: 'bar')
+ subject.info('hello world', foo: 'bar')
- expect(first_line).to eq('time="1973-11-29T21:33:09+00:00" level=info msg="hello" foo="bar" pid=1234')
+ expect(first_line).to eq('time="1973-11-29T21:33:09+00:00" level=info msg="hello world" foo=bar pid=1234')
end
end
@@ -51,22 +51,22 @@ describe GitlabLogger do
let(:level) { Logger::ERROR }
it 'does nothing' do
- subject.warn('hello')
+ subject.warn('hello world')
expect(output.string).to eq('')
end
end
it 'logs data' do
- subject.warn('hello', foo: 'bar')
+ subject.warn('hello world', foo: 'bar')
- expect(first_line).to eq('time="1973-11-29T21:33:09+00:00" level=warn msg="hello" foo="bar" pid=1234')
+ expect(first_line).to eq('time="1973-11-29T21:33:09+00:00" level=warn msg="hello world" foo=bar pid=1234')
end
end
describe '#debug' do
it 'does nothing' do
- subject.debug('hello')
+ subject.debug('hello world')
expect(output.string).to eq('')
end
@@ -75,9 +75,9 @@ describe GitlabLogger do
let(:level) { Logger::DEBUG }
it 'logs data' do
- subject.debug('hello', foo: 'bar')
+ subject.debug('hello world', foo: 'bar')
- expect(first_line).to eq('time="1973-11-29T21:33:09+00:00" level=debug msg="hello" foo="bar" pid=1234')
+ expect(first_line).to eq('time="1973-11-29T21:33:09+00:00" level=debug msg="hello world" foo=bar pid=1234')
end
end
end
@@ -86,14 +86,14 @@ describe GitlabLogger do
let(:format) { 'json' }
it 'writes valid JSON data' do
- subject.info('hello', foo: 'bar')
+ subject.info('hello world', foo: 'bar')
expect(JSON.parse(first_line)).to eq(
- "foo" => "bar",
- "level" => "info",
- "msg" => "hello",
- "pid" => 1234,
- "time" => "1973-11-29T21:33:09+00:00"
+ 'foo' => 'bar',
+ 'level' => 'info',
+ 'msg' => 'hello world',
+ 'pid' => 1234,
+ 'time' => '1973-11-29T21:33:09+00:00'
)
end