Sha256: eb8710e6ca59b40fde56d5e60d24f5fdac918dfbd08def73baa7f003b94f0627

Contents?: true

Size: 1.65 KB

Versions: 3

Compression:

Stored size: 1.65 KB

Contents

# Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
# frozen_string_literal: true

cs__scoped_require 'ougai'
cs__scoped_require 'contrast/utils/thread_tracker'

module Contrast
  module Logger
    # Our format for the Ougai logger allowing for custom log format that
    # extends the behavior of the default Ougai logger
    class Format < Ougai::Formatters::Bunyan
      LOG_TRACKER = Contrast::Utils::ThreadTracker.new
      # Our override of the _call method to add in the extra data that we want,
      # based on
      # https://github.com/tilfin/ougai/blob/1fe4fc2587be8eabc47d36dc57ee2814f8a0162b/lib/ougai/formatters/bunyan.rb#L26
      # By default, it adds:
      #   name: progname || @app_name,
      #   hostname: @hostname,
      #   pid: $$,
      #   level: to_level(severity),
      #   time: time,
      #   v: 0
      # and we add the fields from #format_hash
      def _call severity, time, progname, data
        super(severity,
              time,
              progname,
              format_hash.merge(data)
        )
      end

      private

      # The extra values to add to the Bunyan hash. Because this is unique per
      # thread, we'll create it once and then store it, rather than create a
      # new hash on every log call.
      # @return [Hash{Symbol => Object}] the extra data to log
      #   tid: current thread id
      def format_hash
        hash = LOG_TRACKER.get(:thread_id_hash)
        unless hash
          hash = {
              tid: Thread.current.object_id
          }
          LOG_TRACKER.set(:thread_id_hash, hash)
        end
        hash
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
contrast-agent-3.13.2 lib/contrast/logger/format.rb
contrast-agent-3.13.1 lib/contrast/logger/format.rb
contrast-agent-3.13.0 lib/contrast/logger/format.rb