views:

1150

answers:

2

Is there some way to disable all logging (whether from ActiveRecord, other parts of Rails, plugins, etc.) from a script running via script/runner? I've got a long data-loading script running, and it's generating an absurd amount of useless logging information. I'm not trying to change script/runner's environment (e.g., production, development, etc.)- the script currently needs to run in the "development" environment, and it's doing so without issue at the moment.

All I want to do is suppress all logging for the duration of the script's lifetime. From reading the docs, it looks like if I can get a handle to its Rails::Configuration I should be either able to set log_level to something other than :debug or set its logger to nil. Am I on the right track? If so, how do I get access to its Rails::Configuration object?

A: 

The settings are in your config/environments/*.rb files, which override the default for environment.rb. When it does "Rails::Initializer.run do |config|" it's running with the config object. I'm fairly sure you can change either to suite your needs, but I'm not sure how you'd pass it a flag saying it was a script/runner script currently executing.

(I know you said your not interested in changing the environment, but I thought I'd throw out there that you could create a new env for a dataload script that has most of the same settings but no logging fairly easily)

Todd Gardner
Huh. That's a decent idea, maybe I'll give that a try.But, as far as you know, there's no way to override configuration settings on the fly?
Steven Bedrick
I'm not aware of any; after environment runs, I'm not even sure if you can access the values that were set.
Todd Gardner
+4  A: 

There are a couple of ways I can think of.

1) Set the log level so high that nothing will get logged:

ActiveRecord::Base.logger.level = Logger::Severity::UNKNOWN

See Logger::Severity for more about that. UNKNOWN is the highest.

"level" is just a number, so it can be set to Logger::Severity::UNKNOWN + 1 if you feel paranoid. :)

2) Send the output to /dev/null.

ActiveRecord::Base.logger = Logger.new('/dev/null')

(I'm assuming that there is just one instance of Logger that is used by Rails, and that it's the one you get via ActiveRecord::Base.logger. If there are more of them you'd have to find them and fiddle with them also.)

If you don't like these you might find something else by reading the Logger docs.

Jamie Flournoy
Aha! That's exactly what I was looking for. Thanks!
Steven Bedrick