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
No related branches found
No related tags found
No related merge requests found
Pipeline #28785 passed
......@@ -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)
......
......@@ -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.
......
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
......@@ -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)
......
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment