Socket
Book a DemoInstallSign in
Socket

loglevel

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

loglevel

1.1.0
bundlerRubygems
Version published
Maintainers
1
Created
Source

Loglevel

Gem version Gem downloads Build Status Code Climate Test Coverage Dependency Status Security

Control logging at runtime with an environment variable

Usage:

LOGLEVEL=WARN rails server

Loglevel will direct your logging to STDOUT (as suggested in the 12-factor app). See the Log Device section below for how to control where your logging goes.

Features

Control which components create visible log entries by simply setting an environment variable. For instance:

LOGLEVEL=DEBUG,NOAR,NOHTTP rails server

would set the Rails logger level to DEBUG but would suppress messages from the ActiveRecord logger and the HttpLogger gem.

Here are the available settings:

OptionDescription
FATALEquivalent to config.log_level = :fatal
ERROREquivalent to config.log_level = :error
WARNEquivalent to config.log_level = :warn
INFOEquivalent to config.log_level = :info
DEBUGEquivalent to config.log_level = :debug
NOARDo not show ActiveRecord messages
HTTPShow HTTP messages
NOHTTPDo not show HTTP messages
NOHEADERSDo not include response headers in HTTP log
NOBODYDo not include response body in HTTP log

Dependencies

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.

HttpLogger

The HttpLogger gem works in a slightly different way. When we set the level for HttpLogger we are setting the level at which the HTTP messages are logged.

In the Loglevel environment this will be :debug, so you will only see HTTP messages if LOGLEVEL=DEBUG. If you want to see HTTP messages at a less verbose level then use the HTTP parameter when setting LOGLEVEL, like this:

LOGLEVEL=INFO,HTTP rails server

This will show all messages at INFO and above and will also show HTTP messages.

If you want to see DEBUG messages but not HTTP messages then use the NOHTTP parameter:

LOGLEVEL=DEBUG,NOHTTP rails server

Rails initialization

In a Rails context, we want Loglevel to be configured after Rails's own logger has been initialized but before any other app initialization has taken place. This allows us to control the logging of other components' initialization.

The best way I have found of doing this is to create a script in the config/initializers directory. These initializers are executed in alphabetical order so you can control when Loglevel is initialized by carefully naming your script.

To initialize Loglevel first, create a script called 01_loglevel.rb with the single line

Loglevel.setup

To understand which other points in the Rails initialization process you can choose, see The Rails Initialization Process.

Logger

By default Loglevel will instantiate Ruby's default Logger class (in a Rails context this will be something like ActiveSupport::TaggedLogging). 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 server

Log device

By 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 server

Classes with a logger

By 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 server

The only methods the class must support are logger and logger=

Contributing

Developer

  • 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

FAQs

Package last updated on 08 May 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.