Class: Wavefront::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/wavefront-sdk/core/logger.rb

Overview

Log to a user-supplied Ruby logger, or to standard output.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Logger

Returns a new instance of Logger.

Parameters:

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

    options hash from a child of Wavefront::Base



12
13
14
15
16
# File 'lib/wavefront-sdk/core/logger.rb', line 12

def initialize(opts = {})
  @logger  = opts[:logger] || nil
  @verbose = opts[:verbose] || nil
  @debug   = opts[:debug] || nil
end

Instance Attribute Details

#debugObject (readonly)

Returns the value of attribute debug.



8
9
10
# File 'lib/wavefront-sdk/core/logger.rb', line 8

def debug
  @debug
end

#loggerObject (readonly)

Returns the value of attribute logger.



8
9
10
# File 'lib/wavefront-sdk/core/logger.rb', line 8

def logger
  @logger
end

#verboseObject (readonly)

Returns the value of attribute verbose.



8
9
10
# File 'lib/wavefront-sdk/core/logger.rb', line 8

def verbose
  @verbose
end

Instance Method Details

#format_message(level, msg) ⇒ Object



47
48
49
# File 'lib/wavefront-sdk/core/logger.rb', line 47

def format_message(level, msg)
  format('SDK %<level>s: %<msg>s', level: level.to_s.upcase, msg: msg)
end

#log(msg, level = :info) ⇒ Object

Send a message to a Ruby logger object if the user supplied one, or print to standard out if not.

Parameters:

  • msg (String)

    the string to print

  • level (Symbol) (defaults to: :info)

    the level of the message. :verbose messages equate to a standard INFO log level and :debug to DEBUG.



26
27
28
29
30
31
32
# File 'lib/wavefront-sdk/core/logger.rb', line 26

def log(msg, level = :info)
  if logger
    logger.send(level, msg)
  else
    print_message(level, msg)
  end
end


51
52
53
# File 'lib/wavefront-sdk/core/logger.rb', line 51

def print_debug_message(msg)
  puts msg if debug
end


63
64
65
# File 'lib/wavefront-sdk/core/logger.rb', line 63

def print_error_message(msg)
  warn msg
end


55
56
57
# File 'lib/wavefront-sdk/core/logger.rb', line 55

def print_info_message(msg)
  puts msg if debug || verbose
end


34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/wavefront-sdk/core/logger.rb', line 34

def print_message(level, msg)
  method = format('print_%<level>s_message', level: level).to_sym
  msg = format_message(level, msg)

  if respond_to?(:method)
    send(method, msg)
  else
    print_warn_message(format('undefined message level:%<level>s',
                              level: level))
    print_warn_message(msg)
  end
end


59
60
61
# File 'lib/wavefront-sdk/core/logger.rb', line 59

def print_warn_message(msg)
  warn msg
end