Skip to content
Snippets Groups Projects
Commit 2cd7b003 authored by Daan Davidsz's avatar Daan Davidsz
Browse files

Configure RoQua logger based on RAILS_LOG_TO_STDOUT_USING_ROQUA_LOGGER env variable

parent 6f1d8e2e
Branches
Tags
No related merge requests found
Pipeline #28785 passed
...@@ -8,7 +8,7 @@ GIT ...@@ -8,7 +8,7 @@ GIT
PATH PATH
remote: . remote: .
specs: specs:
roqua-support (0.1.32) roqua-support (0.1.33)
active_interaction (~> 3.0) active_interaction (~> 3.0)
activesupport (>= 3.2, < 6) activesupport (>= 3.2, < 6)
naught (~> 1.0) naught (~> 1.0)
......
...@@ -48,6 +48,10 @@ require 'roqua/support/request_logger' ...@@ -48,6 +48,10 @@ require 'roqua/support/request_logger'
Roqua::Support::RequestLogger.attach_to :action_controller 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 ### Error reporting
Log and error to Roqua.logger, appsignal and/or airbrake, depending on which is configured. Log and error to Roqua.logger, appsignal and/or airbrake, depending on which is configured.
......
class RoquaLoggingRailtie < Rails::Railtie class RoquaLoggingRailtie < Rails::Railtie
initializer 'roqua_logging_railtie.configure_roqua_logging' do config.after_initialize do |app|
RoquaLoggingRailtie.configure if ENV['RAILS_LOG_TO_STDOUT_USING_ROQUA_LOGGER'].present? RoquaLoggingRailtie.configure
end end
def self.configure class << self
Roqua.logger = ActiveSupport::Logger.new(STDOUT) def configure
Roqua.logger.logger.formatter = Logger::Formatter.new 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' def output_stream
Roqua::Support::RequestLogger.attach_to :action_controller if ENV['RAILS_LOG_TO_STDOUT_USING_ROQUA_LOGGER'].present?
STDOUT
else
Rails.root.join("log/#{Rails.env}-events.log")
end
end
end end
end end
...@@ -16,7 +16,7 @@ module Roqua ...@@ -16,7 +16,7 @@ module Roqua
end end
def logger def logger
@logger ||= LogWrapper.new(Logger.new(STDOUT)) @logger || raise('Roqua.logger not yet initialized')
end end
def logger=(logger) def logger=(logger)
......
...@@ -3,40 +3,40 @@ require 'spec_helper' ...@@ -3,40 +3,40 @@ require 'spec_helper'
require 'roqua/support/request_logger' require 'roqua/support/request_logger'
require 'roqua/logging/roqua_logging_railtie' require 'roqua/logging/roqua_logging_railtie'
describe RoquaLoggingRailtie do RSpec.shared_examples 'RoQua logging setup' do
let(:initializer_key) { 'roqua_logging_railtie.configure_roqua_logging' } def configure_roqua_logging(log_to_stdout)
ClimateControl.modify RAILS_LOG_TO_STDOUT_USING_ROQUA_LOGGER: log_to_stdout do
subject(:initializer) do RoquaLoggingRailtie.configure
Rails.application.initializers.select { |i| i.name == initializer_key }.first end
end
it 'loads the initializer' do
expect(Rails.application.initializers.map(&:name)).to include(initializer_key)
end end
it 'attaches Roqua::Support::RequestLogger to action_controller' do it 'attaches Roqua::Support::RequestLogger to action_controller' do
expect(Roqua::Support::RequestLogger).to receive(:attach_to).with(:action_controller) expect(Roqua::Support::RequestLogger).to receive(:attach_to).with(:action_controller)
RoquaLoggingRailtie.configure configure_roqua_logging(log_to_stdout)
end end
end
it 'logs to STDOUT' do Rspec.describe RoquaLoggingRailtie do
RoquaLoggingRailtie.configure context 'when RAILS_LOG_TO_STDOUT_USING_ROQUA_LOGGER is present' do
expect(ActiveSupport::Logger.logger_outputs_to?(Roqua.logger.logger, STDOUT)).to be_truthy include_examples 'RoQua logging setup'
end
it 'when RAILS_LOG_TO_STDOUT_USING_ROQUA_LOGGER is blank' do let(:log_to_stdout) { 'true' }
expect(RoquaLoggingRailtie).to_not receive(:configure)
ClimateControl.modify RAILS_LOG_TO_STDOUT_USING_ROQUA_LOGGER: '' do it 'logs to STDOUT' do
initializer.block.call expect(
ActiveSupport::Logger.logger_outputs_to?(Roqua.logger.logger, STDOUT)
).to be_truthy
end end
end end
it 'when RAILS_LOG_TO_STDOUT_USING_ROQUA_LOGGER is present' do context 'when RAILS_LOG_TO_STDOUT_USING_ROQUA_LOGGER is blank' do
expect(RoquaLoggingRailtie).to receive(:configure) include_examples 'RoQua logging setup'
let(:log_to_stdout) { '' }
ClimateControl.modify RAILS_LOG_TO_STDOUT_USING_ROQUA_LOGGER: 'true' do it 'logs to a log file' do
initializer.block.call expect(Roqua.logger.logger.instance_variable_get("@logdev").dev.path)
.to eql(Rails.root.join('log/test-events.log').to_s)
end end
end end
end end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment