Module: Bolt::Logger

Defined in:
lib/bolt/logger.rb

Class Method Summary collapse

Class Method Details

.configure(destinations, color) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/bolt/logger.rb', line 30

def self.configure(destinations, color)
  root_logger = Logging.logger[:root]

  root_logger.add_appenders Logging.appenders.stderr(
    'console',
    layout: console_layout(color),
    level: default_console_level
  )

  # We set the root logger's level so that it logs everything but we do
  # limit what's actually logged in every appender individually.
  root_logger.level = :all

  destinations.each_pair do |name, params|
    appender = Logging.appenders[name]
    if appender.nil?
      unless name.start_with?('file:')
        raise Bolt::Error.new("Unexpected log: #{name}", 'bolt/internal-error')
      end

      begin
        appender = Logging.appenders.file(
          name,
          filename: name[5..-1], # strip the "file:" prefix
          truncate: (params[:append] == false),
          layout: default_layout,
          level: default_file_level
        )
      rescue ArgumentError => e
        raise Bolt::Error.new("Failed to open log #{name}: #{e.message}", 'bolt/log-error')
      end

      root_logger.add_appenders appender
    end

    appender.level = params[:level] if params[:level]
  end
end

.console_layout(color) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/bolt/logger.rb', line 69

def self.console_layout(color)
  color_scheme = :bolt if color
  Logging.layouts.pattern(
    pattern: '%m\e[0m\n',
    color_scheme: color_scheme
  )
end

.default_console_levelObject



84
85
86
# File 'lib/bolt/logger.rb', line 84

def self.default_console_level
  :warn
end

.default_file_levelObject



88
89
90
# File 'lib/bolt/logger.rb', line 88

def self.default_file_level
  :notice
end

.default_layoutObject



77
78
79
80
81
82
# File 'lib/bolt/logger.rb', line 77

def self.default_layout
  Logging.layouts.pattern(
    pattern: '%d %-6l %c: %m\n',
    date_pattern: '%Y-%m-%dT%H:%M:%S.%6N'
  )
end

.initialize_loggingObject

This method provides a single point-of-entry to setup logging for both the CLI and for tests. This is necessary because we define custom log levels which create corresponding methods on the logger instances; without first initializing the Logging system, calls to those methods will fail.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/bolt/logger.rb', line 12

def self.initialize_logging
  # Initialization isn't idempotent and will result in warnings about const
  # redefs, so skip it if it's already been initialized
  return if Logging.initialized?

  Logging.init :debug, :info, :notice, :warn, :error, :fatal, :any

  Logging.color_scheme(
    'bolt',
    lines: {
      notice: :green,
      warn: :yellow,
      error: :red,
      fatal: %i[white on_red]
    }
  )
end

.levelsObject



96
97
98
# File 'lib/bolt/logger.rb', line 96

def self.levels
  Logging::LNAMES.map(&:downcase)
end

.reset_loggingObject



100
101
102
# File 'lib/bolt/logger.rb', line 100

def self.reset_logging
  Logging.reset
end

.valid_level?(level) ⇒ Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/bolt/logger.rb', line 92

def self.valid_level?(level)
  !Logging.level_num(level).nil?
end