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 = STDERRAdding 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_devicesCustom 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).