logging-rails
logging-rails copied to clipboard
Logging can load initializers first?
Gemfile:
gem 'logging-rails', require: 'logging/rails'
config/loggin.rb
The original.
config/initializers/logging.rb:
Rails.application.configure do
if Rails.env.production?
config.log_to = %w[file]
config.show_log_configuration = false
else
config.log_to = %w[stdout]
config.show_log_configuration = true
end
end
Log configuration:
root ............................................ *debug -T
- <Appenders::RollingFile:0x18f397c name="file">
ActionMailer::Base ............................ debug +A -T
ActiveRecord::Base ............................ debug +A -T
ActiveSupport::Cache::FileStore ............... debug +A -T
ActiveSupport::Dependencies ................... debug +A -T
Logging ....................................... *off -A -T
Rails ......................................... debug +A -T
I tried to let the config on environments files, but the same effect, the Rails original logging was supressed, but I can't log. I tried:
Rails.logger.error 'foo' # true
Logging::Logger[::Rails].error 'foo' # true
::Logging::Logger[self].error 'foo' # true
The problem is that log?: Logging ....................................... *off -A -T
If yes, What I need more to re-enable Rails logging with my manual one?
How is the logging calling? Rails.logger.xyz
?
Thanks you.
Well,
My problem is the config/logging.rb is called first then initializers, so config.log_to comes with a default (file), not my declared stdout. I used to not change original config files and create initializers to be easier future upgrades. So, there is some way to call config/initializers/logging.rb first then config/logging.rb or a better workaround?
I did a ugly workaround like the following:
Rails.application.configure do
if Rails.env.production?
config.log_to = %w[file]
config.show_log_configuration = false
else
config.log_to = %w[stdout]
config.show_log_configuration = true
end
config.log_level = :debug
end
Logging::Rails.configure do |config|
...
Does using Rails.application.config.before_initialize
inside of your initializer fix your issue?
You should really only have to set log_to
and show_log_configuration
in files in config/environments
.
I am using a simple initializer with before_initialize
to set the log level. I do this because I use the log level specified in the environment variable LOG_LEVEL
.
I set it directly on config
to work.
Thank you for the tip, I think it can solve this problem. (: