Thursday, July 9, 2009

Rails logger configuration and log level in production environment

In production environment, it not feasible to use one log file forever. So people use log that rolls over, depending on file size or time etc.

If we want to roll over the log file, we can define our custom logger using the following code in the configuration(in config/environments/production.rb).

config.logger = Logger.new(config.log_path, 50, 5242880)
Here is the doc for instantiating Logger. The params are as follows.
Logger.new(name, shift_age = 7, shift_size = 1048576)<br />Logger.new(name, shift_age = 'weekly')
Another thing that we need to set for the production is the log level. We do not need to log the query generated by active record or logs that are in debug level. So we can define the log level as follows.
config.logger = Logger.new(config.log_path, 50, 5242880)<br />config.logger.level = Logger::INFO
But if you are using the default log which rails offers by default, you can set the log level as,
config.log_level = :info
You can find the rails environment configuration cheat sheet here.

4 comments:

Anonymous said...

Will this work for Windows OS?

Jitu said...

Yes it work in both Linux and windows :)

Anonymous said...

I work as a system admin and do little programming/development for our customized Redmine application.

Each and every time the development team keep on adding new plugins and change the existing code.

I handle some minor updates and do the needful in times of urgency or when the developers are busy in some other work.

I was looking for something like the following:
1. There are multiple rb/erb files that display the output. Is it possible to print the template file name?
2. I want to identify all the available config variables available for the whole application from one place & if this can be done where should I do it.

Eg: I initialized the variable config.log_level to debug and when I try to print <%= config.inspect %> I don't see any output.

But I couldn't find anything helpful.

Please help.

/Prasanth

John Barness said...

Thank you for the code.
I have recently read virtual data room reviews that said that logs should be configured like you described.