Module: Rzo::Logging
- Included in:
- App, App::Subcommand, App::Subcommand
- Defined in:
- lib/rzo/logging.rb
Overview
Support module to mix into a class for consistent logging behavior.
Class Method Summary collapse
-
.log ⇒ Object
Logging is handled centrally, the helper methods will delegate to the centrally configured logging instance.
-
.map_file_option(filepath) ⇒ String
Map a file option to STDOUT, STDERR or a fully qualified file path.
-
.reset_logging!(opts) ⇒ Logger
Reset the global logger instance and return it as an object.
-
.stream_logger(opts) ⇒ Object
Return a new Logger instance configured for file output.
-
.syslog_logger(opts) ⇒ Object
Return a new Syslog::Logger instance configured for syslog output rubocop:disable Metrics/MethodLength.
Instance Method Summary collapse
-
#debug(msg) ⇒ Object
Logs a message at the debug (syslog debug) log level i.e.
-
#error(msg) ⇒ Object
Logs a message at the error (syslog warning) log level.
-
#fatal(msg) ⇒ Object
Logs a message at the fatal (syslog err) log level.
-
#info(msg) ⇒ Object
Logs a message at the info (syslog info) log level i.e.
-
#input_stream(input) ⇒ Object
Helper method to read from STDIN, or a file and execute an arbitrary block of code.
- #log ⇒ Object
- #map_file_option(filepath) ⇒ Object
-
#reset_logging!(opts) ⇒ Object
Reset the logging system, requires command line options to have been parsed.
-
#say(msg) ⇒ Object
Alternative to puts, writes output to STDERR by default and logs at level info.
-
#warn(msg) ⇒ Object
Logs a message at the warn (syslog notice) log level.
-
#write_output(str, output) ⇒ Object
Helper method to write output, used for stubbing out the tests.
Class Method Details
.log ⇒ Object
Logging is handled centrally, the helper methods will delegate to the centrally configured logging instance.
53 54 55 |
# File 'lib/rzo/logging.rb', line 53 def self.log @log || reset_logging!(opts) end |
.map_file_option(filepath) ⇒ String
Map a file option to STDOUT, STDERR or a fully qualified file path.
64 65 66 67 68 69 70 71 72 |
# 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.(filepath) end end |
.reset_logging!(opts) ⇒ Logger
Reset the global logger instance and return it as an object.
11 12 13 14 |
# File 'lib/rzo/logging.rb', line 11 def self.reset_logging!(opts) logger = opts[:syslog] ? syslog_logger(opts) : stream_logger(opts) @log = logger end |
.stream_logger(opts) ⇒ Object
Return a new Logger instance configured for file output
41 42 43 44 45 46 47 48 |
# 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 |
.syslog_logger(opts) ⇒ Object
Return a new Syslog::Logger instance configured for syslog output rubocop:disable Metrics/MethodLength
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# 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 |
Instance Method Details
#debug(msg) ⇒ Object
Logs a message at the debug (syslog debug) log level i.e. Information useful to developers for debugging the application.
123 124 125 |
# File 'lib/rzo/logging.rb', line 123 def debug(msg) log.debug msg end |
#error(msg) ⇒ Object
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.
101 102 103 |
# File 'lib/rzo/logging.rb', line 101 def error(msg) log.error msg end |
#fatal(msg) ⇒ Object
Logs a message at the fatal (syslog err) log level
93 94 95 |
# File 'lib/rzo/logging.rb', line 93 def fatal(msg) log.fatal msg end |
#info(msg) ⇒ Object
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.
116 117 118 |
# File 'lib/rzo/logging.rb', line 116 def info(msg) log.info msg end |
#input_stream(input) ⇒ Object
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.
143 144 145 146 147 148 149 |
# 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 |
#map_file_option(filepath) ⇒ Object
74 75 76 |
# File 'lib/rzo/logging.rb', line 74 def map_file_option(filepath) ::Rzo::Logging.map_file_option(filepath) end |
#reset_logging!(opts) ⇒ Object
Reset the logging system, requires command line options to have been parsed.
87 88 89 |
# File 'lib/rzo/logging.rb', line 87 def reset_logging!(opts) ::Rzo::Logging.reset_logging!(opts) end |
#say(msg) ⇒ Object
Alternative to puts, writes output to STDERR by default and logs at level info.
154 155 156 157 |
# File 'lib/rzo/logging.rb', line 154 def say(msg) log.info(msg) @stderr.puts(msg) end |
#warn(msg) ⇒ Object
Logs a message at the warn (syslog notice) log level. e.g. Events that are unusual, but not error conditions.
108 109 110 |
# File 'lib/rzo/logging.rb', line 108 def warn(msg) log.warn msg end |
#write_output(str, output) ⇒ Object
Helper method to write output, used for stubbing out the tests.
131 132 133 134 135 136 137 |
# 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 |