Sha256: 2a2702e10931d1079e4517e9d3caa250163dcbd5f38464b3b1cdf51e79b5ed48

Contents?: true

Size: 1.5 KB

Versions: 5

Compression:

Stored size: 1.5 KB

Contents

require "timber/context"
require "timber/util"

module Timber
  module Contexts
    # Custom contexts allow you to add application specific context not covered elsewhere.
    # Any data added this way will be included in your logs. A good example is worker job
    # IDs. When processing a job you might add the job ID to the context, allowing you to
    # view *all* logs generated while processing that job, not just the logs that contain
    # the ID.
    #
    # Note in the example below all custom contexts must contain a root key. This is to
    # ensure attribute names and types never clash across your contexts. It gives you
    # much cleaner pallete to organize your data on.
    #
    # @example Adding a custom context
    #   logger.with_context(build: {version: "1.0.0"}) do
    #     # anything logged here will have the custom context above
    #     # when this block exits the context will no longer be included
    #   end
    class Custom < Context
      @keyspace = :custom

      attr_reader :type, :data

      def initialize(attributes)
        normalizer = Util::AttributeNormalizer.new(attributes)
        @type = normalizer.fetch!(:type, :symbol)
        @data = normalizer.fetch!(:data, :hash)
      end

      # Builds a hash representation containing simple objects, suitable for serialization (JSON).
      def to_hash
        @to_hash ||= Util::NonNilHashBuilder.build do |h|
          h.add(type, data)
        end
      end

      def as_json(options = {})
        to_hash
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
timber-2.6.2 lib/timber/contexts/custom.rb
timber-2.6.1 lib/timber/contexts/custom.rb
timber-2.6.0 lib/timber/contexts/custom.rb
timber-2.6.0.pre.beta2 lib/timber/contexts/custom.rb
timber-2.6.0.pre.beta1 lib/timber/contexts/custom.rb