Skip to content

Commit

Permalink
Merge pull request #46 from epimorphics/spike/adjust-json-output
Browse files Browse the repository at this point in the history
Adjusted JSON Output
  • Loading branch information
jonrandahl authored Mar 24, 2023
2 parents d4cdb93 + 5c1fe6c commit 4686969
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog for the JSON Rails Logger gem

## 0.3.5.4 - 2023-03-24

- (Jon) Removed the rails specific properties from the JSON output as they are not
required by the logging monitors and consume more disk space than desired.
- (Jon) Reordered the Timestamp and Level properties to match the order of other
logs on the Epimorphics system tooling.

## 0.3.5.3 - 2023-03-10

- (Jon) Added .ruby-version file to ensure the gem is locked to the specific
Expand Down
13 changes: 6 additions & 7 deletions lib/json_rails_logger/json_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ module JsonRailsLogger
# This class is the json formatter for our logger
class JsonFormatter < ::Logger::Formatter
COMMON_KEYS = %w[
method path status duration
method path status duration user_agent accept request_id
].freeze

def call(severity, timestamp, _progname, raw_msg)
def call(severity, timestamp, progname, raw_msg)
sev = process_severity(severity)
timestp = process_timestamp(timestamp)
msg = process_message(raw_msg)
new_msg = format_message(msg)

payload = { level: sev,
ts: timestp }
payload = { ts: timestp,
level: sev}

payload.merge!(request_id.to_h)
payload.merge!(new_msg.to_h)
Expand Down Expand Up @@ -51,7 +51,7 @@ def process_message(raw_msg)

# rubocop:disable Metrics/AbcSize
def format_message(msg)
new_msg = { rails: { environment: ::Rails.env } }
new_msg = {}

return msg.merge(new_msg) if string_message_field?(msg)

Expand All @@ -61,7 +61,6 @@ def format_message(msg)
split_msg[0] = normalise_duration(split_msg[0]) if includes_duration?(split_msg[0])

new_msg.merge!(split_msg[0])
new_msg[:rails].merge!(split_msg[1])

new_msg
end
Expand Down Expand Up @@ -107,7 +106,7 @@ def get_message(msg)

def user_agent_message?(msg)
msg.is_a?(String) &&
msg.match(/User-Agent: .+Accept: .+/m)
msg.match(/User-Agent: .[\S\s]+Accept: .+/m)
end

def user_agent_message(msg)
Expand Down
2 changes: 1 addition & 1 deletion lib/json_rails_logger/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module JsonRailsLogger
MAJOR = 0
MINOR = 3
PATCH = 5
SUFFIX = 3
SUFFIX = 4
VERSION = "#{MAJOR}.#{MINOR}.#{PATCH}#{SUFFIX && '.' + SUFFIX.to_s}"

end
5 changes: 3 additions & 2 deletions test/formatter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@
_(log_output).must_be_kind_of(String)

json_output = JSON.parse(log_output)
_(json_output['rails']['user_agent']).must_equal('Faraday v1.3.0')
_(json_output['rails']['accept']).must_equal('application/json')

_(json_output['user_agent']).must_equal('Faraday v1.3.0')
_(json_output['accept']).must_equal('application/json')
end

it 'should correctly format the timestamp' do
Expand Down

0 comments on commit 4686969

Please sign in to comment.