diff --git a/app/jobs/reports/base_report.rb b/app/jobs/reports/base_report.rb index bb60c335e98..e15bacb4143 100644 --- a/app/jobs/reports/base_report.rb +++ b/app/jobs/reports/base_report.rb @@ -46,10 +46,6 @@ def save_report(report_name, body, extension:) upload_file_to_s3_timestamped_and_latest(report_name, body, extension) end - def reports_logger - @reports_logger ||= ActiveSupport::Logger.new(Rails.root.join('log', 'reports.log')) - end - def upload_file_to_s3_timestamped_and_latest(report_name, body, extension) latest_path, path = generate_s3_paths(report_name, extension) content_type = Mime::Type.lookup_by_extension(extension).to_s diff --git a/app/services/encryption/kms_logger.rb b/app/services/encryption/kms_logger.rb index 75cd8766fee..0f469171ab6 100644 --- a/app/services/encryption/kms_logger.rb +++ b/app/services/encryption/kms_logger.rb @@ -1,6 +1,5 @@ module Encryption class KmsLogger - LOG_FILENAME = 'kms.log' def self.log(action, key_id:, context: nil) output = { kms: { @@ -8,17 +7,14 @@ def self.log(action, key_id:, context: nil) encryption_context: context, key_id: key_id, }, - log_filename: LOG_FILENAME, + log_filename: Idp::Constants::KMS_LOG_FILENAME, } + logger.info(output.to_json) end def self.logger - @logger ||= if FeatureManagement.log_to_stdout? - Logger.new(STDOUT) - else - Logger.new(Rails.root.join('log', LOG_FILENAME)) - end + Rails.application.config.kms_logger end end end diff --git a/config/application.rb b/config/application.rb index ab9a8086545..37404951725 100644 --- a/config/application.rb +++ b/config/application.rb @@ -9,6 +9,7 @@ require_relative '../lib/asset_sources' require_relative '../lib/identity_config' +require_relative '../lib/feature_management' require_relative '../lib/fingerprinter' require_relative '../lib/identity_job_log_subscriber' require_relative '../lib/email_delivery_observer' @@ -59,9 +60,31 @@ class Application < Rails::Application config.active_job.queue_adapter = :good_job FileUtils.mkdir_p(Rails.root.join('log')) - config.active_job.logger = ActiveSupport::Logger.new(Rails.root.join('log', 'workers.log')) + config.active_job.logger = if FeatureManagement.log_to_stdout? + ActiveSupport::Logger.new(STDOUT) + else + ActiveSupport::Logger.new( + Rails.root.join('log', Idp::Constants::WORKER_LOG_FILENAME), + ) + end config.active_job.logger.formatter = config.log_formatter + config.logger = if FeatureManagement.log_to_stdout? + ActiveSupport::Logger.new(STDOUT) + else + ActiveSupport::Logger.new( + Rails.root.join('log', "#{Rails.env}.log"), + ) + end + + config.kms_logger = if FeatureManagement.log_to_stdout? + ActiveSupport::Logger.new(STDOUT) + else + ActiveSupport::Logger.new( + Rails.root.join('log', Idp::Constants::KMS_LOG_FILENAME), + ) + end + config.good_job.execution_mode = :external config.good_job.poll_interval = 5 config.good_job.enable_cron = true diff --git a/config/environments/production.rb b/config/environments/production.rb index 5a4c37bb4bc..5ef37b59d9b 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -34,10 +34,6 @@ # creates false positive results. config.action_dispatch.ip_spoofing_check = false - if IdentityConfig.store.log_to_stdout - Rails.logger = Logger.new(STDOUT) - config.logger = ActiveSupport::Logger.new(STDOUT) - end config.log_level = :info config.lograge.ignore_actions = ['Api::Internal::SessionsController#show'] end diff --git a/config/initializers/ahoy.rb b/config/initializers/ahoy.rb index bc55c2306e5..8b9db230954 100644 --- a/config/initializers/ahoy.rb +++ b/config/initializers/ahoy.rb @@ -2,6 +2,17 @@ require 'utf8_cleaner' +Rails.application.configure do + config.ahoy = ActiveSupport::OrderedOptions.new + config.ahoy.event_logger = if FeatureManagement.log_to_stdout? + ActiveSupport::Logger.new(STDOUT) + else + ActiveSupport::Logger.new( + Rails.root.join('log', Idp::Constants::EVENT_LOG_FILENAME), + ) + end +end + Ahoy.api = false # Period of inactivity before a new visit is created Ahoy.visit_duration = IdentityConfig.store.session_timeout_in_minutes.minutes @@ -12,10 +23,8 @@ module Ahoy class Store < Ahoy::BaseStore - EVENT_FILENAME = 'events.log' - - def track_visit(data) - log_visit(data) + def track_visit(_data) + nil end def track_event(data) @@ -23,7 +32,7 @@ def track_event(data) data[:id] = data.delete(:event_id) data[:visitor_id] = ahoy.visitor_token data[:visit_id] = data.delete(:visit_token) - data[:log_filename] = EVENT_FILENAME + data[:log_filename] = Idp::Constants::EVENT_LOG_FILENAME log_event(data) end @@ -40,28 +49,8 @@ def exclude? protected - def log_visit(data) - visit_logger.info data.to_json - end - def log_event(data) - event_logger.info data.to_json - end - - def visit_logger - @visit_logger ||= if FeatureManagement.log_to_stdout? - ActiveSupport::Logger.new(STDOUT) - else - ActiveSupport::Logger.new(Rails.root.join('log', 'visits.log')) - end - end - - def event_logger - @event_logger ||= if FeatureManagement.log_to_stdout? - ActiveSupport::Logger.new(STDOUT) - else - ActiveSupport::Logger.new(Rails.root.join('log', EVENT_FILENAME)) - end + Rails.application.config.ahoy.event_logger.info(data.to_json) end def invalid_uuid?(token) diff --git a/config/initializers/aws.rb b/config/initializers/aws.rb index 7cfa5b45f87..a8cece2570f 100644 --- a/config/initializers/aws.rb +++ b/config/initializers/aws.rb @@ -8,6 +8,6 @@ http_read_timeout: IdentityConfig.store.aws_http_timeout.to_f, retry_limit: IdentityConfig.store.aws_http_retry_limit, retry_max_delay: IdentityConfig.store.aws_http_retry_max_delay, - logger: ActiveSupport::Logger.new(Rails.root.join('log', 'production.log')), + logger: Rails.application.config.logger, log_formatter: log_formatter, ) diff --git a/config/initializers/telephony.rb b/config/initializers/telephony.rb index a6a81259c06..e3abf57ef02 100644 --- a/config/initializers/telephony.rb +++ b/config/initializers/telephony.rb @@ -5,9 +5,12 @@ Telephony.config do |c| c.adapter = IdentityConfig.store.telephony_adapter.to_sym c.logger = if FeatureManagement.log_to_stdout? - Logger.new(STDOUT, level: :info) + ActiveSupport::Logger.new(STDOUT, level: :info) else - Logger.new('log/telephony.log', level: :info) + ActiveSupport::Logger.new( + Rails.root.join('log', Idp::Constants::TELEPHONY_LOG_FILENAME), + level: :info, + ) end c.voice_pause_time = IdentityConfig.store.voice_otp_pause_time diff --git a/lib/identity_job_log_subscriber.rb b/lib/identity_job_log_subscriber.rb index b7eeb3b9682..d22e984f174 100644 --- a/lib/identity_job_log_subscriber.rb +++ b/lib/identity_job_log_subscriber.rb @@ -4,8 +4,6 @@ require 'active_job/log_subscriber' class IdentityJobLogSubscriber < ActiveSupport::LogSubscriber - LOG_FILENAME = 'workers.log' - def enqueue(event) job = event.payload[:job] ex = event.payload[:exception_object] @@ -122,13 +120,7 @@ def logger end def self.worker_logger - return @worker_logger if defined?(@worker_logger) - - if FeatureManagement.log_to_stdout? - @worker_logger = ActiveSupport::Logger.new(STDOUT) - else - @worker_logger = ActiveSupport::Logger.new(Rails.root.join('log', LOG_FILENAME)) - end + Rails.application.config.active_job.logger end private @@ -175,7 +167,7 @@ def default_attributes(event, job) trace_id: trace_id(job), queue_name: queue_name(event), job_id: job.job_id, - log_filename: LOG_FILENAME, + log_filename: Idp::Constants::WORKER_LOG_FILENAME, } end diff --git a/lib/idp/constants.rb b/lib/idp/constants.rb index 4fdb59f44ec..03b45648ce5 100644 --- a/lib/idp/constants.rb +++ b/lib/idp/constants.rb @@ -2,6 +2,12 @@ module Idp module Constants AVAILABLE_LOCALES = %w[en es fr] UUID_REGEX = /\A[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\z/ + + KMS_LOG_FILENAME = 'kms.log' + WORKER_LOG_FILENAME = 'workers.log' + EVENT_LOG_FILENAME = 'events.log' + TELEPHONY_LOG_FILENAME = 'telephony.log' + module Vendors ACUANT = 'acuant' LEXIS_NEXIS = 'lexis_nexis' diff --git a/spec/lib/identity_job_log_subscriber_spec.rb b/spec/lib/identity_job_log_subscriber_spec.rb index d870471dad0..4a96e177545 100644 --- a/spec/lib/identity_job_log_subscriber_spec.rb +++ b/spec/lib/identity_job_log_subscriber_spec.rb @@ -128,7 +128,7 @@ queue_name: kind_of(String), timestamp: kind_of(String), trace_id: nil, - log_filename: IdentityJobLogSubscriber::LOG_FILENAME, + log_filename: Idp::Constants::WORKER_LOG_FILENAME, ) end @@ -164,7 +164,7 @@ queue_name: kind_of(String), timestamp: kind_of(String), trace_id: nil, - log_filename: IdentityJobLogSubscriber::LOG_FILENAME, + log_filename: Idp::Constants::WORKER_LOG_FILENAME, ) end @@ -200,7 +200,7 @@ queue_name: 'NilClass(low)', timestamp: kind_of(String), trace_id: nil, - log_filename: IdentityJobLogSubscriber::LOG_FILENAME, + log_filename: Idp::Constants::WORKER_LOG_FILENAME, ) end @@ -235,7 +235,7 @@ trace_id: nil, queue_name: 'NilClass(low)', job_id: job.job_id, - log_filename: IdentityJobLogSubscriber::LOG_FILENAME, + log_filename: Idp::Constants::WORKER_LOG_FILENAME, ) end @@ -300,7 +300,7 @@ queue_name: kind_of(String), timestamp: kind_of(String), trace_id: nil, - log_filename: IdentityJobLogSubscriber::LOG_FILENAME, + log_filename: Idp::Constants::WORKER_LOG_FILENAME, ) end @@ -357,7 +357,7 @@ def perform(_); end queue_name: 'NilClass(low)', timestamp: kind_of(String), trace_id: nil, - log_filename: IdentityJobLogSubscriber::LOG_FILENAME, + log_filename: Idp::Constants::WORKER_LOG_FILENAME, ) end @@ -393,7 +393,7 @@ def perform(_); end queue_name: 'NilClass(low)', job_id: job.job_id, scheduled_at: kind_of(String), - log_filename: IdentityJobLogSubscriber::LOG_FILENAME, + log_filename: Idp::Constants::WORKER_LOG_FILENAME, ) end @@ -435,7 +435,7 @@ def perform(_); end queue_name: kind_of(String), job_id: job.job_id, exception_class_warn: 'Errno::ECONNREFUSED', - log_filename: IdentityJobLogSubscriber::LOG_FILENAME, + log_filename: Idp::Constants::WORKER_LOG_FILENAME, ) end diff --git a/spec/services/encryption/kms_logger_spec.rb b/spec/services/encryption/kms_logger_spec.rb index 3036a547fb4..9b3dd45cb1d 100644 --- a/spec/services/encryption/kms_logger_spec.rb +++ b/spec/services/encryption/kms_logger_spec.rb @@ -10,7 +10,7 @@ encryption_context: { context: 'pii-encryption', user_uuid: '1234-abc' }, key_id: 'super-duper-aws-kms-key-id', }, - log_filename: Encryption::KmsLogger::LOG_FILENAME, + log_filename: Idp::Constants::KMS_LOG_FILENAME, }.to_json expect(described_class.logger).to receive(:info).with(log) @@ -31,7 +31,7 @@ encryption_context: nil, key_id: 'super-duper-aws-kms-key-id', }, - log_filename: Encryption::KmsLogger::LOG_FILENAME, + log_filename: Idp::Constants::KMS_LOG_FILENAME, }.to_json expect(described_class.logger).to receive(:info).with(log)