EasyLogging
Ruby utility that lets you include a unique logger anywhere easily, without redundancy.
Inspired by this StackOverflow thread EasyLogging
provides an easy way to create and configure unique loggers for any context as an alternative to having a global logger (e.g. Rails.logger
). It uses the native Ruby Logger from stdlib and has no runtime dependencies.
Status and support
- ✔ stable
- ✔ supported
- ✖ no ongoing development
You are viewing the README of version v0.4.0. You can find other releases here.
Branch | Status |
---|---|
Release | |
Development |
Features
- Add logging functionality anywhere with one, short, descriptive command
- Logger is unique to context and contains relevant information (e.g. class name)
- Logger is pre-configurable globally (destination, level, formatter)
- Logger is fully configurable locally
- The same syntax works in any context (e.g. class or instance methods)
Installation
Add this line to your application's Gemfile:
gem 'easy_logging'
And then execute:
$ bundle
Or install it yourself as:
$ gem install easy_logging
Usage
Add include EasyLogging
to any context (e.g. a class) you want to extend with logging functionality.
require 'easy_logging'
# Global pre-configuration for every Logger instance
EasyLogging.log_destination = 'app.log'
EasyLogging.level = Logger::DEBUG
class YourClass
include EasyLogging
def do_something
logger.debug('foo')
end
end
class YourOtherClass
include EasyLogging
def self.do_something
# Local custom Logger configuration
logger.formatter = proc do |severity, datetime, progname, msg|
"#{severity}: #{msg}\n"
end
# ...
logger.info('bar')
end
end
YourClass.new.do_something
YourOtherClass.do_something
app.log
:
D, [2018-03-13T13:35:40.337438 #44643] DEBUG -- YourClass: foo
INFO: bar
Global configuration
You should pre-configure EasyLogging before loading your application (or refer to Changing global configuration on the fly).
Destination
EasyLogging.log_destination = 'app.log'
Default: STDOUT
Since: v0.2.0
Level
EasyLogging.level = Logger::DEBUG
Default: Logger::INFO
Since: v0.3.0
Formatter
EasyLogging.formatter = proc do |severity, datetime, progname, msg|
severity + datetime + progname + msg
end
Default: Logger default
Since: v0.3.0
Changing global configuration on the fly
... is tricky but looking at the specs it's fairly easy to understand:
describe 'on the fly modification of global logger configuration' do
context 'class level logger' do
it 'uses old config if EasyLogging was included before config change'
it 'uses new config if EasyLogging was included after config change'
end
context 'instance level logger' do
it 'uses old config if instance was created before config change'
it 'uses new config if instance was created after config change'
end
end
Contribution and feedback
This project is built around known use-cases. If you have one that isn't covered don't hesitate to open an issue and start a discussion.
Bug reports and pull requests are welcome on GitHub at https://github.com/thisismydesign/easy_logging. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
Conventions
This project follows C-Hive guides for code style, way of working and other development concerns.
License
The gem is available as open source under the terms of the MIT License.