Class: DLogger::Logger
- Inherits:
-
Object
- Object
- DLogger::Logger
- Defined in:
- lib/dlogger/logger.rb
Constant Summary collapse
- LOG_LEVELS =
[:trace, :debug, :info, :warn, :error].freeze
Instance Attribute Summary collapse
-
#level ⇒ Object
Returns the value of attribute level.
Class Method Summary collapse
Instance Method Summary collapse
-
#add_output(name, handler) ⇒ Object
Register a new output, the only requirement is that the object passed reponds to the “dispatch” method.
-
#add_to_global_context(h) ⇒ Object
This will get merged in every context no matter which fiber they are in.
-
#initialize(name = "_default") ⇒ Logger
constructor
A new instance of Logger.
-
#log(msg, metadata: {}, restrict: []) ⇒ Object
Main entry point, log a message with its metadata.
-
#pop_context ⇒ Object
The exact opposite of #push_context, if you called it by hand you can remove the context for the stack by calling this method.
-
#push_context(context_data) ⇒ Object
Usually called by #with_context method but you can use it directly to push a context on the stack, it may be useful for asynchronous handling or just to push a global context.
-
#with_context(context_data) ⇒ Object
add context data for any log sent within the given block.
Constructor Details
#initialize(name = "_default") ⇒ Logger
Returns a new instance of Logger.
9 10 11 12 13 14 |
# File 'lib/dlogger/logger.rb', line 9 def initialize(name = "_default") @name = name @outputs = {} @global_context = {} @level = 0 end |
Instance Attribute Details
#level ⇒ Object
Returns the value of attribute level.
7 8 9 |
# File 'lib/dlogger/logger.rb', line 7 def level @level end |
Class Method Details
.level_as_sym(level) ⇒ Object
66 67 68 |
# File 'lib/dlogger/logger.rb', line 66 def self.level_as_sym(level) LOG_LEVELS[level] end |
Instance Method Details
#add_output(name, handler) ⇒ Object
Register a new output, the only requirement is that the object passed reponds to the “dispatch” method.
91 92 93 |
# File 'lib/dlogger/logger.rb', line 91 def add_output(name, handler) @outputs[name] = handler end |
#add_to_global_context(h) ⇒ Object
This will get merged in every context no matter which fiber they are in
100 101 102 |
# File 'lib/dlogger/logger.rb', line 100 def add_to_global_context(h) @global_context.merge!(h) end |
#log(msg, metadata: {}, restrict: []) ⇒ Object
Main entry point, log a message with its metadata.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/dlogger/logger.rb', line 33 def log(msg, metadata: {}, restrict: []) # clearing a small hash is slightly faster than creating a new # one each time. .clear .merge!(@global_context) # first load context data context.each do |ctx_data| case ctx_data when Hash .merge!(ctx_data) when Extension ctx_data.class.properties.each do |attr_name| [attr_name] = ctx_data.send(attr_name) end end end # then add our data .merge!() # and dispatch the result .each do |k,v| if v.is_a?(Proc) [k] = v.call() end end dispatch(msg, metadata: , restrict: restrict) end |
#pop_context ⇒ Object
The exact opposite of #push_context, if you called it by hand you can remove the context for the stack by calling this method.
123 124 125 |
# File 'lib/dlogger/logger.rb', line 123 def pop_context context.pop end |
#push_context(context_data) ⇒ Object
Usually called by #with_context method but you can use it directly to push a context on the stack, it may be useful for asynchronous handling or just to push a global context.
112 113 114 115 116 117 118 |
# File 'lib/dlogger/logger.rb', line 112 def push_context(context_data) if context_data.is_a?(Class) context << context_data.new else context << context_data end end |
#with_context(context_data) ⇒ Object
add context data for any log sent within the given block.
133 134 135 136 137 138 139 |
# File 'lib/dlogger/logger.rb', line 133 def with_context(context_data) push_context(context_data) yield ensure # remove context pop_context end |