From dc77e844fe15038edd1cf8836044ba565aa67853 Mon Sep 17 00:00:00 2001 From: "Jon R. Humphrey" Date: Wed, 24 May 2023 11:43:58 +0100 Subject: [PATCH 1/9] feat: re-added `env['action_dispatch.request_id']` After determining the request_id variable was not always being filled in via the `env['HTTP_X_REQUEST_ID']` request, the rails environment variable `env['action_dispatch.request_id']` was re-added to ensure this value was collected and parsed. --- lib/json_rails_logger/request_id_middleware.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/json_rails_logger/request_id_middleware.rb b/lib/json_rails_logger/request_id_middleware.rb index bba549f..a198c6a 100644 --- a/lib/json_rails_logger/request_id_middleware.rb +++ b/lib/json_rails_logger/request_id_middleware.rb @@ -9,7 +9,7 @@ def initialize(app) end def call(env) - request_id = env['HTTP_X_REQUEST_ID'] + request_id = env['HTTP_X_REQUEST_ID'] || env['action_dispatch.request_id'] Thread.current[JsonRailsLogger::REQUEST_ID] = request_id @app.call(env) ensure From ea84ba312632276f88497cc4b22465081bbb0a99 Mon Sep 17 00:00:00 2001 From: "Jon R. Humphrey" Date: Wed, 24 May 2023 11:44:21 +0100 Subject: [PATCH 2/9] chore: reformatted layout only --- lib/json_rails_logger/json_formatter.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/json_rails_logger/json_formatter.rb b/lib/json_rails_logger/json_formatter.rb index 7bf2df5..59c9156 100644 --- a/lib/json_rails_logger/json_formatter.rb +++ b/lib/json_rails_logger/json_formatter.rb @@ -13,8 +13,10 @@ def call(severity, timestamp, _progname, raw_msg) msg = process_message(raw_msg) new_msg = format_message(msg) - payload = { ts: timestp, - level: sev} + payload = { + ts: timestp, + level: sev + } payload.merge!(request_id.to_h) payload.merge!(new_msg.to_h) From 871358069a0a6b5e3d97662921ad3a1bf4d02862 Mon Sep 17 00:00:00 2001 From: "Jon R. Humphrey" Date: Wed, 24 May 2023 11:44:42 +0100 Subject: [PATCH 3/9] build: Incremented version cadence for changes --- lib/json_rails_logger/version.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/json_rails_logger/version.rb b/lib/json_rails_logger/version.rb index 189d76b..6969fd8 100644 --- a/lib/json_rails_logger/version.rb +++ b/lib/json_rails_logger/version.rb @@ -3,7 +3,7 @@ module JsonRailsLogger MAJOR = 0 MINOR = 3 - PATCH = 5 - SUFFIX = 5 + PATCH = 6 + SUFFIX = 0 VERSION = "#{MAJOR}.#{MINOR}.#{PATCH}#{SUFFIX && '.' + SUFFIX.to_s}" end From 957c6d0f24f64cd98c722a22c19826bf8a849cb6 Mon Sep 17 00:00:00 2001 From: "Jon R. Humphrey" Date: Wed, 24 May 2023 11:45:50 +0100 Subject: [PATCH 4/9] docs: Updated CHANGELOG --- CHANGELOG.md | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b99d0fe..fcc0480 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,23 +1,33 @@ # Changelog for the JSON Rails Logger gem +## 0.3.6.0 - 2023-05-24 + +- (Jon) After determining the request_id variable was not always being filled in + via the `env['HTTP_X_REQUEST_ID']` request, the rails environment variable + `env['action_dispatch.request_id']` was re-added to ensure this value was + collected and parsed. + ## 0.3.5.5 - 2023-05-10 -- (Jon) Reverted the `gemspec` file to ensure the gem accepts higher ruby versions -and allows it to work with current app integrations; e.g. regulated products -- (Jon) Reverted the `unit_tests.yml` file to ensure the test accepts higher ruby -versions and allows it to work with current app integrations; e.g. regulated products +- (Jon) Reverted the `gemspec` file to ensure the gem accepts higher ruby +versions and allows it to work with current app integrations; e.g. regulated +products +- (Jon) Reverted the `unit_tests.yml` file to ensure the test accepts higher +ruby versions and allows it to work with current app integrations; e.g. +regulated products ## 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) 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 - 2.6.6 ruby version to reduce any potential issues with current app integrations + 2.6.6 ruby version to reduce any potential issues with current app + integrations - (Jon) Updated the gemspec to ensure the gem is locked to the same 2.6.6 ruby version to reduce any potential issues with current app integrations - (Jon) Removed dependency version locks due to connected apps not supporting @@ -27,8 +37,8 @@ versions and allows it to work with current app integrations; e.g. regulated pro - (Jon) Updated the gemspec to ensure the dependency versions are locked to specific base versions to avoid any potential issues with the gem being used -- (Jon) Added specific versions to the gemspec dependencies to resolve open-ended - dependency warnings when publishing the gem. +- (Jon) Added specific versions to the gemspec dependencies to resolve + open-ended dependency warnings when publishing the gem. ## 0.3.5.1 - 2023-01-16 From e214c37907359de7497dced937d8cefdb5bcfcc2 Mon Sep 17 00:00:00 2001 From: "Jon R. Humphrey" Date: Thu, 25 May 2023 14:15:54 +0100 Subject: [PATCH 5/9] refactor: adjusted use of env var Now only uses the `env['action_dispatch.request_id']` variable to mimic the `HTTP_X_REQUEST_ID` header as the header doesn't exist when running rails server in the development environment. --- lib/json_rails_logger/request_id_middleware.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/json_rails_logger/request_id_middleware.rb b/lib/json_rails_logger/request_id_middleware.rb index a198c6a..8c90283 100644 --- a/lib/json_rails_logger/request_id_middleware.rb +++ b/lib/json_rails_logger/request_id_middleware.rb @@ -9,7 +9,8 @@ def initialize(app) end def call(env) - request_id = env['HTTP_X_REQUEST_ID'] || env['action_dispatch.request_id'] + request_id = env['HTTP_X_REQUEST_ID'] + request_id = env['action_dispatch.request_id'] if Rails.env.development? Thread.current[JsonRailsLogger::REQUEST_ID] = request_id @app.call(env) ensure From efb2b1216df1f3e8751c1419701bede53a900610 Mon Sep 17 00:00:00 2001 From: "Jon R. Humphrey" Date: Thu, 25 May 2023 14:18:09 +0100 Subject: [PATCH 6/9] refactor: adjusted to handle all request methods Reports on `GET, POST, PUT, DELETE, PATCH` request methods instead of only `GET` --- lib/json_rails_logger/json_formatter.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/json_rails_logger/json_formatter.rb b/lib/json_rails_logger/json_formatter.rb index 59c9156..f4f51f7 100644 --- a/lib/json_rails_logger/json_formatter.rb +++ b/lib/json_rails_logger/json_formatter.rb @@ -7,6 +7,8 @@ class JsonFormatter < ::Logger::Formatter method path status duration user_agent accept request_id ].freeze + REQUEST_METHODS = %w[GET, POST, PUT, DELETE, PATCH].freeze + def call(severity, timestamp, _progname, raw_msg) sev = process_severity(severity) timestp = process_timestamp(timestamp) @@ -45,7 +47,7 @@ def process_message(raw_msg) return msg unless msg.is_a?(String) return status_message(msg) if status_message?(msg) - return get_message(msg) if get_message?(msg) + return request_type(msg) if request_type?(msg) return user_agent_message(msg) if user_agent_message?(msg) { message: msg.squish } @@ -95,12 +97,12 @@ def status_message(msg) { status: status } end - def get_message?(msg) + def request_type?(msg) msg.is_a?(String) && - msg.match(/GET http\S+/) + REQUEST_METHODS.any? { |method| msg.match(/#{method} http\S+/) } end - def get_message(msg) + def request_type(msg) splitted_msg = msg.split method = splitted_msg[0] path = splitted_msg[1] From cf3f9628ddd4e537c17faa98850a810f5b87bb1f Mon Sep 17 00:00:00 2001 From: "Jon R. Humphrey" Date: Fri, 2 Jun 2023 09:45:03 +0100 Subject: [PATCH 7/9] chore: adjusted to include common headers While these are not currently used they could be and having them here will make future adjustments simpler by not having to look for them! --- lib/json_rails_logger/constants.rb | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/lib/json_rails_logger/constants.rb b/lib/json_rails_logger/constants.rb index 511718c..847e9f1 100644 --- a/lib/json_rails_logger/constants.rb +++ b/lib/json_rails_logger/constants.rb @@ -2,4 +2,34 @@ module JsonRailsLogger REQUEST_ID = :request_id + # * THE FOLLOWING ARE NOT CURRENTLY USED BUT AVAILABLE FOR FUTURE USE * # + # REQUEST_URI = :request_uri + # REQUEST_METHOD = :request_method + # REQUEST_PATH = :request_path + # SERVER_NAME = :server_name + # STATUS = :status + # DURATION = :duration + # USER_AGENT = :user_agent + # ACCEPT = :accept + # MESSAGE = :message + # EXCEPTION = :exception + # BACKTRACE = :backtrace + # CONTROLLER = :controller + # ACTION = :action + # PARAMS = :params + # IP = :ip + # AUTH = :auth + # HTTP_HOST = :http_host + # HTTP_VERSION = :http_version + # HTTP_REFERER = :http_referer + # HTTP_COOKIE = :http_cookie + # HTTP_ACCEPT_ENCODING = :http_accept_encoding + # HTTP_ACCEPT_LANGUAGE = :http_accept_language + # HTTP_ACCEPT_CHARSET = :http_accept_charset + # HTTP_CACHE_CONTROL = :http_cache_control + # HTTP_CONNECTION = :http_connection + # HTTP_UPGRADE_INSECURE_REQUESTS = :http_upgrade_insecure_requests + # HTTP_ORIGIN = :http_origin + # HTTP_DNT = :http_dnt + # HTTP_X_REQUEST_ID = :http_x_request_id end From 152f26b55917b027eb28a4a98ea82957ac651df2 Mon Sep 17 00:00:00 2001 From: "Jon R. Humphrey" Date: Fri, 2 Jun 2023 09:45:44 +0100 Subject: [PATCH 8/9] docs: Updated the current changes on the CHANGELOG --- CHANGELOG.md | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fcc0480..fb366f4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,11 +1,12 @@ # Changelog for the JSON Rails Logger gem -## 0.3.6.0 - 2023-05-24 +## 0.3.6.0 - 2023-06-01 -- (Jon) After determining the request_id variable was not always being filled in - via the `env['HTTP_X_REQUEST_ID']` request, the rails environment variable - `env['action_dispatch.request_id']` was re-added to ensure this value was - collected and parsed. +- (Jon) Now uses the `env['action_dispatch.request_id']` variable ONLY in the +development environment in order to mimic the `HTTP_X_REQUEST_ID` header as that +header doesn't exist when running rails server in the development environment. +- (Jon) Reports on `GET, POST, PUT, DELETE, PATCH` request methods instead of +only `GET` ## 0.3.5.5 - 2023-05-10 From 8fbabdef4e845a5c628d846b5a2f7ba32f905edb Mon Sep 17 00:00:00 2001 From: "Jon R. Humphrey" Date: Fri, 2 Jun 2023 10:49:33 +0100 Subject: [PATCH 9/9] fix: removed commas from request method list `%w[]` is a non-interpolated array of words, separated by whitespace therefore doesn't need comma separation --- lib/json_rails_logger/json_formatter.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/json_rails_logger/json_formatter.rb b/lib/json_rails_logger/json_formatter.rb index f4f51f7..02d5f18 100644 --- a/lib/json_rails_logger/json_formatter.rb +++ b/lib/json_rails_logger/json_formatter.rb @@ -7,7 +7,7 @@ class JsonFormatter < ::Logger::Formatter method path status duration user_agent accept request_id ].freeze - REQUEST_METHODS = %w[GET, POST, PUT, DELETE, PATCH].freeze + REQUEST_METHODS = %w[GET POST PUT DELETE PATCH].freeze def call(severity, timestamp, _progname, raw_msg) sev = process_severity(severity) @@ -45,7 +45,6 @@ def process_message(raw_msg) msg = normalize_message(raw_msg) return msg unless msg.is_a?(String) - return status_message(msg) if status_message?(msg) return request_type(msg) if request_type?(msg) return user_agent_message(msg) if user_agent_message?(msg) @@ -106,7 +105,6 @@ def request_type(msg) splitted_msg = msg.split method = splitted_msg[0] path = splitted_msg[1] - { method: method, path: path } end