Sha256: 2bd01c2304c81b5a6f9655d780c7eb315e36affd617370a152f7d905d6c8c6ca

Contents?: true

Size: 1.31 KB

Versions: 12

Compression:

Stored size: 1.31 KB

Contents

# frozen_string_literal: true

require "pakyow/logger/formatters/json"

module Pakyow
  class Logger
    module Formatters
      # Formats log messages as logfmt.
      #
      # @example
      #   severity=INFO timestamp="2016-06-20 10:08:29 -0500" id=678cf582 type=http elapsed=0.01ms method=GET uri=/ ip=127.0.0.1
      #   severity=INFO timestamp="2016-06-20 10:08:29 -0500" id=678cf582 type=http elapsed=1.56ms message="hello 2016-06-20 10:08:29 -0500"
      #   severity=INFO timestamp="2016-06-20 10:08:29 -0500" id=678cf582 type=http elapsed=3.37ms status=200
      #
      # @api private
      class Logfmt < Pakyow::Logger::Formatters::JSON
        private

        UNESCAPED_STRING = /\A[\w\.\-\+\%\,\:\;\/]*\z/i

        def serialize(message)
          first = true
          message.each_pair do |key, value|
            value = case value
            when Array
              value.join(",")
            else
              value.to_s
            end

            unless value.match?(UNESCAPED_STRING)
              value = value.dump
            end

            unless first
              @output.call(" ")
            end

            @output.call(key)
            @output.call("=")
            @output.call(value)
            first = false
          end

          @output.call("\n")
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
pakyow-core-1.0.6 lib/pakyow/logger/formatters/logfmt.rb
pakyow-core-1.0.5 lib/pakyow/logger/formatters/logfmt.rb
pakyow-core-1.0.4 lib/pakyow/logger/formatters/logfmt.rb
pakyow-core-1.0.3 lib/pakyow/logger/formatters/logfmt.rb
pakyow-core-1.0.2 lib/pakyow/logger/formatters/logfmt.rb
pakyow-core-1.0.1 lib/pakyow/logger/formatters/logfmt.rb
pakyow-core-1.0.0 lib/pakyow/logger/formatters/logfmt.rb
pakyow-core-1.0.0.rc5 lib/pakyow/logger/formatters/logfmt.rb
pakyow-core-1.0.0.rc4 lib/pakyow/logger/formatters/logfmt.rb
pakyow-core-1.0.0.rc3 lib/pakyow/logger/formatters/logfmt.rb
pakyow-core-1.0.0.rc2 lib/pakyow/logger/formatters/logfmt.rb
pakyow-core-1.0.0.rc1 lib/pakyow/logger/formatters/logfmt.rb