From 2cd7b00383902fe40d229839c2e8b9c983b77625 Mon Sep 17 00:00:00 2001 From: Daan Davidsz <daan@roqua.nl> Date: Tue, 16 Oct 2018 11:26:03 +0000 Subject: [PATCH] Configure RoQua logger based on RAILS_LOG_TO_STDOUT_USING_ROQUA_LOGGER env variable --- Gemfile.lock | 2 +- README.md | 4 ++ lib/roqua/logging/roqua_logging_railtie.rb | 26 ++++++++--- lib/roqua/support.rb | 2 +- .../logging/roqua_logging_railtie_spec.rb | 44 +++++++++---------- 5 files changed, 47 insertions(+), 31 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 01e77c3..a321c67 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -8,7 +8,7 @@ GIT PATH remote: . specs: - roqua-support (0.1.32) + roqua-support (0.1.33) active_interaction (~> 3.0) activesupport (>= 3.2, < 6) naught (~> 1.0) diff --git a/README.md b/README.md index ced8891..4d640f7 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,10 @@ require 'roqua/support/request_logger' Roqua::Support::RequestLogger.attach_to :action_controller ``` +Starting with Rails version 5 and up logging can be redirected to STDOUT by setting the `RAILS_LOG_TO_STDOUT` environment +variable to a non blank value. roqua-support adds similar functionality by providing an alternative environment variable + - `RAILS_LOG_TO_STDOUT_USING_ROQUA_LOGGER` - to send the logging output of the custom RoQua logger to STDOUT. + ### Error reporting Log and error to Roqua.logger, appsignal and/or airbrake, depending on which is configured. diff --git a/lib/roqua/logging/roqua_logging_railtie.rb b/lib/roqua/logging/roqua_logging_railtie.rb index 15752d9..1d28c45 100644 --- a/lib/roqua/logging/roqua_logging_railtie.rb +++ b/lib/roqua/logging/roqua_logging_railtie.rb @@ -1,13 +1,25 @@ + class RoquaLoggingRailtie < Rails::Railtie - initializer 'roqua_logging_railtie.configure_roqua_logging' do - RoquaLoggingRailtie.configure if ENV['RAILS_LOG_TO_STDOUT_USING_ROQUA_LOGGER'].present? + config.after_initialize do |app| + RoquaLoggingRailtie.configure end - def self.configure - Roqua.logger = ActiveSupport::Logger.new(STDOUT) - Roqua.logger.logger.formatter = Logger::Formatter.new + class << self + def configure + Roqua.logger = ActiveSupport::Logger.new(output_stream).tap do |logger| + logger.formatter = Logger::Formatter.new + end + + require 'roqua/support/request_logger' + Roqua::Support::RequestLogger.attach_to :action_controller + end - require 'roqua/support/request_logger' - Roqua::Support::RequestLogger.attach_to :action_controller + def output_stream + if ENV['RAILS_LOG_TO_STDOUT_USING_ROQUA_LOGGER'].present? + STDOUT + else + Rails.root.join("log/#{Rails.env}-events.log") + end + end end end diff --git a/lib/roqua/support.rb b/lib/roqua/support.rb index 65c9204..77f1c16 100644 --- a/lib/roqua/support.rb +++ b/lib/roqua/support.rb @@ -16,7 +16,7 @@ module Roqua end def logger - @logger ||= LogWrapper.new(Logger.new(STDOUT)) + @logger || raise('Roqua.logger not yet initialized') end def logger=(logger) diff --git a/spec/roqua/logging/roqua_logging_railtie_spec.rb b/spec/roqua/logging/roqua_logging_railtie_spec.rb index bb0923a..eaa9e43 100644 --- a/spec/roqua/logging/roqua_logging_railtie_spec.rb +++ b/spec/roqua/logging/roqua_logging_railtie_spec.rb @@ -3,40 +3,40 @@ require 'spec_helper' require 'roqua/support/request_logger' require 'roqua/logging/roqua_logging_railtie' -describe RoquaLoggingRailtie do - let(:initializer_key) { 'roqua_logging_railtie.configure_roqua_logging' } - - subject(:initializer) do - Rails.application.initializers.select { |i| i.name == initializer_key }.first - end - - it 'loads the initializer' do - expect(Rails.application.initializers.map(&:name)).to include(initializer_key) +RSpec.shared_examples 'RoQua logging setup' do + def configure_roqua_logging(log_to_stdout) + ClimateControl.modify RAILS_LOG_TO_STDOUT_USING_ROQUA_LOGGER: log_to_stdout do + RoquaLoggingRailtie.configure + end end it 'attaches Roqua::Support::RequestLogger to action_controller' do expect(Roqua::Support::RequestLogger).to receive(:attach_to).with(:action_controller) - RoquaLoggingRailtie.configure + configure_roqua_logging(log_to_stdout) end +end - it 'logs to STDOUT' do - RoquaLoggingRailtie.configure - expect(ActiveSupport::Logger.logger_outputs_to?(Roqua.logger.logger, STDOUT)).to be_truthy - end +Rspec.describe RoquaLoggingRailtie do + context 'when RAILS_LOG_TO_STDOUT_USING_ROQUA_LOGGER is present' do + include_examples 'RoQua logging setup' - it 'when RAILS_LOG_TO_STDOUT_USING_ROQUA_LOGGER is blank' do - expect(RoquaLoggingRailtie).to_not receive(:configure) + let(:log_to_stdout) { 'true' } - ClimateControl.modify RAILS_LOG_TO_STDOUT_USING_ROQUA_LOGGER: '' do - initializer.block.call + it 'logs to STDOUT' do + expect( + ActiveSupport::Logger.logger_outputs_to?(Roqua.logger.logger, STDOUT) + ).to be_truthy end end - it 'when RAILS_LOG_TO_STDOUT_USING_ROQUA_LOGGER is present' do - expect(RoquaLoggingRailtie).to receive(:configure) + context 'when RAILS_LOG_TO_STDOUT_USING_ROQUA_LOGGER is blank' do + include_examples 'RoQua logging setup' + + let(:log_to_stdout) { '' } - ClimateControl.modify RAILS_LOG_TO_STDOUT_USING_ROQUA_LOGGER: 'true' do - initializer.block.call + it 'logs to a log file' do + expect(Roqua.logger.logger.instance_variable_get("@logdev").dev.path) + .to eql(Rails.root.join('log/test-events.log').to_s) end end end -- GitLab