Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom logger in Rails 3?

I want to have a custom logger for my application, which of course logs to a different file, someone asked a question: Setting up the logger in rails 3

But I want to have a logger which I can call with my own class name like:

StatusLogger.info "something happend!!!"

How can I do this?

like image 371
JP Silvashy Avatar asked Jun 03 '11 22:06

JP Silvashy


People also ask

How do I create a log in rails?

Rails makes use of the ActiveSupport::Logger class to write log information. Other loggers, such as Log4r , may also be substituted. By default, each log is created under Rails. root/log/ and the log file is named after the environment in which the application is running.

Where can I find rails logger?

In a Rails app, logs are stored under the /log folder. In development mode, the development. log file is used & you see log output on the terminal you're running rails server on.

How does rails logger work?

Rails is configured to create separate log files for each of the three default environments: development, test and production. By default it puts these log files in the log/ directory of your project. So if you open up that folder you'll see a development. log and test.


2 Answers

You could do that with this code

logfile = File.open('/path/to/log.log', 'a')  
StatusLogger = Logger.new(logfile)
StatusLogger.info 'Hello World!'

And you would most likely configure this in an initializer file, or you could do it in an environment file if you wanted.

like image 146
Devin M Avatar answered Sep 20 '22 19:09

Devin M


You mean, like having in application.rb:

StatusLogger = ActiveSupport::BufferedLogger.new(Rails.root.join('log/status.log'))
like image 22
Roman Avatar answered Sep 24 '22 19:09

Roman