[ANN] tagged_logger 0.2.1

F

Fedor Kocherga

Hello all,

I'd like to announce one more logging facility 'tagged_logger':
http://github.com/fkocherga/tagged_logger

=== WHY? ===

Several reasons:
1. It makes #logger() method becomes accessible everywhere, so you do
not have to write code like:
def initialize; @log = ...; end
or like:
def initialize(logger); @log = logger; end

2. Should your classes really know what sort of Logger has to be used?
What if you've started using standard Logger, but later decided to
switch to different logging library? The 'tagged_logger' redirects
logging to whatever logging library you choose, it allows switching
without touching logging code. The 'tagger_logger' is sort of binding
between where and how logging happens.

3. Unlike log4j approach you do not have to build any sort of loggers
hierarchy, but still you have a lot of control over how logging happens
including different outputs for different subsystems. Actually chances
are you already have some taxonomies – class hierarchy, modules, source
code organized in certain way, why should you think about *one more*
hierarchy of loggers? The basic idea behind 'tagged_logger' gem is to
use existing taxonomies implicitly to organize your logging. For
example, when method of class A logs something:

class A
def foo; logger.info("foo"); end
end

the #logger could be smart enough to figure out that it gets called from
class A and later you may use this information to set logging rules.
Here is how:

=== EXAMPLES ===

#1: Simplest one

require 'tagged_logger'
require 'logger'

TaggedLogger.rules do
output_everything_to Logger.new(STDOUT)
end
logger.info("logger is everywhere!")

from now on #logger() becomes available in every class.

#2: Defining your own custom logging

require 'rubygems'
require 'tagged_logger'

TaggedLogger.rules do
output /.*/ do | level, tag, msg |
puts "#{level}-#{tag}: #{msg}"
end
end

The regexp passed to 'output' rule matches every class name and three
block arguments are:
level - one of :debug, :info, :warn, :error, :fatal
msg - the message being printed
tag - class name whose method calls #logger()

In block you may do whatever you like. Also you may have several rules
per one class (or set of classes). Providing block is sort of heavy way
customization. Very frequently it could be done much simpler:

# 3: Defining separate output for three classes Ftp, Http and Sockets:

TaggedLogger.rules do
output [Ftp, Http, Sockets] => Logger.new(open("network.log", "w"))
end

# 4: Putting log entries for three classes Ftp, Http and Sockets under
one common name Network and setting rule for tag Network :

TaggedLogger.rules do
rename [Ftp, Http, Sockets] => :Network
output :Network => Logger.new(STDOUT)
end

You may find more examples at:
http://github.com/fkocherga/tagged_logger/tree/master/examples/

=== LICENSE ===
TaggedLogger is released under the MIT license.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,150
Members
46,697
Latest member
AugustNabo

Latest Threads

Top