Class: VCAP::Logging::Logger
- Inherits:
-
Object
- Object
- VCAP::Logging::Logger
- Defined in:
- lib/vcap/logging/logger.rb
Defined Under Namespace
Classes: LogLevel
Class Attribute Summary collapse
-
.log_levels ⇒ Object
readonly
Returns the value of attribute log_levels.
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#sink_map ⇒ Object
Returns the value of attribute sink_map.
Class Method Summary collapse
-
.define_log_levels(levels) ⇒ Object
Defines convenience methods for each log level.
Instance Method Summary collapse
-
#initialize(name, sink_map) ⇒ Logger
constructor
A new instance of Logger.
-
#log(lvl_name, data = nil, opts = {}) ⇒ Object
Logs a message to the configured sinks.
- #log_level ⇒ Object
- #log_level=(lvl_name) ⇒ Object
-
#logf(lvl_name, fmt, fmt_args, opts = {}) ⇒ Object
Logs a message to the configured sinks.
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_levels ⇒ Object (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
#name ⇒ Object (readonly)
Returns the value of attribute name.
58 59 60 |
# File 'lib/vcap/logging/logger.rb', line 58 def name @name end |
#sink_map ⇒ Object
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.
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.
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? = opts[:tags] || [] << :exception if data.kind_of?(Exception) rec = VCAP::Logging::LogRecord.new(lvl_name, data, self, ) @sink_map.get_sinks(lvl_name).each {|s| s.add_record(rec) } end |
#log_level ⇒ Object
66 67 68 |
# File 'lib/vcap/logging/logger.rb', line 66 def log_level @log_level.name end |
#log_level=(lvl_name) ⇒ Object
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
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 |