Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).

## [unreleased]

- 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

## [4.17.0]

- Correct `source_code_uri` URL
Expand Down
6 changes: 3 additions & 3 deletions lib/semantic_logger/formatters/raw.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,19 +117,19 @@ def call(log, logger)
self.log = log
self.logger = logger

time
level
message
host
application
environment
time
level
pid
thread_name
file_name_and_line
duration
tags
named_tags
name
message
payload
exception
metric
Expand Down
45 changes: 45 additions & 0 deletions test/formatters/json_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
require_relative "../test_helper"

module SemanticLogger
module Formatters
class JsonTest < Minitest::Test
describe Json do
let(:log_time) do
Time.utc(2017, 1, 14, 8, 32, 5.375276)
end

let(:level) do
:debug
end

let(:log) do
log = SemanticLogger::Log.new("JsonTest", level)
log.time = log_time
log
end

let(:expected_time) do
SemanticLogger::Formatters::Base::PRECISION == 3 ? "2017-01-14T08:32:05.375Z" : "2017-01-14T08:32:05.375276Z"
end

let(:formatter) do
formatter = SemanticLogger::Formatters::Json.new(log_host: false)
# Does not use the logger instance for formatting purposes
formatter.call(log, nil)
formatter
end

describe "call" do
it "sets timestamp, level, level_index, and message at the beginning of the JSON object" do
log.message = "Some message"
expected_start = %({"timestamp":"#{expected_time}","level":"debug","level_index":1,"message":"Some message")

is_starting_with_high_priority_fields = formatter.call(log, nil).start_with?(expected_start)

assert is_starting_with_high_priority_fields, "Expected #{formatter.call(log, nil)} to start with #{expected_start}"
end
end
end
end
end
end