Module: TransitionalLogger
- Defined in:
- lib/transitional_logger.rb
Overview
- Author
-
Elon Flegenheimer
- Copyright
-
Copyright © 2013 Elon Flegenheimer
- License
-
The MIT License (MIT)
A singleton logger accessible from ALL objects. It is intended to be used to:
-
send logger data to stdout OR
-
log to a file while consuming stdout & stderr data as log input OR
-
destroy stdout, stderr, & log data OR
-
do one of the above until transitioned to another of the above
The logger can be altered just like a normal Logger:
logger.level = Logger::WARN
Example usage:
TransitionalLogger.stdout
logger.info 'this message goes through the logger to stdout'
puts 'this works like usual'
TransitionalLogger.file('/tmp/whatever.log')
Object.new.logger.info 'this message goes through the logger to the file above'
puts 'this message also goes through the logger above'
TransitionalLogger.blackhole
Object.new.logger.error 'this message disappears'
puts 'this message disappears too!'
Constant Summary collapse
- VERSION =
'1.0.0'
Class Method Summary collapse
-
.blackhole ⇒ Object
Creates a logger that consumes stdout, stderr, and all logger messages.
-
.file(*args) ⇒ Object
Creates the specified logger that is accessible from all objects.
-
.logger ⇒ Object
Logger accessor - otherwise accessible from any object.
-
.reset ⇒ Object
Reset the changes that this object has introduced.
-
.stdout ⇒ Object
Creates a logger that is accessible from all objects.
Class Method Details
.blackhole ⇒ Object
Creates a logger that consumes stdout, stderr, and all logger messages.
59 60 61 |
# File 'lib/transitional_logger.rb', line 59 def blackhole file('/dev/null') end |
.file(*args) ⇒ Object
Creates the specified logger that is accessible from all objects. stdout and stderr are redirected to this logger.
Parameters match Logger#new.
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/transitional_logger.rb', line 47 def file(*args) raise ArgumentError, 'Logger#new params must be specified for #file' if args.size == 0 reset if @streams @master_logger = infect_object(Logger.new(*args)) @streams = [$stdout, $stderr, $stdin] $stdout = CommandeeredOut.new(@master_logger) $stderr = CommandeeredErr.new(@master_logger) @master_logger end |
.logger ⇒ Object
Logger accessor - otherwise accessible from any object
74 75 76 |
# File 'lib/transitional_logger.rb', line 74 def logger @master_logger end |
.reset ⇒ Object
Reset the changes that this object has introduced
64 65 66 67 68 69 70 71 |
# File 'lib/transitional_logger.rb', line 64 def reset @master_logger = nil Object.send :remove_method, :logger rescue nil if @streams $stdout, $stderr = @streams[0..1] @streams = nil end end |
.stdout ⇒ Object
Creates a logger that is accessible from all objects. The logger points to stdout. Stdout is otherwise unaffected.
38 39 40 41 |
# File 'lib/transitional_logger.rb', line 38 def stdout reset if @streams @master_logger = infect_object(Logger.new($stdout)) end |