Class: SysLogger::Formatter::RFC5424

Inherits:
Logger::Formatter
  • Object
show all
Defined in:
lib/syslogger/formatter/rfc5424.rb

Constant Summary collapse

Format =
"<%s>1 %s %s %s %s %s %s %s\n"
FACILITIES =
{
  :kern     => 0,
  :user     => 1,
  :mail     => 2,
  :daemon   => 3,
  :auth     => 4,
  :syslog   => 5,
  :lpr      => 6,
  :news     => 7,
  :uucp     => 8,
  :cron     => 9,
  :authpriv => 10,
  :ftp      => 11,
  :ntp      => 12,
  :audit    => 13,
  :alert    => 14,
  :at       => 15,
  :local0   => 16,
  :local1   => 17,
  :local2   => 18,
  :local3   => 19,
  :local4   => 20,
  :local5   => 21,
  :local6   => 22,
  :local7   => 23
}
SEVERITIES =
{
  :emerg  => 0,
  :alert  => 1,
  :crit   => 2,
  :fatal  => 2,
  :err    => 3,
  :error  => 3,
  :warn   => 4,
  :notice => 5,
  :info   => 6,
  :debug  => 7
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(appname = nil, procid = nil, msgid = nil, facility = nil) ⇒ RFC5424

Returns a new instance of RFC5424.



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/syslogger/formatter/rfc5424.rb', line 49

def initialize(appname = nil, procid = nil, msgid = nil, facility = nil)
  super()

  @counter = 0

  @hostname = Socket.gethostname
  @msgid = format_field(msgid, 32)
  @procid = procid
  @appname = appname

  self.facility = facility || :local7
end

Instance Attribute Details

#appnameObject

Returns the value of attribute appname.



5
6
7
# File 'lib/syslogger/formatter/rfc5424.rb', line 5

def appname
  @appname
end

#msgidObject (readonly)

Returns the value of attribute msgid.



4
5
6
# File 'lib/syslogger/formatter/rfc5424.rb', line 4

def msgid
  @msgid
end

#procidObject

Returns the value of attribute procid.



5
6
7
# File 'lib/syslogger/formatter/rfc5424.rb', line 5

def procid
  @procid
end

Instance Method Details

#call(severity, datetime, progname, message) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/syslogger/formatter/rfc5424.rb', line 70

def call(severity, datetime, progname, message)
  severity = SEVERITIES[severity.to_s.downcase.to_sym] || SEVERITIES[:info]
  pri = (facility << 3) | severity

  # Since we're using RFC5424 format, it makes more sense to use the
  # passed in progname as the msgid rather than changing the appname when
  # a block was received to generate the message.
  message_id = progname.nil? ? msgid : format_field(progname, 32)

  structured_data = {
    "meta" => {
      "x-group" => gen_xgroup,
      "x-counter" => @counter
    }
  }

  lines = msg2str(message).split(/\r?\n/).reject(&:empty?).map do |line|
    @counter = (@counter + 1) % 65536
    structured_data["meta"]["x-counter"] = @counter
    sd = format_sdata(structured_data)
    Format % [pri, datetime.strftime("%FT%T.%6N%:z"), @hostname,
              format_field(@appname, 48), format_field(@procid || Process.pid.to_s, 128),
              message_id, sd, line]
  end
  if lines.size == 1
    lines[0]
  else
    lines
  end
end

#facilityObject



62
63
64
# File 'lib/syslogger/formatter/rfc5424.rb', line 62

def facility
  @facility
end

#facility=(f) ⇒ Object



66
67
68
# File 'lib/syslogger/formatter/rfc5424.rb', line 66

def facility=(f)
  @facility = FACILITIES[f.to_s.downcase.to_sym] || @facility
end