Class: ShellHelpers::CLILogger
- Inherits:
-
ColorLogger
- Object
- Logger
- MoreLogger
- ColorLogger
- ShellHelpers::CLILogger
- Defined in:
- lib/shell_helpers/logger.rb,
lib/shell_helpers/logger.bak.rb
Overview
CLILogger {{{1 A Logger instance that gives better control of messaging the user and logging app activity. At it's most basic, you would use info as a replacement for +puts+ and error as a replacement for STDERR.puts. Since this is a logger, however, you can also use #debug, #warn, and #fatal, and you can control the format and "logging level" as such.
So, by default:
- debug messages do not appear anywhere
- info messages appear on the standard output
- warn, error, and fatal message appear on the standard error
- The default format of messages is simply the message, no logging cruft, however if your output is redirected to a file, a better timestamped logging format is used
You can customize this in several ways:
- You can override the devices used by passing different devices to the constructor
- You can adjust the level of message that goes to the error logger via error_level=
- You can adjust the format for messages to the error logger separately via error_formatter=
=== Example
logger = CLILogger.new
logger.debug("Starting up") # => only the standard output gets this
logger.warn("careful!") # => only the standard error gets this
logger.error("Something went wrong!") # => only the standard error gets this
logger = CLILogger.new
logger.error_level = Logger::ERROR
logger.debug("Starting up") # => only the standard output gets this
logger.warn("careful!") # => only the standard OUTPUT gets this
logger.error("Something went wrong!") # => only the standard error gets this
logger = CLILogger.new('logfile.txt')
logger.debug("Starting up") #=> logfile.txt gets this
logger.error("Something went wrong!") # => BOTH logfile.txt AND the standard error get this
Constant Summary collapse
- DEFAULT_ERROR_LEVEL =
Logger::Severity::WARN
- BLANK_FORMAT =
lambda { |severity,datetime,progname,msg| msg + "\n" }
Constants inherited from ColorLogger
ShellHelpers::ColorLogger::CLI_COLORS, ShellHelpers::ColorLogger::CLI_COLORS_BASE, ShellHelpers::ColorLogger::ColorLoggerError, ShellHelpers::ColorLogger::LOG_LEVELS, ShellHelpers::ColorLogger::WrongLevel
Constants inherited from MoreLogger
MoreLogger::LOG_LEVELS, MoreLogger::WrongLevel
Instance Attribute Summary
Attributes inherited from ColorLogger
#default_formatter, #default_lvl, #quiet_lvl, #raw, #verbose_lvl
Attributes inherited from MoreLogger
Class Method Summary collapse
-
.proxy_method(symbol) ⇒ Object
Helper to proxy methods to the super class AND to the internal error logger +symbol+:: Symbol for name of the method to proxy.
Instance Method Summary collapse
-
#add(severity, message = nil, progname = nil, **opts, &block) ⇒ Object
:nodoc:.
- #cli_level(*args) ⇒ Object
-
#error_formatter=(formatter) ⇒ Object
Overrides the formatter for the error logger.
-
#error_level=(level) ⇒ Object
Set the threshold for what messages go to the error device.
-
#initialize(log_device = $stdout, error_device = $stderr, split_log: :auto, default_error: DEFAULT_ERROR_LEVEL, **kwds) {|_self, @stderr_logger| ... } ⇒ CLILogger
constructor
A logger that logs error-type messages to a second device; useful for ensuring that error messages go to standard error.
- #level=(level) ⇒ Object
-
#log_and_do(*args, severity: INFO, definee: self, **opts, &block) ⇒ Object
log the action and execute it Severity is Logger:: DEBUG < INFO < WARN < ERROR < FATAL < UNKNOWN.
-
#setup_toggle_trap(signal) ⇒ Object
call logger.setup_toggle_trap('USR1') to change the log level to :debug when USR1 is received.
Methods inherited from ColorLogger
#cli_colors, #datetime_format, #datetime_format=, #format_message, #formatter=, #get_formatter, #log_levels, #severity, #severity_lvl
Methods inherited from MoreLogger
#log_levels, #severity, #severity_lvl
Constructor Details
#initialize(log_device = $stdout, error_device = $stderr, split_log: :auto, default_error: DEFAULT_ERROR_LEVEL, **kwds) {|_self, @stderr_logger| ... } ⇒ CLILogger
A logger that logs error-type messages to a second device; useful for ensuring that error messages go to standard error. This should be pretty smart about doing the right thing. If both log devices are ttys, e.g. one is going to standard error and the other to the standard output, messages only appear once in the overall output stream. In other words, an ERROR logged will show up only in the standard error. If either log device is NOT a tty, then all messages go to +log_device+ and only errors go to +error_device+
+log_device+:: device where all log messages should go, based on level By default, this is Logger::Severity::WARN +error_device+:: device where all error messages should go.
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 |
# File 'lib/shell_helpers/logger.rb', line 313 def initialize(log_device=$stdout,error_device=$stderr, split_log: :auto, default_error_lvl: DEFAULT_ERROR_LEVEL, **kwds) @stderr_logger = ColorLogger.new(error_device, default_lvl: default_error_lvl, **kwds) log_device_tty = tty?(log_device) error_device_tty = tty?(error_device) @split_logs = log_device_tty && error_device_tty if split_log==:auto super(log_device, **kwds) self.default_formatter = ColorFormatter.create(:color) if log_device_tty @stderr_logger.default_formatter = ColorFormatter.create(:color) if error_device_tty yield self, @stderr_logger if block_given? end |
Class Method Details
.proxy_method(symbol) ⇒ Object
Helper to proxy methods to the super class AND to the internal error logger +symbol+:: Symbol for name of the method to proxy
273 274 275 276 277 278 279 280 |
# File 'lib/shell_helpers/logger.rb', line 273 def self.proxy_method(symbol) #:nodoc: old_name = "old_#{symbol}".to_sym alias_method old_name,symbol define_method symbol do |*args,&block| send(old_name,*args,&block) @stderr_logger.send(symbol,*args,&block) end end |
Instance Method Details
#add(severity, message = nil, progname = nil, **opts, &block) ⇒ Object
:nodoc:
286 287 288 289 290 291 292 293 294 295 296 297 |
# File 'lib/shell_helpers/logger.rb', line 286 def add(severity, = nil, **opts, &block) #:nodoc: severity_lvl = severity_lvl(severity) if @split_logs unless severity_lvl >= @stderr_logger.level super(severity,, **opts, &block) end else super(severity,,**opts, &block) end severity = severity_lvl if severity == true @stderr_logger.add(severity,,**opts, &block) end |
#cli_level(*args) ⇒ Object
347 348 349 350 |
# File 'lib/shell_helpers/logger.rb', line 347 def cli_level(*args) super adjust_stderr_level end |
#error_formatter=(formatter) ⇒ Object
Overrides the formatter for the error logger. A future call to
formatter= will affect both, so the order of the calls matters.
+formatter+:: Proc that handles the formatting, the same as for #formatter=
363 364 365 |
# File 'lib/shell_helpers/logger.rb', line 363 def error_formatter=(formatter) @stderr_logger.formatter=formatter end |
#error_level=(level) ⇒ Object
Set the threshold for what messages go to the error device. Note that calling #level= will not affect the error logger unless both devices are TTYs. +level+:: a constant from Logger::Severity for the level of messages that should go to the error logger
356 357 358 |
# File 'lib/shell_helpers/logger.rb', line 356 def error_level=(level) @stderr_logger.level = level end |
#level=(level) ⇒ Object
342 343 344 345 |
# File 'lib/shell_helpers/logger.rb', line 342 def level=(level) super adjust_stderr_level end |
#log_and_do(*args, severity: INFO, definee: self, **opts, &block) ⇒ Object
log the action and execute it Severity is Logger:: DEBUG < INFO < WARN < ERROR < FATAL < UNKNOWN
374 375 376 377 378 379 380 381 382 383 384 |
# File 'lib/shell_helpers/logger.rb', line 374 def log_and_do(*args, severity: INFO, definee: self, **opts, &block) msg="log_and_do #{args} on #{self}" msg+=" with options #{opts}" unless opts.empty? msg+=" with block #{block}" if block add(severity,msg) if opts.empty? definee.send(*args, &block) else definee.send(*args, **opts, &block) end end |
#setup_toggle_trap(signal) ⇒ Object
call logger.setup_toggle_trap('USR1') to change the log level to :debug when USR1 is received
399 400 401 402 403 404 405 |
# File 'lib/shell_helpers/logger.rb', line 399 def setup_toggle_trap(signal) if signal Signal.trap(signal) do toggle_log_level end end end |