log_switch 0.1.0 released!
While developing other gems that required a single class/singleton style logger, I got tired of repeating the code to create that logger and mix it in to my base class. I just wanted to be able to require something, then be able to do:
I also wanted to be able to programmatically turn on/off logging by doing something like:
This gem allows just that. Well, almost…
require and extend to mix in to your class/module to get a single point of logging
Use whatever Logger you want
Get your app logging with a single point of logging:
require 'log_switch' class MyThing extend LogSwitch end MyThing.log "I like you, Ruby." # => D, [2011-10-07T14:40:26.697084 #30080] DEBUG -- : I like you, Ruby.
…and then you can simply switch off logging by doing:
MyThing.log = false MyThing.log "You're my favorite." # => No logging occurs!
By default, LogSwitch sets the log level to :debug. You can change the default log level as you go:
MyThing.log_level = :warn MyThing.log "Crap!" # => W, [2011-10-07T15:30:54.012502 #32892] WARN -- : Crap!
You can pass in the log level for your Logger type too:
MyThing.log "Stuff!", :info # => I, [2011-10-07T15:28:49.480741 #32892] INFO -- : Stuff! MyThing.log "Meow", :fatal # => F, [2011-10-07T15:32:21.207867 #32892] FATAL -- : Meow
If you have another Logger object you want to write to, no problem:
some_other_logger = Logger.new 'log.txt' MyThing.logger = some_other_logger MyThing.log "hi!" File.open('log.txt', 'r').read # => Logfile created on 2011-10-07 15:50:19 -0700 by logger.rb/25413 # D, [2011-10-07T15:51:16.385798 #34026] DEBUG -- : hi!