Sha256: f3ce0fc0769f5518baae39361645dcc0ea6fc9ef9704171d976fdb21a5c4fcec

Contents?: true

Size: 1.06 KB

Versions: 5

Compression:

Stored size: 1.06 KB

Contents

require "singleton"

module Timber
  # Holds the current context in a thread safe memory storage. This context is
  # appended to every log line. Think of context as join data between your log lines,
  # allowing you to relate them and filter them appropriately.
  class CurrentContext
    include Singleton

    THREAD_NAMESPACE = :_timber_current_context.freeze

    class << self
      # Convenience method for {#with}.
      #
      # @example Adding a context
      #   custom_context = Timber::Contexts::Custom.new(type: :keyspace, data: %{my: "data"})
      #   Timber::CurrentContext.with(custom_context) do
      #     # ... anything logged here will have the context ...
      #   end
      def with(*args, &block)
        instance.with(*args, &block)
      end
    end

    # Adds a context to the current stack.
    def with(data)
      key = data.keyspace
      hash[key] = data
      yield
    ensure
      hash.delete(key)
    end

    def snapshot
      hash.clone
    end

    private
      def hash
        Thread.current[THREAD_NAMESPACE] ||= {}
      end
  end
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
timber-1.0.3 lib/timber/current_context.rb
timberio-1.0.3 lib/timber/current_context.rb
timberio-1.0.2 lib/timber/current_context.rb
timberio-1.0.1 lib/timber/current_context.rb
timberio-1.0.0 lib/timber/current_context.rb