module Rzo::Logging
Support module to mix into a class for consistent logging behavior.
Public Class Methods
Logging
is handled centrally, the helper methods will delegate to the centrally configured logging instance.
# File lib/rzo/logging.rb, line 53 def self.log @log || reset_logging!(opts) end
Map a file option to STDOUT, STDERR or a fully qualified file path.
@param [String] filepath A relative or fully qualified file path, or the
keyword strings 'STDOUT' or 'STDERR'
@return [String] file path or $stdout or $sederr
# File lib/rzo/logging.rb, line 64 def self.map_file_option(filepath) case filepath when 'STDOUT' then $stdout when 'STDERR' then $stderr when 'STDIN' then $stdin when 'STRING' then StringIO.new else File.expand_path(filepath) end end
Reset the global logger instance and return it as an object.
@return [Logger] initialized logging instance
# File lib/rzo/logging.rb, line 11 def self.reset_logging!(opts) logger = opts[:syslog] ? syslog_logger(opts) : stream_logger(opts) @log = logger end
Return a new Logger instance configured for file output
# File lib/rzo/logging.rb, line 41 def self.stream_logger(opts) out = map_file_option(opts[:logto]) logger = Logger.new(out) logger.level = Logger::WARN logger.level = Logger::INFO if opts[:verbose] logger.level = Logger::DEBUG if opts[:debug] logger end
Return a new Syslog::Logger instance configured for syslog output rubocop:disable Metrics/MethodLength
# File lib/rzo/logging.rb, line 19 def self.syslog_logger(opts) begin require 'syslog/logger' have_syslog = true rescue LoadError have_syslog = false end if have_syslog # Use the daemon facility, matching Puppet behavior. Syslog::Logger.new('rzo', Syslog::LOG_DAEMON) else logger = stream_logger(opts) logger.warn('Syslog is not available. Falling back to stream logging.') unless @syslog_warned @syslog_warned = true logger end end
Public Instance Methods
Logs a message at the debug (syslog debug) log level i.e. Information useful to developers for debugging the application.
# File lib/rzo/logging.rb, line 123 def debug(msg) log.debug msg end
Logs a message at the error (syslog warning) log level. i.e. May indicate that an error will occur if action is not taken. e.g. A non-root file system has only 2GB remaining.
# File lib/rzo/logging.rb, line 101 def error(msg) log.error msg end
Logs a message at the fatal (syslog err) log level
# File lib/rzo/logging.rb, line 93 def fatal(msg) log.fatal msg end
Logs a message at the info (syslog info) log level i.e. Normal operational messages that require no action. e.g. An application has started, paused or ended successfully.
# File lib/rzo/logging.rb, line 116 def info(msg) log.info msg end
Helper method to read from STDIN, or a file and execute an arbitrary block of code. A block must be passed which will recieve an IO object in the event input is a readable file path.
# File lib/rzo/logging.rb, line 143 def input_stream(input) if input.is_a?(IO) yield input else File.open(input, 'r') { |stream| yield stream } end end
# File lib/rzo/logging.rb, line 78 def log ::Rzo::Logging.log end
# File lib/rzo/logging.rb, line 74 def map_file_option(filepath) ::Rzo::Logging.map_file_option(filepath) end
Reset the logging system, requires command line options to have been parsed.
@param [Hash<Symbol, String>] opts Options hash, passed to the support module
# File lib/rzo/logging.rb, line 87 def reset_logging!(opts) ::Rzo::Logging.reset_logging!(opts) end
Alternative to puts, writes output to STDERR by default and logs at level info.
# File lib/rzo/logging.rb, line 154 def say(msg) log.info(msg) @stderr.puts(msg) end
Logs a message at the warn (syslog notice) log level. e.g. Events that are unusual, but not error conditions.
# File lib/rzo/logging.rb, line 108 def warn(msg) log.warn msg end
Helper method to write output, used for stubbing out the tests.
@param [String, IO] output the output path or a IO stream
# File lib/rzo/logging.rb, line 131 def write_output(str, output) if output.is_a?(IO) output.puts(str) else File.open(output, 'w+') { |f| f.puts(str) } end end