Control logging at runtime with an environment variable
Usage:
LOGLEVEL=WARN rails serverControl which components create visible log entries by simply setting an environment variable. For instance:
LOGLEVEL=DEBUG,NOAR,NOHTTP rails serverwould set the Rails logger level to :debug but would suppress messages from
the ActiveRecord logger and the HttpLogger gem.
The features are itemized if you use the HELP option:
LOGLEVEL=HELP rails consoleHere are the available settings:
| Option | Description |
|---|---|
| HELP | Show these options |
| FATAL | Equivalent to config.log_level = :fatal |
| ERROR | Equivalent to config.log_level = :error |
| WARN | Equivalent to config.log_level = :warn |
| INFO | Equivalent to config.log_level = :info |
| DEBUG | Equivalent to config.log_level = :debug |
| NOAR | Do not show ActiveRecord messages |
| NOHTTP | Do not show HTTP messages |
| NOHEADERS | Do not include response headers in HTTP log |
| NOBODY | Do not include response body in HTTP log |
The examples in this document assume Loglevel is being used in a Rails environment but it doesn't depend on Rails and can be used in other contexts.
There are specific options to handle Railsy logging scenarios: things like controlling ActiveRecord logging. There are also specific options for handling the HttpLogger gem.
By default Loglevel will instantiate Ruby's default Logger class. If you want to use a different logger then you can use an environment variable to tell Loglevel which logger you use:
LOGLEVEL_LOGGER=Log4r LOGLEVEL=DEBUG rails serverBy default Loglevel will setup logging to the STDOUT device. If you want to
use a different device there's an environment variable for that:
LOGLEVEL_DEVICE=tmp/test.log LOGLEVEL=DEBUG rails serverBy default, Loglevel will setup the logger for Rails, ActiveRecord::Base and HttpLogger if they are present.
It will also setup logging for any other classes that you include in an environment variable:
LOGLEVEL_CLASSES=MyClass LOGLEVEL=DEBUG rails serverThe only methods the class must support are logger and logger=
- Fork it
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create new Pull Request