Skip to content

Instantly share code, notes, and snippets.

@sephraim
Last active March 14, 2024 14:31
Show Gist options
  • Save sephraim/c812f49449516c60bde0c0e68dd97da4 to your computer and use it in GitHub Desktop.
Save sephraim/c812f49449516c60bde0c0e68dd97da4 to your computer and use it in GitHub Desktop.
[Setup & testing Rails logger] including for development, testing, and production.
# Diff between Logger and ActiveSupport::Logger
https://stackoverflow.com/q/64433146/990637
# Problem with Puma / .silence method when using Logger instead of ActiveSupport::Logger
https://github.com/rails/sprockets-rails/issues/376#issuecomment-287560399
# File: config/environments/development.rb
Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.
# Setup development logger
logger = ActiveSupport::Logger.new($stdout)
logger.formatter = Logger::Formatter.new
config.logger = ActiveSupport::TaggedLogging.new(logger)
config.log_level = :info
# The rest of your config settings...
end
# File: config/environments/test.rb
Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.
# Setup test logger
logger = ActiveSupport::Logger.new($stdout)
logger.formatter = Logger::Formatter.new
config.logger = ActiveSupport::TaggedLogging.new(logger)
config.log_level = :warn
# The rest of your config settings...
end
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe SomeClass, type: :lib do
before(:example) do
allow(Rails.logger).to receive(:info)
allow(Rails.logger).to receive(:error)
end
it 'logs a success message' do
# < SOME CODE THAT EXECUTES THE LOGGER >
expect(Rails.logger).to have_received(:info).with('Success!')
expect(Rails.logger).not_to have_received(:error)
end
end
Rails.logger.info 'This is an info message...'
Rails.logger.warn 'This is a warning message...'
Rails.logger.error 'This is an error message...'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment