From 13d8b8932772aca8b1195b89e89f54e3f6779650 Mon Sep 17 00:00:00 2001 From: Geoff Wilson Date: Wed, 23 Oct 2024 16:25:48 -0400 Subject: [PATCH] Add json log formatter to standard logger --- app/lib/audit_logger.rb | 2 +- app/lib/{audit_log_formatter.rb => json_log_formatter.rb} | 2 +- config/initializers/logging.rb | 4 ++++ 3 files changed, 6 insertions(+), 2 deletions(-) rename app/lib/{audit_log_formatter.rb => json_log_formatter.rb} (85%) create mode 100644 config/initializers/logging.rb diff --git a/app/lib/audit_logger.rb b/app/lib/audit_logger.rb index 84c3442..788a6cc 100644 --- a/app/lib/audit_logger.rb +++ b/app/lib/audit_logger.rb @@ -1,6 +1,6 @@ class AuditLogger < ActiveSupport::Logger def initialize super(Config[:audit_log_file]) - self.formatter = AuditLogFormatter.new + self.formatter = JsonLogFormatter.new end end diff --git a/app/lib/audit_log_formatter.rb b/app/lib/json_log_formatter.rb similarity index 85% rename from app/lib/audit_log_formatter.rb rename to app/lib/json_log_formatter.rb index 0304ea4..5e759cd 100644 --- a/app/lib/audit_log_formatter.rb +++ b/app/lib/json_log_formatter.rb @@ -1,4 +1,4 @@ -class AuditLogFormatter < ActiveSupport::Logger::SimpleFormatter +class JsonLogFormatter < ActiveSupport::Logger::SimpleFormatter def call(severity, timestamp, _progname, message) # request_id is unique to the life of the api request request_id = Thread.current[:request_id] diff --git a/config/initializers/logging.rb b/config/initializers/logging.rb new file mode 100644 index 0000000..e6e15dd --- /dev/null +++ b/config/initializers/logging.rb @@ -0,0 +1,4 @@ +Rails.application.config.to_prepare do + Rails.logger = ActiveSupport::Logger.new(STDOUT) + Rails.logger.formatter = JsonLogFormatter.new +end