Class: VCAP::Logging::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/vcap/logging/logger.rb

Defined Under Namespace

Classes: LogLevel

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, sink_map) ⇒ Logger

Returns a new instance of Logger.



61
62
63
64
# File 'lib/vcap/logging/logger.rb', line 61

def initialize(name, sink_map)
  @name = name
  @sink_map = sink_map
end

Class Attribute Details

.log_levelsObject (readonly)

Returns the value of attribute log_levels.



13
14
15
# File 'lib/vcap/logging/logger.rb', line 13

def log_levels
  @log_levels
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



58
59
60
# File 'lib/vcap/logging/logger.rb', line 58

def name
  @name
end

#sink_mapObject

Returns the value of attribute sink_map.



59
60
61
# File 'lib/vcap/logging/logger.rb', line 59

def sink_map
  @sink_map
end

Class Method Details

.define_log_levels(levels) ⇒ Object

Defines convenience methods for each log level. For example, if ‘debug’ is the name of a level corresponding ‘debug’ and ‘debugf’ instance methods will be defined for all loggers.

Parameters:

  • levels

    Array Log levels to use



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/vcap/logging/logger.rb', line 19

def define_log_levels(levels)

  @prev_log_methods ||= []
  # Clean up previously defined methods
  for meth_name in @prev_log_methods
    undef_method(meth_name)
  end

  @prev_log_methods = []
  @log_levels       = {}

  # Partially evaluate log/logf for the level specified by each name
  for name, level in levels
    @log_levels[name] = LogLevel.new(name, level)

    define_log_helper(name)
    @prev_log_methods << name

    name_f = "#{name}f".to_sym
    define_logf_helper(name_f, name)
    @prev_log_methods << name_f
  end
end

Instance Method Details

#log(lvl_name, data = nil, opts = {}) ⇒ Object

Logs a message to the configured sinks. You may optionally supply a block to be called; its return value will be used as data for the log record.

Parameters:

  • lvl_name

    Symbol Log level for the associated message

  • data (defaults to: nil)

    Object Optional data to log. How this is converted to a string is determined by the formatters.

  • opts (defaults to: {})

    Hash :tags => Array Tags to associated with this log message

Raises:

  • (ArgumentError)


84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/vcap/logging/logger.rb', line 84

def log(lvl_name, data=nil, opts={})
  level = self.class.log_levels[lvl_name]
  raise ArgumentError, "Unknown level #{lvl_name}" unless level

  return unless level.value <= @log_level.value
  data = yield if block_given?
  tags = opts[:tags] || []
  tags << :exception if data.kind_of?(Exception)

  rec = VCAP::Logging::LogRecord.new(lvl_name, data, self, tags)
  @sink_map.get_sinks(lvl_name).each {|s| s.add_record(rec) }
end

#log_levelObject



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

def log_level
  @log_level.name
end

#log_level=(lvl_name) ⇒ Object

Raises:

  • (ArgumentError)


70
71
72
73
74
75
76
# File 'lib/vcap/logging/logger.rb', line 70

def log_level=(lvl_name)
  level = self.class.log_levels[lvl_name]
  raise ArgumentError, "Unknown level #{lvl_name}" unless level
  @log_level = level

  self
end

#logf(lvl_name, fmt, fmt_args, opts = {}) ⇒ Object

Logs a message to the configured sinks. This is analogous to the printf() family

Parameters:

  • lvl_name

    Symbol Log level for the associated message

  • fmt

    String Format string to use when formatting the message

  • fmt_args

    Array Arguments to format string

  • opts (defaults to: {})

    Hash See log()

Raises:

  • (ArgumentError)


103
104
105
106
107
108
109
110
111
112
# File 'lib/vcap/logging/logger.rb', line 103

def logf(lvl_name, fmt, fmt_args, opts={})
  level = self.class.log_levels[lvl_name]
  raise ArgumentError, "Unknown level #{lvl_name}" unless level

  return unless level.value <= @log_level.value
  data = fmt % fmt_args

  rec = VCAP::Logging::LogRecord.new(lvl_name, data, self, opts[:tags] || [])
  @sink_map.get_sinks(lvl_name).each {|s| s.add_record(rec) }
end