Sha256: 3753e9862e4a26b4c5a806f6230e1f361964de81f6832e82db01aca6d7ced879

Contents?: true

Size: 826 Bytes

Versions: 1

Compression:

Stored size: 826 Bytes

Contents

# To retrieve thread-global metadata that is used to enrich data that is sent
# to logging and debugging tools. In addition to holding the data
# thread-global, this will also attempt to update current of error reporting
# tools etc.

module Betterlog
  class GlobalMetadata
    include Tins::SexySingleton

    thread_local(:current) { {} }

    def add(data)
      data = data.symbolize_keys_recursive
      self.current = data | current
      self
    end

    def remove(data)
      keys = data.ask_and_send_or_self(:keys).map(&:to_sym)
      keys.each { current.delete(_1) }
    end

    def with_meta(data = {}, &block)
      add data
      block.call(current.dup.freeze)
    ensure
      remove data
    end
  end

  def self.with_meta(data = {}, &block)
    Betterlog::GlobalMetadata.with_meta(data, &block)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
betterlog-2.0.1 lib/betterlog/global_metadata.rb