Create a custom Rails Logger

拟墨画扇 提交于 2019-12-23 02:58:07

问题


Is this outdated? http://guides.rubyonrails.org/debugging_rails_applications.html I am following it step by step,

And in part '2.1 What is the Logger?'

It says how to create a custom logger.

1) You can specify an alternative logger in your environment.rb or any environment file:

Rails.logger = Logger.new(STDOUT)
Rails.logger = Log4r::Logger.new("Application Log")

I put this code in my environment.rb file and I get this:

uninitialized constant Log4r (NameError)

..then it says 'Or in the Initializer section, add any of the following:'

config.logger = Logger.new(STDOUT)
config.logger = Log4r::Logger.new("Application Log")

I dont have an initializer section in my environment.rb file! I get the same error!

It says 'You can also substitute another logger such as Log4r if you wish.' Why would somebody want to substitute an existing logger?

I removed Rails.logger = Log4r::Logger.new("Application Log") from the environment.rb file. It seems to be working, but I was expecting a file being created in the log/ directory but nothing. Here is my enviroment.rb file:

# Load the rails application
require File.expand_path('../application', __FILE__)

Rails.logger = Logger.new('Application Log')

# Initialize the rails application
MyApp::Application.initialize!

help? Some explanation please?


回答1:


You can set the Rails logger globally with:

Rails.logger = ...

or you can put it into an environment file, e.g. in config/environments/development.rb

MyApp::Application.configure do
  config.logger = ...
end

Next, Log4r is a separate gem that does not come with Rails. If you want to use it, add it to your Gemfile:

gem "log4r", "~> 1.1.10"

Why would somebody want to substitute an existing logger?

Because loggers have a different set of features. You might want to use a custom logger to upload the logs to an external service, process it etc.




回答2:


You need to require log4r in application.rb. I am assuming you have installed the gem. then in application.rb

 require 'log4r'


来源:https://stackoverflow.com/questions/14400831/create-a-custom-rails-logger

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!