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 = -> (_, _, _, ) { "#{}#{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 { |s| s.to_s.encode("UTF-8", invalid: :replace, undef: :replace, replace: "?") }.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.
80 81 82 83 84 85 86 87 88 89 |
# File 'lib/dry/logger/dispatcher.rb', line 80 def initialize(id, backends: [], tags: [], context: {}, **) @id = id @backends = backends @options = {**, progname: id} @mutex = Mutex.new @default_tags = .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 @options 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
71 72 73 74 75 76 |
# File 'lib/dry/logger/dispatcher.rb', line 71 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
275 276 277 278 279 280 281 282 283 284 285 286 |
# File 'lib/dry/logger/dispatcher.rb', line 275 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
240 241 242 243 |
# File 'lib/dry/logger/dispatcher.rb', line 240 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
105 106 107 |
# File 'lib/dry/logger/dispatcher.rb', line 105 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.
296 297 298 299 300 |
# File 'lib/dry/logger/dispatcher.rb', line 296 def each_backend(&block) mutex.synchronize do backends.each(&block) end end |
#error(message = nil, **payload, &block) ⇒ true
Log an entry with ERROR severity
132 133 134 |
# File 'lib/dry/logger/dispatcher.rb', line 132 def error( = nil, **payload, &block) log(:error, , **payload, &block) end |
#fatal(message = nil, **payload, &block) ⇒ true
Log an entry with FATAL severity
141 142 143 |
# File 'lib/dry/logger/dispatcher.rb', line 141 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
307 308 309 310 |
# File 'lib/dry/logger/dispatcher.rb', line 307 def forward(meth, ...) each_backend { |backend| backend.public_send(meth, ...) } true end |
#info(message = nil, **payload, &block) ⇒ true
Log an entry with INFO severity
114 115 116 |
# File 'lib/dry/logger/dispatcher.rb', line 114 def info( = nil, **payload, &block) log(:info, , **payload, &block) end |
#inspect ⇒ Object
290 291 292 |
# File 'lib/dry/logger/dispatcher.rb', line 290 def inspect %(#<#{self.class} id=#{id} options=#{} backends=#{backends}>) end |
#level ⇒ Integer
Return severity level
156 157 158 |
# File 'lib/dry/logger/dispatcher.rb', line 156 def level LEVELS[[:level]] end |
#log(severity, message = nil, **payload) { ... } ⇒ true
Pass logging to all configured backends
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 227 228 |
# File 'lib/dry/logger/dispatcher.rb', line 191 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
258 259 260 261 262 263 |
# File 'lib/dry/logger/dispatcher.rb', line 258 def tagged(*) .push() yield ensure .pop end |
#unknown(message = nil, **payload, &block) ⇒ true
Log an entry with UNKNOWN severity
96 97 98 |
# File 'lib/dry/logger/dispatcher.rb', line 96 def unknown( = nil, **payload, &block) log(:unknown, , **payload, &block) end |
#warn(message = nil, **payload, &block) ⇒ true
Log an entry with WARN severity
123 124 125 |
# File 'lib/dry/logger/dispatcher.rb', line 123 def warn( = nil, **payload, &block) log(:warn, , **payload, &block) end |