Class: Steno::Logger
Constant Summary collapse
- LEVELS =
{ off: Steno::LogLevel.new(:off, 0), fatal: Steno::LogLevel.new(:fatal, 1), error: Steno::LogLevel.new(:error, 5), warn: Steno::LogLevel.new(:warn, 10), info: Steno::LogLevel.new(:info, 15), debug: Steno::LogLevel.new(:debug, 16), debug1: Steno::LogLevel.new(:debug1, 17), debug2: Steno::LogLevel.new(:debug2, 18), all: Steno::LogLevel.new(:all, 30) }
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
- .define_level_active_predicate(name) ⇒ Object
-
.define_log_method(name) ⇒ Object
The following helpers are used to create a new scope for binding the log level.
- .define_logf_method(name) ⇒ Object
- .lookup_level(name) ⇒ Object
Instance Method Summary collapse
-
#initialize(name, sinks, opts = {}) ⇒ Logger
constructor
A new instance of Logger.
-
#level ⇒ Symbol
Returns the name of the current log level.
-
#level=(level_name) ⇒ nil
Sets the minimum level for which records will be added to sinks.
-
#level_active?(level_name) ⇒ true || false
Returns whether or not records for the given level would be forwarded to sinks.
-
#log(level_name, message = nil, user_data = nil) ⇒ nil
Adds a record to the configured sinks.
- #log_exception(ex, user_data = {}) ⇒ nil
-
#tag(user_data = {}) ⇒ Steno::TaggedLogger
Returns a proxy that will emit the supplied user data along with each log record.
Constructor Details
#initialize(name, sinks, opts = {}) ⇒ Logger
Returns a new instance of Logger.
69 70 71 72 73 74 75 |
# File 'lib/steno/logger.rb', line 69 def initialize(name, sinks, opts = {}) @name = name @min_level = self.class.lookup_level(opts[:level] || :info) @min_level_lock = Mutex.new @sinks = sinks @context = opts[:context] || Steno::Context::Null.new end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
61 62 63 |
# File 'lib/steno/logger.rb', line 61 def name @name end |
Class Method Details
.define_level_active_predicate(name) ⇒ Object
34 35 36 |
# File 'lib/steno/logger.rb', line 34 def define_level_active_predicate(name) define_method(name.to_s + '?') { level_active?(name) } end |
.define_log_method(name) ⇒ Object
The following helpers are used to create a new scope for binding the log level.
26 27 28 |
# File 'lib/steno/logger.rb', line 26 def define_log_method(name) define_method(name) { |*args, &blk| log(name, *args, &blk) } end |
.define_logf_method(name) ⇒ Object
30 31 32 |
# File 'lib/steno/logger.rb', line 30 def define_logf_method(name) define_method(name.to_s + 'f') { |fmt, *args| log(name, fmt % args) } end |
Instance Method Details
#level ⇒ Symbol
Returns the name of the current log level
91 92 93 |
# File 'lib/steno/logger.rb', line 91 def level @min_level_lock.synchronize { @min_level.name } end |
#level=(level_name) ⇒ nil
Sets the minimum level for which records will be added to sinks.
82 83 84 85 86 |
# File 'lib/steno/logger.rb', line 82 def level=(level_name) level = self.class.lookup_level(level_name) @min_level_lock.synchronize { @min_level = level } end |
#level_active?(level_name) ⇒ true || false
Returns whether or not records for the given level would be forwarded to sinks.
101 102 103 104 |
# File 'lib/steno/logger.rb', line 101 def level_active?(level_name) level = self.class.lookup_level(level_name) @min_level_lock.synchronize { level <= @min_level } end |
#log(level_name, message = nil, user_data = nil) ⇒ nil
Adds a record to the configured sinks.
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/steno/logger.rb', line 122 def log(level_name, = nil, user_data = nil) return unless level_active?(level_name) = yield if block_given? callstack = caller loc = parse_record_loc(callstack) data = @context.data.merge(user_data || {}) record = Steno::Record.new(@name, level_name, , loc, data) @sinks.each { |sink| sink.add_record(record) } nil end |
#log_exception(ex, user_data = {}) ⇒ nil
111 112 113 |
# File 'lib/steno/logger.rb', line 111 def log_exception(ex, user_data = {}) warn("Caught exception: #{ex}", user_data.merge(backtrace: ex.backtrace)) end |
#tag(user_data = {}) ⇒ Steno::TaggedLogger
Returns a proxy that will emit the supplied user data along with each log record.
145 146 147 |
# File 'lib/steno/logger.rb', line 145 def tag(user_data = {}) Steno::TaggedLogger.new(self, user_data) end |