Ruby logging library that provides the ability to add class/module specific filters
View the Project on GitHub gshutler/hatchet
Hosted on GitHub Pages — Theme by orderedlist
Throughout this page we will build up a custom appender. Going from a stub of the required methods through to a complete, configurable appender ready for use with Hatchet.
Appenders are classes with an interface of:
Returns true
if the appender should log messages for the given level and
context, otherwise returns false
.
A Symbol representing the level of the log call. One of, in decreasing level of severity:
:fatal
:error
:warn
:info
:debug
An object representing the context of the log call. When transformed to a
String
by calling to_s
it will represent a hierarchy with each level
separated by ::
.
Adds a message to the logging destination.
A Symbol representing the level of the log call. One of, in descreasing level of severity:
:fatal
:error
:warn
:info
:debug
An object representing the context of the log call. When transformed to a
String
by calling to_s
it will represent a hierarchy with each level
separated by ::
.
The Hatchet::Message
to log.
The add
method will only be called by Hatchet if the enabled?
method returns
true
for a given logging call.
It is recommended you make your appender’s logging level and message formatting configurable in the same way as standard appenders.
The level management functionality has been encapsulated in LevelManager
which you can mix into your own classes:
The LevelManager
adds the standard level
configuration method to your appender and implements
the enabled?
method too.
Appenders are expected to yield themselves to a block, when given, upon creation:
It is recommended that you format messages via a formatter object. This will enable users of your appender to easily change the format of messages it produces without you having to modify your appender.
Of course, you can specify a preferred formattr by setting that as the default. You may even chose to ship a custom formatter alongside your appender.
All that is left is for our logger to implement the logging of messages. We will
keep this simple and just make it log to STDOUT
:
That’s all there is to creating a custom appender. You should be able to see the
same structure in the LoggerAppender
that comes with Hatchet and the HipChatAppender
from hatchet-hipchat.
Remember not to reinvent the wheel and first check the list of known appenders for an existing appender that does what you want and contribute to that if it isn’t quite right for your needs.