# TaggedLogger Detaches **what** is logged from **how** it is logged. ## What is it for? Every time you want to log something, simply write: logger.debug("verbose debug information") #warn, #info, #error, #fatal also work and do not worry about what kind of logger you use and how your code accesses it. You may configure these things later, one day writing to STDOUT works for you, another day you'll need something more sophisticated, like several log files each serving different components and for different audience. ## Installation $ sudo gem install tagged_logger --source=http://gemcutter.org ## Usage The simplest way to turn logging on for every class whose methods calls *#logger* TaggedLogger.rules do output_everything_to Logger.new(STDOUT) end From now on you may call *#logger* from anywhere, for example: class IWantToLogThis def foo logger.warn "foo" end end and after execution of: IWantToLogThis.new.foo you will see somewhat like: W, [2009-12-20T21:45:02.443524 #37166] WARN -- : IWantToLogThis: foo You may define your own formatting: TaggedLogger.rules do output /.*/ do |level, tag, msg| puts "#{level}-#{tag}: #{msg}" end end The '/.\*/' says that rule is applied for every class (i.e. class name matches /.\*/). The *level* and *msg* semantics should be obvious, they are severity threshold and being logged message, but *tag* is something not so obvious - it is class name whose method calls *#logger*. In example above tag is "IWantToLogThis" since *IWantToLogThis#foo* calls *#logger*. Note the *#logger* does not have *tag* parameter - tags defined automatically and they are always class names. If you decided to have separate log files for classes *Network* and *Database*: TaggedLogger.rules do output Network => Logger.new("network.log") output Database => Logger.new("database.log") end In case you want to define common log for several classes: TaggedLogger.rules do output [Ftp, Http, Sockets] => Logger.new("network.log") end Or if you want to have all these classes showing up under common tag *Network* in standard output: TaggedLogger.rules do output_everything_to Logger.new(STDOUT) rename [Ftp, Http, Sockets] => :Network end You may also use regular expressions in your rules: TaggedLogger.rules do output /Active::/ => Logger.new("active.log") end ## License TaggedLogger is released under the MIT license. ## Shortcomings Was not tested with *log4r*