From 518ed359e8eeb331ae53cf2adb622fc40f140d7a Mon Sep 17 00:00:00 2001 From: "Jon R. Humphrey" Date: Wed, 16 Oct 2024 15:03:47 +0100 Subject: [PATCH 01/18] Auto stash before merge of "spike/update-publish-action" and "origin/main" --- .github/workflows/unit-tests.yml | 24 ++++++----- Gemfile.lock | 2 +- Makefile | 10 ++--- lib/json_rails_logger/constants.rb | 1 + lib/json_rails_logger/json_formatter.rb | 54 ++++++++++++++++++++++--- 5 files changed, 71 insertions(+), 20 deletions(-) diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml index 8ba329e..561e52f 100644 --- a/.github/workflows/unit-tests.yml +++ b/.github/workflows/unit-tests.yml @@ -6,12 +6,18 @@ jobs: test: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 - - name: Set up Ruby - uses: ruby/setup-ruby@v1 - with: - ruby-version: 2.6 - - name: Install dependencies (bundle) - run: bundle install - - name: Run tests - run: rake test + - name: Checkout code + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Set up Ruby + uses: ruby/setup-ruby@v1 + with: + ruby-version: 2.6 + + - name: Install dependencies (bundle) + run: bundle install + + - name: Run tests + run: rake test diff --git a/Gemfile.lock b/Gemfile.lock index de2f2a4..4072651 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -79,7 +79,7 @@ GEM PLATFORMS ruby x86_64-darwin-21 - x86_64-linux + x86_64-darwin-23 DEPENDENCIES json_rails_logger! diff --git a/Makefile b/Makefile index b3e45b9..3b1f78f 100644 --- a/Makefile +++ b/Makefile @@ -12,10 +12,6 @@ SPEC=${NAME}.gemspec all: publish -auth: ${AUTH} - -build: gem - ${AUTH}: @mkdir -p ${HOME}/.gem @echo '---' > ${AUTH} @@ -25,12 +21,16 @@ ${AUTH}: ${GEM}: ${SPEC} ./lib/${NAME}/version.rb gem build ${SPEC} +auth: ${AUTH} + +build: gem + gem: ${GEM} @echo ${GEM} test: gem @bundle install - @rake test + @bundle exec rake test publish: ${AUTH} ${GEM} @echo Publishing package ${NAME}:${VERSION} to ${OWNER} ... diff --git a/lib/json_rails_logger/constants.rb b/lib/json_rails_logger/constants.rb index 847e9f1..85709bd 100644 --- a/lib/json_rails_logger/constants.rb +++ b/lib/json_rails_logger/constants.rb @@ -2,6 +2,7 @@ module JsonRailsLogger REQUEST_ID = :request_id + QUERY_STRING = :query_string # * THE FOLLOWING ARE NOT CURRENTLY USED BUT AVAILABLE FOR FUTURE USE * # # REQUEST_URI = :request_uri # REQUEST_METHOD = :request_method diff --git a/lib/json_rails_logger/json_formatter.rb b/lib/json_rails_logger/json_formatter.rb index d1ffe0a..faff62f 100644 --- a/lib/json_rails_logger/json_formatter.rb +++ b/lib/json_rails_logger/json_formatter.rb @@ -5,11 +5,43 @@ module JsonRailsLogger class JsonFormatter < ::Logger::Formatter ## Required keys to be logged to the output REQUIRED_KEYS = %w[ - method path status duration user_agent accept request_id request_url message query_string + accept + action + backtrace + body + cache_control + charset + connection + controller + cookies + duration + encoding + forwarded_for + gateway + host + keep_alive + language + message + method + path + query_string + referrer + remote + request_id + request_status + request_uri + request_url + server_name + server_port + server_protocol + server_software + status + user_agent + view ].freeze ## Optional keys to be ignored from the output for the time being - OPTIONAL_KEYS = %w[format controller action view exception exception_object].freeze + OPTIONAL_KEYS = %w[format exception exception_object].freeze ## Request methods to check for in the message REQUEST_METHODS = %w[GET POST PUT DELETE PATCH].freeze @@ -26,6 +58,7 @@ def call(severity, timestamp, _progname, raw_msg) level: sev } + payload.merge!(query_string.to_h) payload.merge!(request_id.to_h) payload.merge!(new_msg.to_h.except!(:optional).compact) @@ -50,6 +83,15 @@ def request_id { request_id: request_id } if request_id end + def format_datetime(time) + time.strftime('%Y-%m-%dT%H:%M:%S.%6N') + end + + def query_string + query_string = Thread.current[JsonRailsLogger::QUERY_STRING] + { query_string: query_string } if query_string + end + def process_message(raw_msg) msg = normalize_message(raw_msg) @@ -69,13 +111,15 @@ def format_message(msg) return new_msg.merge(msg) unless msg.is_a?(Enumerable) + # If the message is a hash, check if it contains the required keys split_msg = msg.partition { |k, _v| REQUIRED_KEYS.include?(k.to_s) }.map(&:to_h) + # If the returned hash is empty, check if the message is a hash with optional keys if split_msg[0].empty? split_msg = msg.partition do |k, _v| OPTIONAL_KEYS.exclude?(k.to_s) end.map(&:to_h) end - + # Check if the message contains a duration key and normalise it split_msg[0] = normalise_duration(split_msg[0]) if includes_duration?(split_msg[0]) new_msg.merge!(split_msg[0]) @@ -139,9 +183,9 @@ def includes_duration?(msg) msg.key?('duration') end - # If duration is a float, convert it to an integer as microseconds + # If duration is a float, convert it to an integer as milliseconds µs -> ms def normalise_duration(msg) - msg.to_h { |k, v| k.to_s == 'duration' && v.is_a?(Float) ? [k, (v * 1000).round(0)] : [k, v] } + msg.to_h { |k, v| k.to_s == 'duration' && v.is_a?(Float) ? [k, v.round(0)] : [k, v] } end end end From c91a9189a38020efe28398b93e0cbb48293f4063 Mon Sep 17 00:00:00 2001 From: "Jon R. Humphrey" Date: Wed, 16 Oct 2024 15:35:30 +0100 Subject: [PATCH 02/18] fix: merge conflict --- Gemfile.lock | 4 ++++ lib/json_rails_logger/version.rb | 7 +++++++ 2 files changed, 11 insertions(+) diff --git a/Gemfile.lock b/Gemfile.lock index 4072651..3b07d6f 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,11 @@ PATH remote: . specs: +<<<<<<< Updated upstream json_rails_logger (1.1.0) +======= + json_rails_logger (1.0.4.pre.rc01) +>>>>>>> Stashed changes json (~> 2.0) lograge (~> 0.11) railties (~> 6.0) diff --git a/lib/json_rails_logger/version.rb b/lib/json_rails_logger/version.rb index 291eb00..b8a2803 100644 --- a/lib/json_rails_logger/version.rb +++ b/lib/json_rails_logger/version.rb @@ -2,8 +2,15 @@ module JsonRailsLogger MAJOR = 1 +<<<<<<< Updated upstream MINOR = 1 PATCH = 0 SUFFIX = nil VERSION = "#{MAJOR}.#{MINOR}.#{PATCH}#{SUFFIX && "-#{SUFFIX}"}" +======= + MINOR = 0 + PATCH = 4 + SUFFIX = '-rc01' + VERSION = "#{MAJOR}.#{MINOR}.#{PATCH}#{SUFFIX && "#{SUFFIX}"}" +>>>>>>> Stashed changes end From 94b7738e7337db62b9579683c5c54757a94a5fb9 Mon Sep 17 00:00:00 2001 From: "Jon R. Humphrey" Date: Wed, 16 Oct 2024 15:38:27 +0100 Subject: [PATCH 03/18] Auto stash before merge of "spike/update-publish-action" and "origin/main" --- Gemfile.lock | 4 ---- lib/json_rails_logger/version.rb | 7 ------- 2 files changed, 11 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 3b07d6f..4072651 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,11 +1,7 @@ PATH remote: . specs: -<<<<<<< Updated upstream json_rails_logger (1.1.0) -======= - json_rails_logger (1.0.4.pre.rc01) ->>>>>>> Stashed changes json (~> 2.0) lograge (~> 0.11) railties (~> 6.0) diff --git a/lib/json_rails_logger/version.rb b/lib/json_rails_logger/version.rb index b8a2803..291eb00 100644 --- a/lib/json_rails_logger/version.rb +++ b/lib/json_rails_logger/version.rb @@ -2,15 +2,8 @@ module JsonRailsLogger MAJOR = 1 -<<<<<<< Updated upstream MINOR = 1 PATCH = 0 SUFFIX = nil VERSION = "#{MAJOR}.#{MINOR}.#{PATCH}#{SUFFIX && "-#{SUFFIX}"}" -======= - MINOR = 0 - PATCH = 4 - SUFFIX = '-rc01' - VERSION = "#{MAJOR}.#{MINOR}.#{PATCH}#{SUFFIX && "#{SUFFIX}"}" ->>>>>>> Stashed changes end From 96adeb378ec3557d2bf43922cdf03b24a0bb9242 Mon Sep 17 00:00:00 2001 From: "Jon R. Humphrey" Date: Fri, 25 Oct 2024 13:00:18 +0100 Subject: [PATCH 04/18] refactor: update required keys Although they're not actually "required" this updates these keys to later use if required --- lib/json_rails_logger/json_formatter.rb | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/lib/json_rails_logger/json_formatter.rb b/lib/json_rails_logger/json_formatter.rb index faff62f..81e9a8c 100644 --- a/lib/json_rails_logger/json_formatter.rb +++ b/lib/json_rails_logger/json_formatter.rb @@ -9,25 +9,29 @@ class JsonFormatter < ::Logger::Formatter action backtrace body - cache_control - charset - connection controller - cookies duration encoding forwarded_for gateway - host + http_accept_charset + http_accept_encoding + http_accept_language + http_cache_control + http_charset + http_cookie + http_connection + http_host + http_origin + http_referer keep_alive - language message method - path query_string - referrer - remote + remote_addr request_id + request_params + request_path request_status request_uri request_url @@ -37,11 +41,10 @@ class JsonFormatter < ::Logger::Formatter server_software status user_agent - view ].freeze ## Optional keys to be ignored from the output for the time being - OPTIONAL_KEYS = %w[format exception exception_object].freeze + OPTIONAL_KEYS = %w[format exception exception_object view].freeze ## Request methods to check for in the message REQUEST_METHODS = %w[GET POST PUT DELETE PATCH].freeze From b536e65fd10ba7c6786c029b6e8f7828146e542e Mon Sep 17 00:00:00 2001 From: "Jon R. Humphrey" Date: Fri, 25 Oct 2024 13:03:23 +0100 Subject: [PATCH 05/18] refactor: updated constants This exposes these "keys" to any application using the gem; further adjustments forthcoming --- lib/json_rails_logger/constants.rb | 55 +++++++++++++++++------------- 1 file changed, 32 insertions(+), 23 deletions(-) diff --git a/lib/json_rails_logger/constants.rb b/lib/json_rails_logger/constants.rb index 85709bd..d05781c 100644 --- a/lib/json_rails_logger/constants.rb +++ b/lib/json_rails_logger/constants.rb @@ -1,36 +1,45 @@ # frozen_string_literal: true module JsonRailsLogger - REQUEST_ID = :request_id + ACCEPT = :accept + BACKTRACE = :backtrace + BODY = :body + DURATION = :duration + EXCEPTION = :exception + GATEWAY = :gateway + HTTP_HOST = :http_host + HTTP_ORIGIN = :http_origin + HTTP_REFERER = :http_referer + MESSAGE = :message + METHOD = :method QUERY_STRING = :query_string - # * 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 + REMOTE_ADDR = :remote_addr + REQUEST_ID = :request_id + REQUEST_PARAMS = :request_params + REQUEST_PATH = :request_path + REQUEST_STATUS = :request_status + REQUEST_URI = :request_uri + SERVER_NAME = :server_name + SERVER_PORT = :server_port + SERVER_PROTOCOL = :server_protocol + SERVER_SOFTWARE = :server_software + STATUS = :status + USER_AGENT = :user_agent + # * THE FOLLOWING ARE NOT CURRENTLY USED BUT AVAILABLE FOR USE * # # ACTION = :action - # PARAMS = :params - # IP = :ip # AUTH = :auth - # HTTP_HOST = :http_host - # HTTP_VERSION = :http_version - # HTTP_REFERER = :http_referer - # HTTP_COOKIE = :http_cookie + # CONTROLLER = :controller + # FORWARDED_FOR = :forwarded_for + # HTTP_ACCEPT_CHARSET = :http_accept_charset # 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_COOKIE = :http_cookie # HTTP_CONNECTION = :http_connection - # HTTP_UPGRADE_INSECURE_REQUESTS = :http_upgrade_insecure_requests - # HTTP_ORIGIN = :http_origin # HTTP_DNT = :http_dnt + # HTTP_UPGRADE_INSECURE_REQUESTS = :http_upgrade_insecure_requests + # HTTP_VERSION = :http_version # HTTP_X_REQUEST_ID = :http_x_request_id + # IP = :ip + # REQUEST_METHOD = :request_method end From 97126f0a6d95fadd01bf62265622f8a3e394a6e0 Mon Sep 17 00:00:00 2001 From: "Jon R. Humphrey" Date: Fri, 25 Oct 2024 13:19:41 +0100 Subject: [PATCH 06/18] fix: removes surplus keys too much content is included in the log, needs refinement down the line --- lib/json_rails_logger/constants.rb | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/json_rails_logger/constants.rb b/lib/json_rails_logger/constants.rb index d05781c..8c01206 100644 --- a/lib/json_rails_logger/constants.rb +++ b/lib/json_rails_logger/constants.rb @@ -1,35 +1,26 @@ # frozen_string_literal: true module JsonRailsLogger - ACCEPT = :accept BACKTRACE = :backtrace - BODY = :body DURATION = :duration EXCEPTION = :exception - GATEWAY = :gateway - HTTP_HOST = :http_host - HTTP_ORIGIN = :http_origin - HTTP_REFERER = :http_referer MESSAGE = :message METHOD = :method QUERY_STRING = :query_string - REMOTE_ADDR = :remote_addr REQUEST_ID = :request_id REQUEST_PARAMS = :request_params REQUEST_PATH = :request_path REQUEST_STATUS = :request_status REQUEST_URI = :request_uri - SERVER_NAME = :server_name - SERVER_PORT = :server_port - SERVER_PROTOCOL = :server_protocol - SERVER_SOFTWARE = :server_software STATUS = :status - USER_AGENT = :user_agent # * THE FOLLOWING ARE NOT CURRENTLY USED BUT AVAILABLE FOR USE * # + # ACCEPT = :accept # ACTION = :action # AUTH = :auth + # BODY = :body # CONTROLLER = :controller # FORWARDED_FOR = :forwarded_for + # GATEWAY = :gateway # HTTP_ACCEPT_CHARSET = :http_accept_charset # HTTP_ACCEPT_ENCODING = :http_accept_encoding # HTTP_ACCEPT_LANGUAGE = :http_accept_language @@ -37,9 +28,18 @@ module JsonRailsLogger # HTTP_COOKIE = :http_cookie # HTTP_CONNECTION = :http_connection # HTTP_DNT = :http_dnt + # HTTP_HOST = :http_host + # HTTP_ORIGIN = :http_origin + # HTTP_REFERER = :http_referer # HTTP_UPGRADE_INSECURE_REQUESTS = :http_upgrade_insecure_requests # HTTP_VERSION = :http_version # HTTP_X_REQUEST_ID = :http_x_request_id # IP = :ip + # REMOTE_ADDR = :remote_addr # REQUEST_METHOD = :request_method + # SERVER_NAME = :server_name + # SERVER_PORT = :server_port + # SERVER_PROTOCOL = :server_protocol + # SERVER_SOFTWARE = :server_software + # USER_AGENT = :user_agent end From 9d86e95d8709f48866394cc866f49ccb133010ac Mon Sep 17 00:00:00 2001 From: "Jon R. Humphrey" Date: Fri, 25 Oct 2024 13:27:21 +0100 Subject: [PATCH 07/18] ci: introduces publish action as workflow step also removes merged unit-tests action as initial step in workflow --- .github/workflows/unit-tests.yml | 23 ---------- .../workflows/{publish.yml => workflow.yml} | 45 +++++++++++++++++-- 2 files changed, 42 insertions(+), 26 deletions(-) delete mode 100644 .github/workflows/unit-tests.yml rename .github/workflows/{publish.yml => workflow.yml} (52%) diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml deleted file mode 100644 index 561e52f..0000000 --- a/.github/workflows/unit-tests.yml +++ /dev/null @@ -1,23 +0,0 @@ -name: Unit Tests - -on: [push, pull_request] - -jobs: - test: - runs-on: ubuntu-latest - steps: - - name: Checkout code - uses: actions/checkout@v4 - with: - fetch-depth: 0 - - - name: Set up Ruby - uses: ruby/setup-ruby@v1 - with: - ruby-version: 2.6 - - - name: Install dependencies (bundle) - run: bundle install - - - name: Run tests - run: rake test diff --git a/.github/workflows/publish.yml b/.github/workflows/workflow.yml similarity index 52% rename from .github/workflows/publish.yml rename to .github/workflows/workflow.yml index e4d353a..d79575d 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/workflow.yml @@ -1,10 +1,49 @@ -name: Release and Publish Gem +name: Workflow + on: + push: + branches-ignore: + - 'issue/**' + - 'task/**' + - 'spike/**' + paths-ignore: + - README.md + - CHANGELOG.md + - .gitignore + workflow_dispatch: {} -#---- Below this line should not require editing ---- jobs: - build: + + Check: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: "Record desired ruby version" + id: ruby + shell: bash + run: | + [ -f .ruby-version ] && echo version=$(cat .ruby-version) | tee -a $GITHUB_OUTPUT || echo "version=" >> $GITHUB_OUTPUT + + - name: "Setup ruby" + if: steps.ruby.outputs.version != '' + uses: ruby/setup-ruby@v1 + with: + ruby-version: ${{ steps.ruby.outputs.version }} + + - name: Install dependencies (bundle) + run: bundle install + + - name: Run checks + run: | + PAT=${{ secrets.EPI_GPR_READ_ACCESS_TOKEN }} make lint test + + Publish: + needs: Check name: "Build gem package" runs-on: ubuntu-latest steps: From 9cd04c48b0480451d022b0e76e6c9d0e7fb4fe06 Mon Sep 17 00:00:00 2001 From: "Jon R. Humphrey" Date: Fri, 25 Oct 2024 13:27:48 +0100 Subject: [PATCH 08/18] build: increment version patch cadence --- lib/json_rails_logger/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/json_rails_logger/version.rb b/lib/json_rails_logger/version.rb index 291eb00..30f49fd 100644 --- a/lib/json_rails_logger/version.rb +++ b/lib/json_rails_logger/version.rb @@ -3,7 +3,7 @@ module JsonRailsLogger MAJOR = 1 MINOR = 1 - PATCH = 0 + PATCH = 1 SUFFIX = nil VERSION = "#{MAJOR}.#{MINOR}.#{PATCH}#{SUFFIX && "-#{SUFFIX}"}" end From e4ad78da0a6775c3cfc9c26722d9ccff9c6786e4 Mon Sep 17 00:00:00 2001 From: "Jon R. Humphrey" Date: Fri, 25 Oct 2024 13:28:09 +0100 Subject: [PATCH 09/18] docs: Updated README --- CHANGELOG.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eb7fa98..20bf3f0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,15 @@ # Changelog for the JSON Rails Logger gem +## 1.1.1 - 2024-10 + +- (Jon) Updated the exposed keys to allow more flexibility in the logging +- (Jon) Merged the two GitHub actions into one to reduce the number of actions + required to maintain the gem, while introducing dependency on successful linting + and tests before the gem is published + ## 1.1.0 - 2024-10 -(Dan) Updates ruby to 2.7.8 and version cadence to 1.1.0 +- (Dan) Updates ruby to 2.7.8 and version cadence to 1.1.0 ## 1.0.4-rc01 - 2023-08 From e3d77c51019be6a5db1ebc6cfe36079f6b13f2d2 Mon Sep 17 00:00:00 2001 From: "Jon R. Humphrey" Date: Fri, 25 Oct 2024 13:28:29 +0100 Subject: [PATCH 10/18] build: added lint target to makefile --- Makefile | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Makefile b/Makefile index 3b1f78f..4964c57 100644 --- a/Makefile +++ b/Makefile @@ -25,6 +25,10 @@ auth: ${AUTH} build: gem +lint: gem + @echo "Running rubocop..." + @bundle exec rubocop + gem: ${GEM} @echo ${GEM} From 59bdc20066ee435f7ddfdaa22003f2ab9ee9967a Mon Sep 17 00:00:00 2001 From: "Jon R. Humphrey" Date: Fri, 25 Oct 2024 14:22:24 +0100 Subject: [PATCH 11/18] revert: removes combined workflow restores individual test and publish workflows, albeit with improved ruby-version detection in the test workflow --- .../workflows/{workflow.yml => publish.yml} | 45 ++----------------- .github/workflows/unit-tests.yml | 31 +++++++++++++ 2 files changed, 34 insertions(+), 42 deletions(-) rename .github/workflows/{workflow.yml => publish.yml} (52%) create mode 100644 .github/workflows/unit-tests.yml diff --git a/.github/workflows/workflow.yml b/.github/workflows/publish.yml similarity index 52% rename from .github/workflows/workflow.yml rename to .github/workflows/publish.yml index d79575d..e4d353a 100644 --- a/.github/workflows/workflow.yml +++ b/.github/workflows/publish.yml @@ -1,49 +1,10 @@ -name: Workflow - +name: Release and Publish Gem on: - push: - branches-ignore: - - 'issue/**' - - 'task/**' - - 'spike/**' - paths-ignore: - - README.md - - CHANGELOG.md - - .gitignore - workflow_dispatch: {} +#---- Below this line should not require editing ---- jobs: - - Check: - runs-on: ubuntu-latest - steps: - - name: Checkout code - uses: actions/checkout@v4 - with: - fetch-depth: 0 - - - name: "Record desired ruby version" - id: ruby - shell: bash - run: | - [ -f .ruby-version ] && echo version=$(cat .ruby-version) | tee -a $GITHUB_OUTPUT || echo "version=" >> $GITHUB_OUTPUT - - - name: "Setup ruby" - if: steps.ruby.outputs.version != '' - uses: ruby/setup-ruby@v1 - with: - ruby-version: ${{ steps.ruby.outputs.version }} - - - name: Install dependencies (bundle) - run: bundle install - - - name: Run checks - run: | - PAT=${{ secrets.EPI_GPR_READ_ACCESS_TOKEN }} make lint test - - Publish: - needs: Check + build: name: "Build gem package" runs-on: ubuntu-latest steps: diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml new file mode 100644 index 0000000..896ba2e --- /dev/null +++ b/.github/workflows/unit-tests.yml @@ -0,0 +1,31 @@ +name: Unit Tests + +on: [push, pull_request] + +jobs: + test: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: "Record desired ruby version" + id: ruby + shell: bash + run: | + [ -f .ruby-version ] && echo version=$(cat .ruby-version) | tee -a $GITHUB_OUTPUT || echo "version=" >> $GITHUB_OUTPUT + + - name: "Setup ruby" + if: steps.ruby.outputs.version != '' + uses: ruby/setup-ruby@v1 + with: + ruby-version: ${{ steps.ruby.outputs.version }} + + - name: Install dependencies (bundle) + run: bundle install + + - name: Run checks + run: | + PAT=${{ secrets.EPI_GPR_READ_ACCESS_TOKEN }} make lint test From 67f4f5d9681c70904a3e72223977f78fc9a61e77 Mon Sep 17 00:00:00 2001 From: "Jon R. Humphrey" Date: Fri, 25 Oct 2024 14:40:58 +0100 Subject: [PATCH 12/18] test: include rubocop as dev dependency --- json_rails_logger.gemspec | 1 + 1 file changed, 1 insertion(+) diff --git a/json_rails_logger.gemspec b/json_rails_logger.gemspec index 1cedde0..ac910ed 100644 --- a/json_rails_logger.gemspec +++ b/json_rails_logger.gemspec @@ -32,4 +32,5 @@ Gem::Specification.new do |spec| spec.add_runtime_dependency 'railties', '~> 6.0' spec.add_development_dependency 'rake', '~> 13.0' + spec.add_development_dependency 'rubocop', '~> 1.25.0' end From 82b249c45187326b16e3a108dbd4ea0a9d7aa704 Mon Sep 17 00:00:00 2001 From: "Jon R. Humphrey" Date: Fri, 25 Oct 2024 14:41:52 +0100 Subject: [PATCH 13/18] style: add rubocop disable for warnings Metrics/ClassLength and Metrics/AbcSize --- lib/json_rails_logger/json_formatter.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/json_rails_logger/json_formatter.rb b/lib/json_rails_logger/json_formatter.rb index 81e9a8c..39ddc84 100644 --- a/lib/json_rails_logger/json_formatter.rb +++ b/lib/json_rails_logger/json_formatter.rb @@ -2,7 +2,7 @@ module JsonRailsLogger # This class is the json formatter for our logger - class JsonFormatter < ::Logger::Formatter + class JsonFormatter < ::Logger::Formatter # rubocop:disable Metrics/ClassLength ## Required keys to be logged to the output REQUIRED_KEYS = %w[ accept @@ -50,7 +50,7 @@ class JsonFormatter < ::Logger::Formatter REQUEST_METHODS = %w[GET POST PUT DELETE PATCH].freeze # rubocop:disable Metrics/MethodLength - def call(severity, timestamp, _progname, raw_msg) + def call(severity, timestamp, _progname, raw_msg) # rubocop:disable Metrics/AbcSize sev = process_severity(severity) timestp = process_timestamp(timestamp) msg = process_message(raw_msg) From 4ea50e87fbfa8f310ac54e554877f1c36b7838d3 Mon Sep 17 00:00:00 2001 From: "Jon R. Humphrey" Date: Fri, 25 Oct 2024 14:42:44 +0100 Subject: [PATCH 14/18] revert: removes erroneous `format_datetime` method in favour of original formatting helper --- lib/json_rails_logger/json_formatter.rb | 4 ---- 1 file changed, 4 deletions(-) diff --git a/lib/json_rails_logger/json_formatter.rb b/lib/json_rails_logger/json_formatter.rb index 39ddc84..7c7be3f 100644 --- a/lib/json_rails_logger/json_formatter.rb +++ b/lib/json_rails_logger/json_formatter.rb @@ -86,10 +86,6 @@ def request_id { request_id: request_id } if request_id end - def format_datetime(time) - time.strftime('%Y-%m-%dT%H:%M:%S.%6N') - end - def query_string query_string = Thread.current[JsonRailsLogger::QUERY_STRING] { query_string: query_string } if query_string From bc8f2b28a9897c6895f02fff173814935f640d4e Mon Sep 17 00:00:00 2001 From: "Jon R. Humphrey" Date: Fri, 25 Oct 2024 14:43:23 +0100 Subject: [PATCH 15/18] test: resolve millisecond conversion in results as we're now passing in duration as milliseconds --- test/formatter_test.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/formatter_test.rb b/test/formatter_test.rb index b102e8a..ba4c5db 100644 --- a/test/formatter_test.rb +++ b/test/formatter_test.rb @@ -92,13 +92,13 @@ end end - it 'should correctly format a millisecond duration into microseconds' do + it 'should correctly format a microsecond duration into milliseconds' do message = '{"duration": 1234567.89}' log_output = fixture.call('INFO', timestamp, progname, message) _(log_output).must_be_kind_of(String) json_output = JSON.parse(log_output) - _(json_output['duration']).must_equal(1_234_567_890) + _(json_output['duration']).must_equal(1_234_568) end end From a9968fcb71d68d10518c5253ad7b1c1995765e1a Mon Sep 17 00:00:00 2001 From: "Jon R. Humphrey" Date: Fri, 25 Oct 2024 14:46:10 +0100 Subject: [PATCH 16/18] fix: resolve `PATH` as alias for `:request_path` as well as restores `path` to required keys list --- lib/json_rails_logger/constants.rb | 1 + lib/json_rails_logger/json_formatter.rb | 1 + 2 files changed, 2 insertions(+) diff --git a/lib/json_rails_logger/constants.rb b/lib/json_rails_logger/constants.rb index 8c01206..5be4f54 100644 --- a/lib/json_rails_logger/constants.rb +++ b/lib/json_rails_logger/constants.rb @@ -6,6 +6,7 @@ module JsonRailsLogger EXCEPTION = :exception MESSAGE = :message METHOD = :method + PATH = :request_path # Alias for REQUEST_PATH QUERY_STRING = :query_string REQUEST_ID = :request_id REQUEST_PARAMS = :request_params diff --git a/lib/json_rails_logger/json_formatter.rb b/lib/json_rails_logger/json_formatter.rb index 7c7be3f..b988885 100644 --- a/lib/json_rails_logger/json_formatter.rb +++ b/lib/json_rails_logger/json_formatter.rb @@ -27,6 +27,7 @@ class JsonFormatter < ::Logger::Formatter # rubocop:disable Metrics/ClassLength keep_alive message method + path query_string remote_addr request_id From b5ae02dcd1c23a6a97caa32a234d1db9a237a8ea Mon Sep 17 00:00:00 2001 From: "Jon R. Humphrey" Date: Fri, 25 Oct 2024 14:46:43 +0100 Subject: [PATCH 17/18] build: include bundler call in lint target --- Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/Makefile b/Makefile index 4964c57..81fb43e 100644 --- a/Makefile +++ b/Makefile @@ -26,6 +26,7 @@ auth: ${AUTH} build: gem lint: gem + @bundle install @echo "Running rubocop..." @bundle exec rubocop From 3c13a215f37f3b127a66830a4c53471d348e704c Mon Sep 17 00:00:00 2001 From: "Jon R. Humphrey" Date: Fri, 25 Oct 2024 14:47:13 +0100 Subject: [PATCH 18/18] build: adds rubocop and dependencies to lock file --- Gemfile.lock | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 4072651..d84edde 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - json_rails_logger (1.1.0) + json_rails_logger (1.1.1) json (~> 2.0) lograge (~> 0.11) railties (~> 6.0) @@ -28,6 +28,7 @@ GEM minitest (>= 5.1) tzinfo (~> 2.0) zeitwerk (~> 2.3) + ast (2.4.2) builder (3.2.4) concurrent-ruby (1.2.2) crass (1.0.6) @@ -51,8 +52,10 @@ GEM racc (~> 1.4) nokogiri (1.13.10-x86_64-darwin) racc (~> 1.4) - nokogiri (1.13.10-x86_64-linux) - racc (~> 1.4) + parallel (1.26.3) + parser (3.3.5.0) + ast (~> 2.4.1) + racc racc (1.6.2) rack (2.2.6.3) rack-test (2.0.2) @@ -68,12 +71,28 @@ GEM method_source rake (>= 12.2) thor (~> 1.0) + rainbow (3.1.1) rake (13.0.6) + regexp_parser (2.9.2) request_store (1.5.1) rack (>= 1.4) + rexml (3.3.9) + rubocop (1.25.1) + parallel (~> 1.10) + parser (>= 3.1.0.0) + rainbow (>= 2.2.2, < 4.0) + regexp_parser (>= 1.8, < 3.0) + rexml + rubocop-ast (>= 1.15.1, < 2.0) + ruby-progressbar (~> 1.7) + unicode-display_width (>= 1.4.0, < 3.0) + rubocop-ast (1.32.3) + parser (>= 3.3.1.0) + ruby-progressbar (1.13.0) thor (1.2.1) tzinfo (2.0.6) concurrent-ruby (~> 1.0) + unicode-display_width (2.6.0) zeitwerk (2.6.7) PLATFORMS @@ -84,6 +103,7 @@ PLATFORMS DEPENDENCIES json_rails_logger! rake (~> 13.0) + rubocop (~> 1.25.0) BUNDLED WITH - 2.4.22 + 2.4.4