# # rails turns ruby's built-in logger class into a piece of junk, completely # ripping out all the standard log format bits that make log analysis # possible and, it would be quite possible for rails to use it's own subclass: # leaving the built-in logger alone but, since it doesn't, we can do that # oursevles. the following code snippet shows how to can we can create a # logger subclass that actually works in spite of the rails monkey patching. # it also adds some methods that allow a log to be turned on/off in order to # selectively silence the log, exposes access to the underlying device, and # provides a test to see if the log is dumping to the console (tty) or a file. # module SomeRandomNameSpace class Logger < ::Logger def self.new *a, &b super(*a, &b).instance_eval{ @default_formatter = @formatter = Formatter.new; self } end def format_message(severity, datetime, progname, msg) (@formatter || @default_formatter).call(severity, datetime, progname, msg) end def device @logdev.instance_eval{ @dev } end def tty? device.respond_to?('tty?') and device.tty? end def turn which @logdev.extend OnOff unless OnOff === @logdev @logdev.turn which end def on turn :on end def off turn :off end module OnOff def turn which @turned = which.to_s =~ %r/on/i ? :on : :off end def write message return message.to_s.size if @turned == :off super end end end end