Module: Methadone::CLILogging
- Defined in:
- lib/methadone/cli_logging.rb
Overview
Provides easier access to a shared Methadone::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 Methadone::CLILogger
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.
Constant Summary collapse
- LOG_LEVELS =
{ 'debug' => Logger::DEBUG, 'info' => Logger::INFO, 'warn' => Logger::WARN, 'error' => Logger::ERROR, 'fatal' => Logger::FATAL, }
Class Method Summary collapse
Instance Method Summary collapse
-
#change_logger(new_logger) ⇒ Object
(also: #logger=)
Change the global logger that includers will use.
-
#debug(progname = nil, &block) ⇒ Object
pass-through to
logger.debug(progname,&block)
. -
#error(progname = nil, &block) ⇒ Object
pass-through to
logger.error(progname,&block)
. -
#fatal(progname = nil, &block) ⇒ Object
pass-through to
logger.fatal(progname,&block)
. -
#info(progname = nil, &block) ⇒ Object
pass-through to
logger.info(progname,&block)
. -
#logger ⇒ Object
Access the shared logger.
-
#use_log_level_option(args = {}) ⇒ Object
Call this if you’ve included Methadone::Main to set up a
--log-level
option for your app that will allow the user to configure the logging level. -
#warn(progname = nil, &block) ⇒ Object
pass-through to
logger.warn(progname,&block)
.
Class Method Details
.included(k) ⇒ Object
30 31 32 |
# File 'lib/methadone/cli_logging.rb', line 30 def self.included(k) k.extend(self) 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
48 49 50 51 52 |
# File 'lib/methadone/cli_logging.rb', line 48 def change_logger(new_logger) raise ArgumentError,"Logger may not be nil" if new_logger.nil? @@logger = new_logger @@logger.level = @log_level if @log_level end |
#debug(progname = nil, &block) ⇒ Object
pass-through to logger.debug(progname,&block)
58 |
# File 'lib/methadone/cli_logging.rb', line 58 def debug(progname = nil, &block); logger.debug(progname,&block); end |
#error(progname = nil, &block) ⇒ Object
pass-through to logger.error(progname,&block)
64 |
# File 'lib/methadone/cli_logging.rb', line 64 def error(progname = nil, &block); logger.error(progname,&block); end |
#fatal(progname = nil, &block) ⇒ Object
pass-through to logger.fatal(progname,&block)
66 |
# File 'lib/methadone/cli_logging.rb', line 66 def fatal(progname = nil, &block); logger.fatal(progname,&block); end |
#info(progname = nil, &block) ⇒ Object
pass-through to logger.info(progname,&block)
60 |
# File 'lib/methadone/cli_logging.rb', line 60 def info(progname = nil, &block); logger.info(progname,&block); end |
#logger ⇒ Object
Access the shared logger. All classes that include this module will get the same logger via this method.
36 37 38 |
# File 'lib/methadone/cli_logging.rb', line 36 def logger @@logger ||= CLILogger.new end |
#use_log_level_option(args = {}) ⇒ Object
Call this if you’ve included Methadone::Main to set up a --log-level
option for your app that will allow the user to configure the logging level. You can pass an optional hash with :toggle_debug_on_signal => <SIGNAME>
to enable runtime toggling of the log level by sending the signal <SIGNAME>
to your app
args
-
optional hash
Example:
main do
# your app
end
use_log_level_option
go!
Example with runtime toggling:
main do
# your app
end
use_log_level_option :toggle_debug_on_signal => 'USR1'
go!
103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/methadone/cli_logging.rb', line 103 def use_log_level_option(args = {}) on("--log-level LEVEL",LOG_LEVELS,'Set the logging level', '(' + LOG_LEVELS.keys.join('|') + ')', '(Default: info)', :global) do |level| @log_level = level @log_level_original = level @log_level_toggled = false logger.level = level setup_toggle_trap(args[:toggle_debug_on_signal]) end end |
#warn(progname = nil, &block) ⇒ Object
pass-through to logger.warn(progname,&block)
62 |
# File 'lib/methadone/cli_logging.rb', line 62 def warn(progname = nil, &block); logger.warn(progname,&block); end |