Sha256: 9e16e4dff031c72f2ad0afd607c7d6374a1d276fd0e359a0d7b0aee3841e76d7

Contents?: true

Size: 899 Bytes

Versions: 2

Compression:

Stored size: 899 Bytes

Contents

require 'logger'
require 'time'

module Pallets
  class Logger < ::Logger
    # Overwrite severity methods to add metadata capabilities
    %i[debug info warn error fatal unknown].each do |severity|
      define_method severity do |message|
        metadata = Thread.current[:pallets_log_metadata]
        return super(message) if metadata.nil?

        formatted_metadata = ' ' + metadata.map { |k, v| "#{k}=#{v}" }.join(' ')
        super(formatted_metadata) { message }
      end
    end

    def with_metadata(hash)
      Thread.current[:pallets_log_metadata] = hash
      yield
    ensure
      Thread.current[:pallets_log_metadata] = nil
    end

    module Formatters
      class Pretty < ::Logger::Formatter
        def call(severity, time, metadata, message)
          "#{time.utc.iso8601(4)} pid=#{Process.pid}#{metadata} #{severity}: #{message}\n"
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pallets-0.10.0 lib/pallets/logger.rb
pallets-0.9.0 lib/pallets/logger.rb