Last active
March 14, 2024 14:31
-
-
Save sephraim/c812f49449516c60bde0c0e68dd97da4 to your computer and use it in GitHub Desktop.
[Setup & testing Rails logger] including for development, testing, and production.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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