Skip to content

Instantly share code, notes, and snippets.

@f-steff
Created April 19, 2021 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save f-steff/8d50b2f5166b20e29b0c56183b83c4ee to your computer and use it in GitHub Desktop.
Save f-steff/8d50b2f5166b20e29b0c56183b83c4ee to your computer and use it in GitHub Desktop.
Python logger to remote RFC 5424 syslog server - with structured data
#!/usr/bin/python
# -*- coding: utf-8 -*-
# https://github.com/jobec/rfc5424-logging-handler
# Documentation: https://rfc5424-logging-handler.readthedocs.io/en/latest/
import sys
import logging
from rfc5424logging import Rfc5424SysLogHandler, Rfc5424SysLogAdapter
logger = logging.getLogger('RFC5424_Syslog_test_ApplicationName')
logger.setLevel(logging.DEBUG)
# Data specified in the handler init will override default values and become new defaults.
sh = Rfc5424SysLogHandler(address=('10.0.2.2', 514), # Remote computer with syslog.
#hostname="Server_Name_Overwritten_Static",
# appname="ApplicationName_Overwritten_Static",
# procid="ProcessID_Overwritten_Static",
# structured_data={'sd_id_1': {'key1': 'value1'}}, # Static structured data. Can be populated dynamically, too.
enterprise_id=32473, # Some ID is required for structured data.
utc_timestamp=False) # True = Use UTC time.
logger.addHandler(sh)
adapter = Rfc5424SysLogAdapter(logger)
adapter.warning('This message have dynamic structured date',
structured_data={'sd_id2': {'key2': 'value2', 'key3': 'value3'}})
adapter.debug('This message have a dynamic msgid',
msgid='NessageID_overwritten_Dynamic')
adapter.info('This message have dynamic msgid and structured data',
structured_data={'sd_id2': {'key2': 'value2', 'key3': 'value3'}},
msgid='NessageID_overwritten_Dynamic')
adapter.error('Some very different message with lots of dynamic contents',
msgid='NessageID_overwritten_Dynamic',
appname="ApplicationName_Overwritten_Dynamic",
hostname="Server_Name_Overwritten_Dynamic",
procid="ProcessID_Overwritten_Dynamic")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment