Sha256: f9a693c0fe69d1f3b616bcc5172f946ed415a1a2ba8e2fd6733789d45b40d023

Contents?: true

Size: 1.02 KB

Versions: 39

Compression:

Stored size: 1.02 KB

Contents

require 'date'

module RFC5424
  class Formatter
    Format = "<%d>1 %s %s %s %s %s %s %s"

    class << self
      def format(
        priority: 14,
        timestamp: nil,
        log: "",
        hostname: "-",
        app_name: "-",
        proc_id: "-",
        msg_id: "-",
        sd: "-"
      )
        Format % [priority, format_time(timestamp), hostname[0..254], app_name[0..47], proc_id[0..127], msg_id[0..31], sd, log]
      end

      def format_time(timestamp)
        return "-" if timestamp.nil?
        return Time.at(timestamp.to_r).utc.to_datetime.rfc3339(6) if timestamp.is_a?(Fluent::EventTime)

        DateTime.strptime(timestamp.to_s, '%s').rfc3339(6)
      end
    end
  end

  class StructuredData
    attr_reader :sd_id, :sd_elements

    def initialize(sd_id:, sd_elements: {})
      @sd_id = sd_id
      @sd_elements = sd_elements
    end

    def to_s
      el = @sd_elements.inject("") do |elements, tuple|
        elements + %{ #{tuple.first}="#{tuple.last}"}
      end
      %{[#{sd_id}#{el}]}
    end
  end
end

Version data entries

39 entries across 39 versions & 1 rubygems

Version Path
fluent-plugin-syslog_rfc5424-0.9.0.rc.8 lib/rfc5424/formatter.rb
fluent-plugin-syslog_rfc5424-0.9.0.rc.7 lib/rfc5424/formatter.rb
fluent-plugin-syslog_rfc5424-0.9.0.rc.5 lib/rfc5424/formatter.rb
fluent-plugin-syslog_rfc5424-0.9.0.rc.4 lib/rfc5424/formatter.rb
fluent-plugin-syslog_rfc5424-0.9.0.rc.2 lib/rfc5424/formatter.rb
fluent-plugin-syslog_rfc5424-0.9.0.rc.1 lib/rfc5424/formatter.rb
fluent-plugin-syslog_rfc5424-0.8.0 lib/rfc5424/formatter.rb
fluent-plugin-syslog_rfc5424-0.7.3.pre.rc.14 lib/rfc5424/formatter.rb
fluent-plugin-syslog_rfc5424-0.7.3.pre.rc.13 lib/rfc5424/formatter.rb
fluent-plugin-syslog_rfc5424-0.7.3.pre.rc.12 lib/rfc5424/formatter.rb
fluent-plugin-syslog_rfc5424-0.7.3.pre.rc.11 lib/rfc5424/formatter.rb
fluent-plugin-syslog_rfc5424-0.7.3 lib/rfc5424/formatter.rb
fluent-plugin-syslog_rfc5424-0.7.2.alpha11 lib/rfc5424/formatter.rb
fluent-plugin-syslog_rfc5424-0.7.2.alpha10 lib/rfc5424/formatter.rb
fluent-plugin-syslog_rfc5424-0.7.2.alpha9 lib/rfc5424/formatter.rb
fluent-plugin-syslog_rfc5424-0.7.2.alpha8 lib/rfc5424/formatter.rb
fluent-plugin-syslog_rfc5424-0.7.2.alpha7 lib/rfc5424/formatter.rb
fluent-plugin-syslog_rfc5424-0.7.2.alpha6 lib/rfc5424/formatter.rb
fluent-plugin-syslog_rfc5424-0.7.2.alpha5 lib/rfc5424/formatter.rb
fluent-plugin-syslog_rfc5424-0.7.2.alpha4 lib/rfc5424/formatter.rb