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(&: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
-
#context ⇒ Object
readonly
(EXPERIMENTAL) Shared payload context.
- #id ⇒ Object readonly private
- #mutex ⇒ Object readonly private
- #on_crash ⇒ Object readonly private
- #options ⇒ Object readonly private
Class Method Summary collapse
- .default_context ⇒ Object private
-
.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.
-
#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: self.class.default_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
(EXPERIMENTAL) 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: self.class.default_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.
95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/dry/logger/dispatcher.rb', line 95 def initialize( id, backends: [], tags: [], context: self.class.default_context, ** ) @id = id @backends = backends @options = {**, progname: id} @mutex = Mutex.new @context = context @tags = @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.
35 36 37 |
# File 'lib/dry/logger/dispatcher.rb', line 35 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.
43 44 45 |
# File 'lib/dry/logger/dispatcher.rb', line 43 def clock @clock end |
#context ⇒ Object (readonly)
(EXPERIMENTAL) Shared payload context
31 32 33 |
# File 'lib/dry/logger/dispatcher.rb', line 31 def context @context 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.
19 20 21 |
# File 'lib/dry/logger/dispatcher.rb', line 19 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.
51 52 53 |
# File 'lib/dry/logger/dispatcher.rb', line 51 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.
47 48 49 |
# File 'lib/dry/logger/dispatcher.rb', line 47 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.
39 40 41 |
# File 'lib/dry/logger/dispatcher.rb', line 39 def @options end |
Class Method Details
.default_context ⇒ 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.
89 90 91 |
# File 'lib/dry/logger/dispatcher.rb', line 89 def self.default_context Thread.current[:__dry_logger__] ||= {} end |
.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
80 81 82 83 84 85 |
# File 'lib/dry/logger/dispatcher.rb', line 80 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
270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/dry/logger/dispatcher.rb', line 270 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 |
#debug(message = nil, **payload, &block) ⇒ true
Log an entry with DEBUG severity
122 123 124 |
# File 'lib/dry/logger/dispatcher.rb', line 122 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.
291 292 293 294 295 |
# File 'lib/dry/logger/dispatcher.rb', line 291 def each_backend(&block) mutex.synchronize do backends.each(&block) end end |
#error(message = nil, **payload, &block) ⇒ true
Log an entry with ERROR severity
149 150 151 |
# File 'lib/dry/logger/dispatcher.rb', line 149 def error( = nil, **payload, &block) log(:error, , **payload, &block) end |
#fatal(message = nil, **payload, &block) ⇒ true
Log an entry with FATAL severity
158 159 160 |
# File 'lib/dry/logger/dispatcher.rb', line 158 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
302 303 304 305 |
# File 'lib/dry/logger/dispatcher.rb', line 302 def forward(meth, ...) each_backend { |backend| backend.public_send(meth, ...) } true end |
#info(message = nil, **payload, &block) ⇒ true
Log an entry with INFO severity
131 132 133 |
# File 'lib/dry/logger/dispatcher.rb', line 131 def info( = nil, **payload, &block) log(:info, , **payload, &block) end |
#inspect ⇒ Object
285 286 287 |
# File 'lib/dry/logger/dispatcher.rb', line 285 def inspect %(#<#{self.class} id=#{id} options=#{} backends=#{backends}>) end |
#level ⇒ Integer
Return severity level
173 174 175 |
# File 'lib/dry/logger/dispatcher.rb', line 173 def level LEVELS[[:level]] end |
#log(severity, message = nil, **payload) { ... } ⇒ true
Pass logging to all configured backends
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/dry/logger/dispatcher.rb', line 208 def log(severity, = nil, **payload, &block) case when Hash then log(severity, **, &block) else if block progname = = block.call end progname ||= id entry = Entry.new( clock: clock, progname: progname, severity: severity, tags: @tags, message: , payload: {**context, **payload} ) each_backend do |backend| backend.__send__(severity, entry) if backend.log?(entry) rescue StandardError => e on_crash.(progname: id, exception: e, message: , payload: payload) end end true rescue StandardError => e on_crash.(progname: id, exception: e, message: , payload: payload) true end |
#tagged(*tags) ⇒ Object
(EXPERIMENTAL) Tagged logging withing the provided block
253 254 255 256 257 258 |
# File 'lib/dry/logger/dispatcher.rb', line 253 def tagged(*) @tags.concat() yield ensure @tags = [] end |
#unknown(message = nil, **payload, &block) ⇒ true
Log an entry with UNKNOWN severity
113 114 115 |
# File 'lib/dry/logger/dispatcher.rb', line 113 def unknown( = nil, **payload, &block) log(:unknown, , **payload, &block) end |
#warn(message = nil, **payload, &block) ⇒ true
Log an entry with WARN severity
140 141 142 |
# File 'lib/dry/logger/dispatcher.rb', line 140 def warn( = nil, **payload, &block) log(:warn, , **payload, &block) end |