Module: R10K::Logging

Defined Under Namespace

Classes: TerminalOutputter

Constant Summary collapse

LOG_LEVELS =
%w{DEBUG2 DEBUG1 DEBUG INFO NOTICE WARN ERROR FATAL}
SYSLOG_LEVELS_MAP =
{
  'DEBUG2' => 'DEBUG',
  'DEBUG1' => 'DEBUG',
  'DEBUG' => 'DEBUG',
  'INFO' => 'INFO',
  'NOTICE' => 'INFO',
  'WARN' => 'WARN',
  'ERROR' => 'ERROR',
  'FATAL' => 'FATAL',
}.freeze

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.disable_default_stderrObject

Returns the value of attribute disable_default_stderr.



163
164
165
# File 'lib/r10k/logging.rb', line 163

def disable_default_stderr
  @disable_default_stderr
end

.formatterObject (readonly)

Returns the value of attribute formatter.



148
149
150
# File 'lib/r10k/logging.rb', line 148

def formatter
  @formatter
end

.levelObject

Returns the value of attribute level.



143
144
145
# File 'lib/r10k/logging.rb', line 143

def level
  @level
end

.outputterObject (readonly)

Returns the value of attribute outputter.



153
154
155
# File 'lib/r10k/logging.rb', line 153

def outputter
  @outputter
end

.outputtersObject (readonly)

Returns the value of attribute outputters.



158
159
160
# File 'lib/r10k/logging.rb', line 158

def outputters
  @outputters
end

Class Method Details

.add_outputters(outputs) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/r10k/logging.rb', line 90

def add_outputters(outputs)
  outputs.each do |output|
    type = output.fetch(:type)
    # Support specifying both short as well as full names
    type = type.to_s[0..-10] if type.to_s.downcase.end_with? 'outputter'

    name = output.fetch(:name, 'r10k')
    if output[:level]
      level = parse_level(output[:level])
      if level.nil?
        raise ArgumentError, _("Invalid log level '%{val}'. Valid levels are %{log_levels}") % { val: output[:level], log_levels: LOG_LEVELS.map(&:downcase).inspect }
      end
    else
      level = self.level
    end
    only_at = output[:only_at]
    only_at&.map! do |val|
      lv = parse_level(val)
      if lv.nil?
        raise ArgumentError, _("Invalid log level '%{val}'. Valid levels are %{log_levels}") % { val: val, log_levels: LOG_LEVELS.map(&:downcase).inspect }
      end

      lv
    end
    parameters = output.fetch(:parameters, {}).merge({ level: level })

    begin
      # Try to load the outputter file if possible
      require "log4r/outputter/#{type.to_s.downcase}outputter"
    rescue LoadError
      false
    end
    outputtertype = Log4r.constants
                         .select { |klass| klass.to_s.end_with? 'Outputter' }
                         .find { |klass| klass.to_s.downcase == "#{type.to_s.downcase}outputter" }
    raise ArgumentError, "Unable to find a #{output[:type]} outputter." unless outputtertype

    outputter = Log4r.const_get(outputtertype).new(name, parameters)
    outputter.only_at(*only_at) if only_at
    # Handle log4r's syslog mapping correctly
    outputter.map_levels_by_name_to_syslog(SYSLOG_LEVELS_MAP) if outputter.respond_to? :map_levels_by_name_to_syslog

    @outputters << outputter
    Log4r::Logger.global.add outputter
  end
end

.debug_formatterObject



169
170
171
# File 'lib/r10k/logging.rb', line 169

def debug_formatter
  Log4r::PatternFormatter.new(:pattern => '[%d - %l] %m')
end

.default_formatterObject



165
166
167
# File 'lib/r10k/logging.rb', line 165

def default_formatter
  Log4r::PatternFormatter.new(:pattern => '%l\t -> %m')
end

.default_outputterObject



173
174
175
# File 'lib/r10k/logging.rb', line 173

def default_outputter
  R10K::Logging::TerminalOutputter.new('terminal', $stderr, :level => self.level, :formatter => formatter)
end

.parse_level(input) ⇒ Integer, NilClass

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Convert the input to a valid Log4r log level

Parameters:

  • input (String, TrueClass)

    The level to parse. If TrueClass then Log4r::INFO will be returned (indicating a generic raised verbosity), if a string it will be parsed either as a numeric value or a textual log level.

Returns:

  • (Integer, NilClass)

    The numeric log level, or nil if the log level is invalid.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/r10k/logging.rb', line 53

def parse_level(input)
  case input
  when TrueClass
    Log4r::INFO
  when /\A\d+\Z/
    Integer(input)
  when String
    const_name = input.upcase
    if LOG_LEVELS.include?(const_name)
      begin
        Log4r.const_get(const_name.to_sym)
      rescue NameError
      end
    end
  end
end

Instance Method Details

#loggerObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/r10k/logging.rb', line 26

def logger
  if @logger.nil?
    name = logger_name
    if Log4r::Logger[name]
      @logger = Log4r::Logger[name]
    else
      @logger = Log4r::Logger.new(name)
      @logger.add(R10K::Logging.outputter)
      R10K::Logging.outputters.each do |output|
        @logger.add(output)
      end
    end
  end
  @logger
end

#logger_nameObject



22
23
24
# File 'lib/r10k/logging.rb', line 22

def logger_name
  self.class.to_s
end