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

Instance Method Summary collapse

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



469
470
471
# File 'lib/shell_helpers/logger.rb', line 469

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

Raises:



459
460
461
462
463
# File 'lib/shell_helpers/logger.rb', line 459

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



473
474
475
# File 'lib/shell_helpers/logger.rb', line 473

def log_and_do(*args,**kws)
	logger.log_and_do(*args,**kws)
end

#loggerObject

Access the shared logger. All classes that include this module will get the same logger via this method.



440
441
442
443
444
445
446
# File 'lib/shell_helpers/logger.rb', line 440

def logger
	unless CLILogging.class_variable_defined?(:@@logger)
		@@logger = CLILogger.new
		@@logger.progname=$0
	end
	@@logger
end