Class: Dry::Logger::Dispatcher
- Inherits:
-
Object
- Object
- Dry::Logger::Dispatcher
- Defined in:
- lib/dry/logger/dispatcher.rb
Overview
Logger dispatcher routes log entries to configured logging backends
Constant Summary collapse
- CRASH_LOGGER =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
::Logger.new($stdout).tap { |logger| logger.formatter = -> (_, _, _, ) { "#{message}#{NEW_LINE}" } logger.level = FATAL }.freeze
- ON_CRASH =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
-> (progname:, exception:, message:, payload:) { CRASH_LOGGER.fatal(Logger.templates[:crash] % { severity: "FATAL", progname: progname, time: Time.now, log_entry: [, payload].map(&:to_s).reject(&:empty?).join(SEPARATOR), exception: exception.class, message: exception., backtrace: TAB + exception.backtrace.join(NEW_LINE + TAB) }) }
Instance Attribute Summary collapse
- #backends ⇒ Object readonly private
- #clock ⇒ Object readonly private
- #id ⇒ Object readonly private
- #mutex ⇒ Object readonly private
- #on_crash ⇒ Object readonly private
- #options ⇒ Object readonly private
Class Method Summary collapse
-
.setup(id, **options) {|dispatcher| ... } ⇒ Dispatcher
private
Set up a dispatcher.
Instance Method Summary collapse
-
#add_backend(instance = nil, **backend_options) {|backend| ... } ⇒ Dispatcher
Add a new backend to an existing dispatcher.
-
#context ⇒ Object
Shared payload context.
-
#debug(message = nil, **payload, &block) ⇒ true
Log an entry with DEBUG severity.
- #each_backend(&block) ⇒ Object private
-
#error(message = nil, **payload, &block) ⇒ true
Log an entry with ERROR severity.
-
#fatal(message = nil, **payload, &block) ⇒ true
Log an entry with FATAL severity.
-
#forward(meth) ⇒ true
private
Pass logging to all configured backends.
-
#info(message = nil, **payload, &block) ⇒ true
Log an entry with INFO severity.
-
#initialize(id, backends: [], tags: [], context: {}, **options) ⇒ Dispatcher
constructor
private
A new instance of Dispatcher.
- #inspect ⇒ Object
-
#level ⇒ Integer
Return severity level.
-
#log(severity, message = nil, **payload) { ... } ⇒ true
Pass logging to all configured backends.
-
#tagged(*tags) ⇒ Object
Tagged logging withing the provided block.
-
#unknown(message = nil, **payload, &block) ⇒ true
Log an entry with UNKNOWN severity.
-
#warn(message = nil, **payload, &block) ⇒ true
Log an entry with WARN severity.
Constructor Details
#initialize(id, backends: [], tags: [], context: {}, **options) ⇒ Dispatcher
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Dispatcher.
78 79 80 81 82 83 84 85 86 87 |
# File 'lib/dry/logger/dispatcher.rb', line 78 def initialize(id, backends: [], tags: [], context: {}, **) @id = id @backends = backends = {**, progname: id} @mutex = Mutex.new = .freeze @default_context = context.freeze @clock = Clock.new(**([:clock] || EMPTY_HASH)) @on_crash = [:on_crash] || ON_CRASH end |
Instance Attribute Details
#backends ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
24 25 26 |
# File 'lib/dry/logger/dispatcher.rb', line 24 def backends @backends end |
#clock ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
32 33 34 |
# File 'lib/dry/logger/dispatcher.rb', line 32 def clock @clock end |
#id ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
20 21 22 |
# File 'lib/dry/logger/dispatcher.rb', line 20 def id @id end |
#mutex ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
40 41 42 |
# File 'lib/dry/logger/dispatcher.rb', line 40 def mutex @mutex end |
#on_crash ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
36 37 38 |
# File 'lib/dry/logger/dispatcher.rb', line 36 def on_crash @on_crash end |
#options ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
28 29 30 |
# File 'lib/dry/logger/dispatcher.rb', line 28 def end |
Class Method Details
.setup(id, **options) {|dispatcher| ... } ⇒ Dispatcher
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Set up a dispatcher
69 70 71 72 73 74 |
# File 'lib/dry/logger/dispatcher.rb', line 69 def self.setup(id, **) dispatcher = new(id, **DEFAULT_OPTS, **) yield(dispatcher) if block_given? dispatcher.add_backend if dispatcher.backends.empty? dispatcher end |
Instance Method Details
#add_backend(instance = nil, **backend_options) {|backend| ... } ⇒ Dispatcher
Add a new backend to an existing dispatcher
273 274 275 276 277 278 279 280 281 282 283 284 |
# File 'lib/dry/logger/dispatcher.rb', line 273 def add_backend(instance = nil, **) backend = case (instance ||= Dry::Logger.new(**, **)) when Backends::Stream then instance else Backends::Proxy.new(instance, **, **) end yield(backend) if block_given? backends << backend self end |
#context ⇒ Object
Shared payload context
238 239 240 241 |
# File 'lib/dry/logger/dispatcher.rb', line 238 def context @context_key ||= :"context_#{object_id}" ExecutionContext[@context_key] ||= @default_context.dup end |
#debug(message = nil, **payload, &block) ⇒ true
Log an entry with DEBUG severity
103 104 105 |
# File 'lib/dry/logger/dispatcher.rb', line 103 def debug( = nil, **payload, &block) log(:debug, , **payload, &block) end |
#each_backend(&block) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
294 295 296 297 298 |
# File 'lib/dry/logger/dispatcher.rb', line 294 def each_backend(&block) mutex.synchronize do backends.each(&block) end end |
#error(message = nil, **payload, &block) ⇒ true
Log an entry with ERROR severity
130 131 132 |
# File 'lib/dry/logger/dispatcher.rb', line 130 def error( = nil, **payload, &block) log(:error, , **payload, &block) end |
#fatal(message = nil, **payload, &block) ⇒ true
Log an entry with FATAL severity
139 140 141 |
# File 'lib/dry/logger/dispatcher.rb', line 139 def fatal( = nil, **payload, &block) log(:fatal, , **payload, &block) end |
#forward(meth) ⇒ true
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Pass logging to all configured backends
305 306 307 308 |
# File 'lib/dry/logger/dispatcher.rb', line 305 def forward(meth, ...) each_backend { |backend| backend.public_send(meth, ...) } true end |
#info(message = nil, **payload, &block) ⇒ true
Log an entry with INFO severity
112 113 114 |
# File 'lib/dry/logger/dispatcher.rb', line 112 def info( = nil, **payload, &block) log(:info, , **payload, &block) end |
#inspect ⇒ Object
288 289 290 |
# File 'lib/dry/logger/dispatcher.rb', line 288 def inspect %(#<#{self.class} id=#{id} options=#{options} backends=#{backends}>) end |
#level ⇒ Integer
Return severity level
154 155 156 |
# File 'lib/dry/logger/dispatcher.rb', line 154 def level LEVELS[[:level]] end |
#log(severity, message = nil, **payload) { ... } ⇒ true
Pass logging to all configured backends
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/dry/logger/dispatcher.rb', line 189 def log(severity, = nil, **payload, &block) # rubocop:disable Metrics/PerceivedComplexity return true if LEVELS[severity] < level case when Hash then log(severity, **, &block) else if block progname = block_result = block.call case block_result when Hash then payload = block_result else = block_result end end progname ||= id entry = Entry.new( clock: clock, progname: progname, severity: severity, tags: , message: , payload: {**context, **payload} ) each_backend do |backend| backend.__send__(severity, entry) if backend.log?(entry) rescue StandardError => exception on_crash.(progname: id, exception: exception, message: , payload: payload) end end true rescue StandardError => exception on_crash.(progname: id, exception: exception, message: , payload: payload) true end |
#tagged(*tags) ⇒ Object
Tagged logging withing the provided block
256 257 258 259 260 261 |
# File 'lib/dry/logger/dispatcher.rb', line 256 def tagged(*) .push() yield ensure .pop end |
#unknown(message = nil, **payload, &block) ⇒ true
Log an entry with UNKNOWN severity
94 95 96 |
# File 'lib/dry/logger/dispatcher.rb', line 94 def unknown( = nil, **payload, &block) log(:unknown, , **payload, &block) end |
#warn(message = nil, **payload, &block) ⇒ true
Log an entry with WARN severity
121 122 123 |
# File 'lib/dry/logger/dispatcher.rb', line 121 def warn( = nil, **payload, &block) log(:warn, , **payload, &block) end |