Sha256: d3c74730a6cb434f3e1ef80c48aee2d57a7529e43bb813270139afae34de3678

Contents?: true

Size: 1.96 KB

Versions: 18

Compression:

Stored size: 1.96 KB

Contents

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

module Contrast
  module Logger
    # Our decorator for the Ougai logger allowing for timing and with_level
    # methods.
    module Time
      # Log the message at the given level.
      #
      # @param level [String] the name of the method to use. Should be one of trace, debug, info, warn, error
      # @param message [String] the message to log
      def with_level level, message
        send(level.to_sym, message)
      end

      # Log, at the debug level, the action with a message including the time it took for the wrapped function to
      # complete. If not logging to debug, simply yield the given block.
      #
      # @param msgs [Array<Object>] the arguments to pass to the logger. msgs[0] will be modified to include the elapsed
      #   time.
      # @param block [Block, Proc] the block to execute
      def debug_with_time *msgs, &block
        if debug?
          log_with_time(:debug, *msgs, &block)
        elsif block
          yield
        end
      end

      # Log, at the trace level, the action with a message including the time it took for the wrapped function to
      # complete. If not logging to debug, simply yield the given block.
      #
      # @param msgs [Array<Object>] the arguments to pass to the logger. msgs[0] will be modified to include the elapsed
      #   time.
      # @param block [Block, Proc] the block to execute
      def trace_with_time *msgs, &block
        if trace?
          log_with_time(:trace, *msgs, &block)
        elsif block
          yield
        end
      end

      private

      def log_with_time level, *msgs
        a = Contrast::Utils::Timer.now_ms
        ret = yield if block_given?
        z = Contrast::Utils::Timer.now_ms
        msgs[0] = "#{ msgs[0] }: pid=#{ Process.pid }, elapsed=#{ z - a }ms"
        send(level, *msgs)
        ret
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
contrast-agent-7.6.1 lib/contrast/logger/time.rb
contrast-agent-7.6.0 lib/contrast/logger/time.rb
contrast-agent-7.5.0 lib/contrast/logger/time.rb
contrast-agent-7.4.1 lib/contrast/logger/time.rb
contrast-agent-7.4.0 lib/contrast/logger/time.rb
contrast-agent-7.3.2 lib/contrast/logger/time.rb
contrast-agent-7.3.1 lib/contrast/logger/time.rb
contrast-agent-7.3.0 lib/contrast/logger/time.rb
contrast-agent-7.2.0 lib/contrast/logger/time.rb
contrast-agent-7.1.0 lib/contrast/logger/time.rb
contrast-agent-7.0.0 lib/contrast/logger/time.rb
contrast-agent-6.15.3 lib/contrast/logger/time.rb
contrast-agent-6.15.2 lib/contrast/logger/time.rb
contrast-agent-6.15.1 lib/contrast/logger/time.rb
contrast-agent-6.15.0 lib/contrast/logger/time.rb
contrast-agent-6.14.0 lib/contrast/logger/time.rb
contrast-agent-6.13.0 lib/contrast/logger/time.rb
contrast-agent-6.12.0 lib/contrast/logger/time.rb