This module implements an RFC 5424 IETF Syslog Protocol generator in Python. It should work on Python 2.7 or Python 3.4+.
This work is available under the terms of the ISC License.
Members¶
-
class
syslog_rfc5424_formatter.
RFC5424Formatter
(fmt='%(message)s', datefmt=None, style='%', procid=None, msgid=None, sd_id=None)[source]¶ A derived formatter than allows for isotime specification for full RFC5424 compliancy (with corrected TZ format).
This should be combined with the Syslog Handler to actually emit logs.
For a “proper” ISOTIME format, use “%(isotime)s” in a formatter instance of this class or a class derived from this class. This is for a work-around where strftime has no mechanism to produce timezone in the format of “-08:00” as required by RFC5424.
The ‘%(isotime)s’ replacement will read in the record timestamp and try and reparse it. This really is a problem with RFC5424 and strftime. I am unsure if this will be fixed in the future (in one or the other case)
This formatter has an added benefit of allowing for ‘%(hostname)s’ to be specified which will return a ‘-‘ as specified in RFC5424 if socket.gethostname() returns bad data (exception).
This formatter will automatically insert the RFC5424 header for you; the format string that you pass in the constructor is only applied to the message body (and should typically just be %(message)).
Stuctured Data Example:
[exampleSDID@32473 iut=”3” eventSource=”Application” eventID=”1011”]To use structured data:
- Construct the logger with an sd_id kwarg (or set the sd_id attribute on the logger object)
- Construct your individual records with {‘args’: {‘structured_data’: {‘iut’: ‘3’}}}
-
format
(record)[source]¶ Format the specified record as text.
The record’s attribute dictionary is used as the operand to a string formatting operation which yields the returned string. Before formatting the dictionary, a couple of preparatory steps are carried out. The message attribute of the record is computed using LogRecord.getMessage(). If the formatting string uses the time (as determined by a call to usesTime(), formatTime() is called to format the event time. If there is exception information, it is formatted using formatException() and appended to the message.
-
msgid
¶ Default MSGID to add to syslog message
-
procid
¶ Default PROCID to add to syslog message
-
sd_id
¶ Default SD-ID to add to STRUCTURED-DATA section in syslog message