Sha256: 16e7dfc42c70633adb0d52e2187ab81fbe53e276b676bcd99adff5a54f69ee65

Contents?: true

Size: 1.01 KB

Versions: 5

Compression:

Stored size: 1.01 KB

Contents

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

module Timber
  module Contexts
    # The system context tracks OS level process information, such as the process ID.
    #
    # @note This is tracked automatically in {CurrentContext}. When the current context
    #   is initialized, the system context gets added automatically.
    class System < Context
      HOSTNAME_MAX_BYTES = 256.freeze

      @keyspace = :system

      attr_reader :hostname, :pid

      def initialize(attributes)
        normalizer = Util::AttributeNormalizer.new(attributes)
        @hostname = normalizer.fetch(:hostname, :string, :limit => HOSTNAME_MAX_BYTES)
        @pid = normalizer.fetch(:pid, :integer)
      end

      # Builds a hash representation containing simple objects, suitable for serialization (JSON).
      def to_hash
        @to_hash ||= Util::NonNilHashBuilder.build do |h|
          h.add(:hostname, hostname)
          h.add(:pid, pid)
        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/system.rb
timber-2.6.1 lib/timber/contexts/system.rb
timber-2.6.0 lib/timber/contexts/system.rb
timber-2.6.0.pre.beta2 lib/timber/contexts/system.rb
timber-2.6.0.pre.beta1 lib/timber/contexts/system.rb