Class: Berkshelf::Logger

Inherits:
Logger
  • Object
show all
Defined in:
lib/berkshelf/logger.rb

Instance Method Summary collapse

Constructor Details

#initialize(device = STDOUT) ⇒ Logger

Returns a new instance of Logger.



3
4
5
6
7
# File 'lib/berkshelf/logger.rb', line 3

def initialize(device = STDOUT)
  super
  self.level = Logger::WARN
  @filter_params = []
end

Instance Method Details

#add(severity, message = nil, progname = nil, &block) ⇒ Boolean

Reimplements Logger#add adding message filtering. The info, warn, debug, error, and fatal methods all call add.

Examples:

log.filter_param("hello")
log.info("hello world!") => "FILTERED world!"

Parameters:

  • severity (Fixnum)

    an integer measuing the severity - Logger::INFO, etc.

  • message (String) (defaults to: nil)

    nil

    the message to log

  • progname (String) (defaults to: nil)

    nil

    the program name performing the logging

  • &block

    a block that will be evaluated (for complicated logging)

Returns:

  • (Boolean)


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/berkshelf/logger.rb', line 26

def add(severity, message = nil, progname = nil, &block)
  severity ||= Logger::UNKNOWN
  if @logdev.nil? || severity < (@level)
    return true
  end

  progname ||= @progname
  if message.nil?
    if block_given?
      message = yield
    else
      message = progname
      progname = @progname
    end
  end
  @logdev.write(
    format_message(format_severity(severity), Time.now, progname, filter(message))
  )
  true
end

#clear_filter_paramsObject



55
56
57
# File 'lib/berkshelf/logger.rb', line 55

def clear_filter_params
  @filter_params.clear
end

#deprecate(message) ⇒ Object



68
69
70
71
# File 'lib/berkshelf/logger.rb', line 68

def deprecate(message)
  trace = caller.join("\n\t")
  warn "DEPRECATION WARNING: #{message}\n\t#{trace}"
end

#exception(ex) ⇒ Object

Log an exception and its backtrace to FATAL

Parameters:

  • ex (Exception)


76
77
78
79
# File 'lib/berkshelf/logger.rb', line 76

def exception(ex)
  fatal("#{ex.class}: #{ex}")
  fatal(ex.backtrace.join("\n")) unless ex.backtrace.nil?
end

#filter(message) ⇒ Object



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

def filter(message)
  filter_params.each do |param|
    message.gsub!(param.to_s, "FILTERED")
  end
  message
end

#filter_param(param) ⇒ Object



51
52
53
# File 'lib/berkshelf/logger.rb', line 51

def filter_param(param)
  @filter_params << param unless filter_params.include?(param)
end

#filter_paramsObject



47
48
49
# File 'lib/berkshelf/logger.rb', line 47

def filter_params
  @filter_params.dup
end