Ruby utility that lets you include a unique logger anywhere easily, without redundancy
Clone or download
Latest commit 8a50201 May 7, 2018
Permalink
Failed to load latest commit information.
bin
lib
spec
.gitignore
.rspec
.travis.yml
Gemfile
Guardfile
LICENSE
README.md
Rakefile
easy_logging.gemspec

README.md

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.

Branch Status
Release Build Status Coverage Status Gem Version Total Downloads
Development Build Status Coverage Status Depfu

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

Feedback

Any feedback is much appreciated.

I can only tailor this project to fit use-cases I know about - which are usually my own ones. If you find that this might be the right direction to solve your problem too but you find that it's suboptimal or lacks features don't hesitate to contact me.

Please let me know if you make use of this project so that I can prioritize further efforts.

Development

This gem is developed using Bundler conventions. A good overview can be found here.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/thisismydesign/easy_logging.

License

The gem is available as open source under the terms of the MIT License.