Class: Longleaf::Logging::RedirectingLogger
- Inherits:
-
Object
- Object
- Longleaf::Logging::RedirectingLogger
- Defined in:
- lib/longleaf/logging/redirecting_logger.rb
Overview
Logger which directs messages to stdout and/or stderr, depending on the nature of the message. Status logging, which includes standard logger methods, goes to STDERR. Operation success and failure messages go to STDOUT, and to STDERR at info level.
Instance Method Summary collapse
- #<<(msg) ⇒ Object
- #debug(progname = nil, &block) ⇒ Object
- #error(progname = nil, &block) ⇒ Object
-
#failure(eventOrMessage, file_name = nil, message = nil, service = nil, error: nil) ⇒ Object
Logs a failure message to STDOUT, as well as STDERR at info level.
- #fatal(progname = nil, &block) ⇒ Object
- #info(progname = nil, &block) ⇒ Object
-
#initialize(failure_only: false, log_level: 'WARN', log_format: nil, datetime_format: nil) ⇒ RedirectingLogger
constructor
A new instance of RedirectingLogger.
-
#outcome(outcome, eventOrMessage, file_name = nil, message = nil, service = nil, error = nil) ⇒ Object
Logs an outcome message to STDOUT, as well as STDERR at info level.
-
#success(eventOrMessage, file_name = nil, message = nil, service = nil) ⇒ Object
Logs a success message to STDOUT, as well as STDERR at info level.
- #unknown(progname = nil, &block) ⇒ Object
- #warn(progname = nil, &block) ⇒ Object
Constructor Details
#initialize(failure_only: false, log_level: 'WARN', log_format: nil, datetime_format: nil) ⇒ RedirectingLogger
Returns a new instance of RedirectingLogger.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/longleaf/logging/redirecting_logger.rb', line 14 def initialize(failure_only: false, log_level: 'WARN', log_format: nil, datetime_format: nil) @stderr_log = Logger.new($stderr) @stderr_log.level = log_level @stderr_log.datetime_format = datetime_format @log_format = log_format if @log_format.nil? @stderr_log.formatter = proc do |severity, datetime, progname, msg| formatted_date = @stderr_log.datetime_format.nil? ? datetime : datetime.strftime(datetime_format) "#{severity} [#{formatted_date}]: #{msg}\n" end elsif @log_format.is_a?(String) @stderr_log.formatter = proc do |severity, datetime, progname, msg| # Make sure the format ends with a newline @log_format = @log_format + "\n" unless @log_format.end_with?("\n") formatted_date = @stderr_log.datetime_format.nil? ? datetime : datetime.strftime(datetime_format) @log_format % { :severity => severity, :datetime => formatted_date, :progname => progname, :msg => msg } end end @stdout_log = Logger.new($stdout) @stdout_log.formatter = proc do |severity, datetime, progname, msg| "#{msg}\n" end if failure_only @stdout_log.level = 'warn' else @stdout_log.level = 'info' end end |
Instance Method Details
#<<(msg) ⇒ Object
69 70 71 |
# File 'lib/longleaf/logging/redirecting_logger.rb', line 69 def <<(msg) @stderr_log << msg end |
#debug(progname = nil, &block) ⇒ Object
45 46 47 |
# File 'lib/longleaf/logging/redirecting_logger.rb', line 45 def debug(progname = nil, &block) @stderr_log.debug(progname, &block) end |
#error(progname = nil, &block) ⇒ Object
57 58 59 |
# File 'lib/longleaf/logging/redirecting_logger.rb', line 57 def error(progname = nil, &block) @stderr_log.error(progname, &block) end |
#failure(eventOrMessage, file_name = nil, message = nil, service = nil, error: nil) ⇒ Object
Logs a failure message to STDOUT, as well as STDERR at info level. If an error was provided, it is logged to STDERR at error level.
92 93 94 95 96 97 98 99 |
# File 'lib/longleaf/logging/redirecting_logger.rb', line 92 def failure(eventOrMessage, file_name = nil, = nil, service = nil, error: nil) text = outcome_text('FAILURE', eventOrMessage, file_name, , service, error) @stdout_log.warn(text) @stderr_log.info(text) @stderr_log.error("#{error.}") unless error.nil? @stderr_log.error("#{error.backtrace}") unless error.nil? || error.backtrace.nil? end |
#fatal(progname = nil, &block) ⇒ Object
61 62 63 |
# File 'lib/longleaf/logging/redirecting_logger.rb', line 61 def fatal(progname = nil, &block) @stderr_log.fatal(progname, &block) end |
#info(progname = nil, &block) ⇒ Object
49 50 51 |
# File 'lib/longleaf/logging/redirecting_logger.rb', line 49 def info(progname = nil, &block) @stderr_log.info(progname, &block) end |
#outcome(outcome, eventOrMessage, file_name = nil, message = nil, service = nil, error = nil) ⇒ Object
Logs an outcome message to STDOUT, as well as STDERR at info level. If file_name and message are nil, eventOrMessage will be used as the message.
111 112 113 114 115 |
# File 'lib/longleaf/logging/redirecting_logger.rb', line 111 def outcome(outcome, eventOrMessage, file_name = nil, = nil, service = nil, error = nil) text = outcome_text(outcome, eventOrMessage, file_name, , service, error) @stdout_log.info(text) @stderr_log.info(text) end |
#success(eventOrMessage, file_name = nil, message = nil, service = nil) ⇒ Object
Logs a success message to STDOUT, as well as STDERR at info level.
80 81 82 |
# File 'lib/longleaf/logging/redirecting_logger.rb', line 80 def success(eventOrMessage, file_name = nil, = nil, service = nil) outcome('SUCCESS', eventOrMessage, file_name, , service) end |
#unknown(progname = nil, &block) ⇒ Object
65 66 67 |
# File 'lib/longleaf/logging/redirecting_logger.rb', line 65 def unknown(progname = nil, &block) @stderr_log.unknown(progname, &block) end |
#warn(progname = nil, &block) ⇒ Object
53 54 55 |
# File 'lib/longleaf/logging/redirecting_logger.rb', line 53 def warn(progname = nil, &block) @stderr_log.warn(progname, &block) end |