Module: Bolt::Logger

Defined in:
lib/bolt/logger.rb

Class Method Summary collapse

Class Method Details

.configure(config) ⇒ 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
# File 'lib/bolt/logger.rb', line 30

def self.configure(config)
  root_logger = Logging.logger[:root]

  config[:log].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_level
        )
      rescue ArgumentError => e
        raise Bolt::CLIError, "Failed to open log #{name}: #{e.message}"
      end

      root_logger.add_appenders appender
    end

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

.default_layoutObject



59
60
61
62
63
64
# File 'lib/bolt/logger.rb', line 59

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

.default_levelObject



66
67
68
# File 'lib/bolt/logger.rb', line 66

def self.default_level
  :notice
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

  root_logger = Logging.logger[:root]
  root_logger.add_appenders Logging.appenders.stderr(
    'console',
    layout: default_layout,
    level: default_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
end

.levelsObject



74
75
76
# File 'lib/bolt/logger.rb', line 74

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

.reset_loggingObject



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

def self.reset_logging
  Logging.reset
end

.valid_level?(level) ⇒ Boolean

Returns:

  • (Boolean)


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

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