Sha256: cb088bf1563d785a25849b975b37a601ec14e701815df4cf8f72f91e2aba970d

Contents?: true

Size: 1.42 KB

Versions: 13

Compression:

Stored size: 1.42 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)
        @type = Timber::Util::Object.try(attributes[:type], :to_sym) || raise(ArgumentError.new(":type is required"))
        @data = attributes[:data] || raise(ArgumentError.new(":data is required"))
      end

      # Builds a hash representation containing simple objects, suitable for serialization (JSON).
      def as_json(options = {})
        {type => data}
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
timber-2.5.1 lib/timber/contexts/custom.rb
timber-2.5.0 lib/timber/contexts/custom.rb
timber-2.4.0 lib/timber/contexts/custom.rb
timber-2.3.4 lib/timber/contexts/custom.rb
timber-2.3.3 lib/timber/contexts/custom.rb
timber-2.3.2 lib/timber/contexts/custom.rb
timber-2.3.1 lib/timber/contexts/custom.rb
timber-2.3.0 lib/timber/contexts/custom.rb
timber-2.2.3 lib/timber/contexts/custom.rb
timber-2.2.2 lib/timber/contexts/custom.rb
timber-2.2.1 lib/timber/contexts/custom.rb
timber-2.2.0 lib/timber/contexts/custom.rb
timber-2.1.10 lib/timber/contexts/custom.rb