Skip to content

Commit f116a6d

Browse files
Add high priority fields to beginning of JSON object when json format
1 parent 4b948bb commit f116a6d

File tree

3 files changed

+50
-3
lines changed

3 files changed

+50
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
55

66
## [unreleased]
77

8+
- Add high priority fields (timestamp, level, level index and message) to beginning of JSON object output when using JSON formatter to ensure log entries are parsable even when JSON logs are mangled
9+
810
## [4.17.0]
911

1012
- Correct `source_code_uri` URL

lib/semantic_logger/formatters/raw.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,19 +117,19 @@ def call(log, logger)
117117
self.log = log
118118
self.logger = logger
119119

120+
time
121+
level
122+
message
120123
host
121124
application
122125
environment
123-
time
124-
level
125126
pid
126127
thread_name
127128
file_name_and_line
128129
duration
129130
tags
130131
named_tags
131132
name
132-
message
133133
payload
134134
exception
135135
metric

test/formatters/json_test.rb

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
require_relative "../test_helper"
2+
3+
module SemanticLogger
4+
module Formatters
5+
class JsonTest < Minitest::Test
6+
describe Json do
7+
let(:log_time) do
8+
Time.utc(2017, 1, 14, 8, 32, 5.375276)
9+
end
10+
11+
let(:level) do
12+
:debug
13+
end
14+
15+
let(:log) do
16+
log = SemanticLogger::Log.new("JsonTest", level)
17+
log.time = log_time
18+
log
19+
end
20+
21+
let(:expected_time) do
22+
SemanticLogger::Formatters::Base::PRECISION == 3 ? "2017-01-14T08:32:05.375Z" : "2017-01-14T08:32:05.375276Z"
23+
end
24+
25+
let(:formatter) do
26+
formatter = SemanticLogger::Formatters::Json.new(log_host: false)
27+
# Does not use the logger instance for formatting purposes
28+
formatter.call(log, nil)
29+
formatter
30+
end
31+
32+
describe "call" do
33+
it "sets timestamp, level, level_index, and message at the beginning of the JSON object" do
34+
log.message = "Some message"
35+
expected_start = %({"timestamp":"#{expected_time}","level":"debug","level_index":1,"message":"Some message")
36+
37+
is_starting_with_high_priority_fields = formatter.call(log, nil).start_with?(expected_start)
38+
39+
assert is_starting_with_high_priority_fields, "Expected #{formatter.call(log, nil)} to start with #{expected_start}"
40+
end
41+
end
42+
end
43+
end
44+
end
45+
end

0 commit comments

Comments
 (0)