Class: Imap::Backup::Logger

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/imap/backup/logger.rb

Overview

Wraps the standard logger, providing configuration and sanitization

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLogger

Returns a new instance of Logger.



72
73
74
75
# File 'lib/imap/backup/logger.rb', line 72

def initialize
  @logger = ::Logger.new($stdout)
  $stdout.sync = true
end

Instance Attribute Details

#loggerLogger (readonly)

Returns the configured Logger.

Returns:

  • (Logger)

    the configured Logger



70
71
72
# File 'lib/imap/backup/logger.rb', line 70

def logger
  @logger
end

Class Method Details

.count(verbose) ⇒ Object



65
66
67
# File 'lib/imap/backup/logger.rb', line 65

def self.count(verbose)
  verbose.reduce(1) { |acc, v| acc + (v ? 1 : -1) }
end

.loggerImap::Backup::Logger

Returns the singleton instance of this class.

Returns:



15
16
17
# File 'lib/imap/backup/logger.rb', line 15

def self.logger
  Logger.instance.logger
end

.sanitize_stderr(&block) ⇒ void

This method returns an undefined value.

Wraps a block, filtering output to standard error, hidng passwords and outputs the results to standard out



54
55
56
57
58
59
60
61
62
# File 'lib/imap/backup/logger.rb', line 54

def self.sanitize_stderr(&block)
  sanitizer = Text::Sanitizer.new($stdout)
  previous_stderr = $stderr
  $stderr = sanitizer
  block.call
ensure
  sanitizer.flush
  $stderr = previous_stderr
end

.setup_logging(options = {}) ⇒ Hash

Returns the options without the :quiet and :verbose keys.

Parameters:

  • options (Hash) (defaults to: {})

    command-line options

Options Hash (options):

  • :quiet (Boolean) — default: false

    if true, no output will be written

  • :verbose (Array<Boolean>) — default: []

    counts how many ‘–verbose` parameters were passed (and, potentially subtracts the number of `–no-verbose` parameters). If the result is 0, does normal info-level logging, If the result is 1, does debug logging, If the result is 2, does debug logging and client-server debug logging. This option is overridden by the `:verbose` option.

Returns:

  • (Hash)

    the options without the :quiet and :verbose keys



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/imap/backup/logger.rb', line 30

def self.setup_logging(options = {})
  copy = options.clone
  quiet = copy.delete(:quiet)
  verbose = copy.delete(:verbose) || []
  verbose_count = count(verbose)
  level =
    case
    when quiet
      ::Logger::Severity::UNKNOWN
    when verbose_count >= 2
      ::Logger::Severity::DEBUG
    else
      ::Logger::Severity::INFO
    end
  logger.level = level

  Net::IMAP.debug = (verbose_count >= 3)

  copy
end