Class: Longleaf::Logging::RedirectingLogger

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(failure_only: false, log_level: 'WARN', log_format: nil, datetime_format: nil) ⇒ RedirectingLogger

Returns a new instance of RedirectingLogger.

Parameters:

  • failure_only (Boolean) (defaults to: false)

    If set to true, only failure messages will be output to STDOUT

  • log_level (String) (defaults to: 'WARN')

    logger level used for output to STDERR

  • log_format (String) (defaults to: nil)

    format string for log entries to STDERR. There are 4 variables available for inclusion in the output: severity, datetime, progname, msg. Variables must be wrapped in %{}.

  • datetime_format (String) (defaults to: nil)

    datetime formatting string used for logger dates appearing in STDERR.



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.

Parameters:

  • eventOrMessage (String)

    name of the preservation event which failed, or the message to output if it is the only parameter.

  • file_name (String) (defaults to: nil)

    file name which is the subject of this message.

  • message (String) (defaults to: nil)

    descriptive message to accompany this output

  • service (String) (defaults to: nil)

    name of the service which executed.

  • error (Error) (defaults to: nil)

    error which occurred



92
93
94
95
96
97
98
99
# File 'lib/longleaf/logging/redirecting_logger.rb', line 92

def failure(eventOrMessage, file_name = nil, message = nil, service = nil, error: nil)
  text = outcome_text('FAILURE', eventOrMessage, file_name, message, service, error)
  @stdout_log.warn(text)

  @stderr_log.info(text)
  @stderr_log.error("#{error.message}") 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.

Parameters:

  • outcome (String)

    The status of the outcome. Required.

  • eventOrMessage (String)

    name of the preservation event which was successful, or the message to output if it is the only parameter. Required.

  • file_name (String) (defaults to: nil)

    file name which is the subject of this message.

  • message (String) (defaults to: nil)

    descriptive message to accompany this output

  • service (String) (defaults to: nil)

    name of the service which executed.

  • error (Error) (defaults to: nil)

    error which occurred



111
112
113
114
115
# File 'lib/longleaf/logging/redirecting_logger.rb', line 111

def outcome(outcome, eventOrMessage, file_name = nil, message = nil, service = nil, error = nil)
  text = outcome_text(outcome, eventOrMessage, file_name, message, 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.

Parameters:

  • eventOrMessage (String)

    name of the preservation event which succeeded, or the message to output if it is the only parameter. Required.

  • file_name (String) (defaults to: nil)

    file name which is the subject of this message.

  • message (String) (defaults to: nil)

    descriptive message to accompany this output

  • service (String) (defaults to: nil)

    name of the service which executed.



80
81
82
# File 'lib/longleaf/logging/redirecting_logger.rb', line 80

def success(eventOrMessage, file_name = nil, message = nil, service = nil)
  outcome('SUCCESS', eventOrMessage, file_name, message, 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