Sha256: 8260e470dd84a1a558919d87d667998867084525cba8c51ebdd43178bb5924e3

Contents?: true

Size: 1.03 KB

Versions: 1

Compression:

Stored size: 1.03 KB

Contents

module Fluent
  module TextFormatter
    class SprintfFormatter < Formatter
      Plugin.register_formatter('sprintf', self)

      include Configurable
      include HandleTagAndTimeMixin

      config_param :sprintf_format, :string

      def initialize
        super
      end

      def configure(conf)
        super
        @time_format = @time_format || '%Y-%m-%d %H:%M:%S'
        r = /\$\{([^}]+)\}/
        @keys = @sprintf_format.scan(r).map{ |key| key.first }
        @sprintf_format = @sprintf_format.gsub(/\$\{([^}]+)\}/, '%s')
        begin
          @sprintf_format % @keys
        rescue ArgumentError => e
          raise Fluent::ConfigError, "formatter_sprintf: @sprintf_format is can't include '%'"
        end
      end

      def format(tag, time, record)
        values = @keys.map{ |key| 
          if key == 'tag'
            tag
          elsif key == 'time'
            Time.at(time).strftime(@time_format)
          else
            record[key] 
          end
        }
        @sprintf_format % values
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fluent-plugin-formatter_sprintf-0.0.1 lib/fluent/plugin/formatter_sprintf.rb