Module: LogSwitch
- Defined in:
- lib/log_switch.rb,
lib/log_switch/mixin.rb,
lib/log_switch/version.rb
Overview
LogSwitch allows for extending a class/module with a logger and, most importantly, allows for turning off logging programmatically. See the README.rdoc
for more info.
Defined Under Namespace
Modules: Mixin
Constant Summary collapse
- VERSION =
'0.4.0'
Instance Attribute Summary collapse
-
#log(message, level = log_level) ⇒ Object
Logs a message using the level provided.
-
#log_class_name ⇒ Object
writeonly
Toggle prepending the class name of the #log caller to the log message.
-
#log_level ⇒ Symbol
The current default log level.
-
#logger ⇒ Object
Defaults to a
Logger
writing to STDOUT.
Class Method Summary collapse
-
.extend_object(base) ⇒ Object
Saves the name of the class that extended itself with this module.
-
.extender ⇒ Class
Simply returns the name of the class that extended itself with this module.
Instance Method Summary collapse
-
#before(&block) ⇒ Object
#log calls the block given to this method before it logs every time.
-
#log? ⇒ Boolean
Tells whether logging is turned on or not.
-
#log_class_name? ⇒ Boolean
Tells whether logging of the class name with the log message is turned on.
-
#reset_config! ⇒ Object
Sets back to defaults.
Instance Attribute Details
#log(message, level = log_level) ⇒ Object
Logs a message using the level provided. If no level provided, use @log_level.
74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/log_switch.rb', line 74 def log(, level=log_level) before.call unless before.nil? yield if block_given? if log? if .respond_to? :each_line .each_line { |line| logger.send level, line.chomp } else logger.send(level, ) end end end |
#log_class_name=(value) ⇒ Object (writeonly)
Toggle prepending the class name of the #log caller to the log message.
35 36 37 |
# File 'lib/log_switch.rb', line 35 def log_class_name=(value) @log_class_name = value end |
#log_level ⇒ Symbol
Returns The current default log level. Starts off as :debug.
48 49 50 |
# File 'lib/log_switch.rb', line 48 def log_level @log_level ||= :debug end |
#logger ⇒ Object
Defaults to a Logger
writing to STDOUT.
43 44 45 |
# File 'lib/log_switch.rb', line 43 def logger @logger ||= ::Logger.new STDOUT end |
Class Method Details
.extend_object(base) ⇒ Object
Saves the name of the class that extended itself with this module. Used by Mixin to know which class to include itself to.
12 13 14 15 |
# File 'lib/log_switch.rb', line 12 def self.extend_object(base) @extender = base super(base) end |
.extender ⇒ Class
Simply returns the name of the class that extended itself with this module. It’s set by extend_object.
21 22 23 |
# File 'lib/log_switch.rb', line 21 def self.extender @extender end |
Instance Method Details
#before(&block) ⇒ Object
#log calls the block given to this method before it logs every time. This, thus, acts as a hook in the case where you want to make sure some code gets executed before you log a message. Useful for making sure a file exists before logging to it.
65 66 67 |
# File 'lib/log_switch.rb', line 65 def before(&block) @before_block ||= block end |
#log? ⇒ Boolean
Tells whether logging is turned on or not.
38 39 40 |
# File 'lib/log_switch.rb', line 38 def log? @log != false end |
#log_class_name? ⇒ Boolean
Returns Tells whether logging of the class name with the log message is turned on.
54 55 56 |
# File 'lib/log_switch.rb', line 54 def log_class_name? @log_class_name == true end |
#reset_config! ⇒ Object
Sets back to defaults.
88 89 90 91 92 |
# File 'lib/log_switch.rb', line 88 def reset_config! self.log = true self.logger = ::Logger.new STDOUT self.log_class_name = false end |