Hatchet
Ruby logging library that provides the ability to add class/module specific filters.
This README provides a brief overview of Hatchet, see the main site for more complete documentation and tutorials.
Installation
Add this line to your application's Gemfile:
gem 'hatchet'
And then execute:
$ bundle
Or install it yourself as:
$ gem install hatchet
Usage
Logging
To use the logger you must add it to your classes as a mixin or use it to extend
your modules. Then you can call the logger through the methods log
and
logger
. They are aliases for the same method to ease migration.
Classes
class Foo
include Hatchet
def self.class_work
log.info { 'Doing some class work' }
end
def work
log.info { 'Doing some work' }
end
def dangerous_work
log.info { 'Attempting dangerous work' }
attempt_dangerous_work
log.info { 'Dangerous work complete' }
true
rescue => e
log.error "Dangerous work failed - #{e.message}", e
false
end
end
Modules
module Bar
include Hatchet
def self.work
log.info { 'Doing some module work' }
end
def work
log.info { 'Doing some mixin work' }
end
end
Configuration
Standard
Hatchet.configure do |config|
# Set the level to use unless overridden (defaults to :info)
config.level :info
# Set the level for a specific class/module and its children
config.level :debug, 'Namespace::Something::Nested'
# Add as many appenders as you like
config.appenders << Hatchet::LoggerAppender.new do |appender|
# Set the logger that this is wrapping (required)
appender.logger = Logger.new('log/test.log')
end
end
Sinatra
Use the standard configuration method but also register Hatchet as a helper where appropriate:
register Hatchet
Note that you may have to use the log
alias as Sinatra already has a logger
method.
Rails
Hatchet includes a Railtie that is loaded automatically and wraps the
Rails.logger
. The Hatchet configuration object is available through
config.hatchet
within your standard configuration files for fine-tuning your
Hatchet configuration.
To make it so your log calls are scoped to your controllers you also need to add
Hatchet to your ApplicationController
:
class ApplicationController < ActionController::Base
include Hatchet
end
You could include it in your models so that each of those has its own logging context too.
Contributing
- 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
All pull requests should come complete with tests when appropriate and should follow the existing style which is best described in Github's Ruby style guide.