Low commit activity in last 3 years
No release in over a year
Logger that support many and different devices for specified levels
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 9.0.0, < 12.0.0
>= 12.0.0, < 14.0.0
>= 3.5.0, < 3.11.0
>= 1.25.0, < 2.0.0
>= 0.6.0, < 1.0.0
>= 2.8.0, < 3.0.0

Runtime

>= 7.0.0, < 8.0.0
 Project Readme

MultipleDevicesLogger

Logger that support many and different devices for specified levels.

Setup

Just add this into your Gemfile:

gem 'multiple_devices_logger'

Then, just run a bundle install.

Usage

Simple usage

This will add STDOUT device for debug and info severities and a file for all others.

require 'multiple_devices_logger'

logger = MultipleDevicesLogger.new
logger.add_device(STDOUT, Logger::DEBUG, Logger::INFO)
logger.add_device('/tmp/logs', Logger::WARN, Logger::ERROR, Logger::FATAL)
logger.warn('BAM!')

Note that severty can specified as string or symbol.

Define default device

If there is no device for given severity, default device is used. It can be defined thanks to default_device accessor.

logger.default_device = STDERR

Adding a device for all severities

If you don't specify any severity to #add_device method, specified device will be added to all sevrities.

Here is an example that logs all messages to STDERR and fatal messages to both a file and STDERR.

logger.add_device(STDERR)
logger.add_device('/tmp/fatal.log', :fatal)

Operators

To simplify devices definition, you can use >, >=, <, <= operators:

logger.add_device('/var/log/myapp.log', '>= warn')

Clear all devices

Just use #clear_devices to clear all registered devices:

logger.clear_devices

Custom formatter for each device

You can configure a formatter for each device via :formatter option, here is an example:

logger.add_device(STDERR, :debug, formatter: -> (severity, time, progname, message) { "[#{severity}] #{message}\n" })

Otherwise default formatter is used.

Ignore exceptions from logging

You can ignore some exceptions to be logged globally:

logger.ignore_exceptions(IOError, ArgumentError)

Or just for some devices with :ignore_exceptions otpion when you register the device:

logger.add_device(STDERR, :debug, ignore_exceptions: [IOError, ArgumentError])

In addition to classes, A lambda can be given to ignore exception, example:

logger.ignore_exceptions(ArgumentError, -> (e) { e.message =~ /404/ }

Executing test suite

This project is fully tested with Rspec 3. Just run bundle exec rake (after a bundle install).