Module: ShellHelpers::CLILogging
- Extended by:
- CLILogging
- Included in:
- ShellHelpers, CLILogging, Shortcuts, Sh
- Defined in:
- lib/shell_helpers/logger.rb,
lib/shell_helpers/logger.bak.rb
Overview
CLILogging {{{1 Provides easier access to a shared DR::CLILogger instance. Include this module into your class, and #logger provides access to a shared logger. This is handy if you want all of your clases to have access to the same logger, but don't want to (or aren't able to) pass it around to each class. This also provides methods for direct logging without going through the
logger
=== Example
class MyClass
include DR::CLILogging
def doit
debug("About to doit!")
if results
info("We did it!")
else
error("Something went wrong")
end
debug("Done doing it")
end
end
Note that every class that mixes this in shares the same logger instance, so if you call #change_logger, this will change the logger for all classes that mix this in. This is likely what you want.
Defined Under Namespace
Modules: Shortcuts
Constant Summary collapse
- LOG_LEVELS =
logger.log_levels
Class Method Summary collapse
-
.setup_toggle_trap(signal) ⇒ Object
call CLILogging.setup_toggle_trap('USR1') to change the log level to :debug when USR1 is received.
Instance Method Summary collapse
-
#change_logger(new_logger) ⇒ Object
(also: #logger=)
Change the global logger that includers will use.
- #log_and_do(*args) ⇒ Object
-
#logger ⇒ Object
Access the shared logger.
Class Method Details
.setup_toggle_trap(signal) ⇒ Object
call CLILogging.setup_toggle_trap('USR1') to change the log level to :debug when USR1 is received
471 472 473 |
# File 'lib/shell_helpers/logger.rb', line 471 def self.setup_toggle_trap(signal) logger.setup_toggle_trap(signal) end |
Instance Method Details
#change_logger(new_logger) ⇒ Object Also known as: logger=
Change the global logger that includers will use. Useful if you don't want the default configured logger. Note that the +change_logger+ version is preferred because Ruby will often parse logger = Logger.new as the declaration of, and assignment to, of a local variable. You'd need to do self.logger=Logger.new to be sure. This method is a bit easier.
+new_logger+:: the new logger. May not be nil and should be a logger of some kind
461 462 463 464 465 |
# File 'lib/shell_helpers/logger.rb', line 461 def change_logger(new_logger) raise ArgumentError,"Logger may not be nil" if new_logger.nil? @@logger = new_logger @@logger.level = @log_level if defined?(@log_level) && @log_level end |
#log_and_do(*args) ⇒ Object
475 476 477 |
# File 'lib/shell_helpers/logger.rb', line 475 def log_and_do(*args,**kws) logger.log_and_do(*args,**kws) end |
#logger ⇒ Object
Access the shared logger. All classes that include this module will get the same logger via this method.
442 443 444 445 446 447 448 |
# File 'lib/shell_helpers/logger.rb', line 442 def logger unless CLILogging.class_variable_defined?(:@@logger) @@logger = CLILogger.new @@logger.progname=$0 end @@logger end |