Skip to main content

A more up-to-date, RFC5424-Compliant syslog handler for the Python logging framework

Project description

https://img.shields.io/pypi/v/rfc5424-logging-handler.svg https://img.shields.io/pypi/pyversions/rfc5424-logging-handler.svg https://travis-ci.org/jobec/rfc5424-logging-handler.svg?branch=master https://codecov.io/github/jobec/rfc5424-logging-handler/coverage.svg?branch=master

A more up-to-date, RFC 5424 compliant syslog handler for the Python logging framework

Features

  • RFC 5424 Compliant

  • No need for complicated formatting strings

Installation

Python package:

pip install rfc5424-logging-handler

Usage

Basic

After installing you can use this package like this:

import logging
from rfc5424logging import Rfc5424SysLogHandler

logger = logging.getLogger('syslogtest')
logger.setLevel(logging.INFO)

sh = Rfc5424SysLogHandler(address=('10.0.0.1', 514))
logger.addHandler(sh)

msg_type = 'interesting'
logger.info('This is an %s message', msg_type)

This will send the following message to the syslog server:

<14>1 2020-01-01T05:10:20.841485+01:00 myserver syslogtest 5252 - - This is an interesting message

Extended

Full blown example:

import logging
from rfc5424logging import Rfc5424SysLogHandler

logger = logging.getLogger('syslogtest')
logger.setLevel(logging.INFO)

sh = Rfc5424SysLogHandler(
    address=('10.0.0.1', 514),
    hostname="overridden_server_name",
    appname="my_wonderfull_app",
    procid=555,
    structured_data={'sd_id_1': {'key1': 'value1'}},
    enterprise_id=32473
)
logger.addHandler(sh)

msg_type = 'interesting'
extra = {
    'msgid': 'some_unique_msgid',
    'structured_data': {
        'sd_id2': {'key2': 'value2', 'key3': 'value3'}
    }
}
logger.info('This is an %s message', msg_type, extra=extra)

That will send the following message to the syslog server:

<14>1 2020-01-01T05:10:20.841485+01:00 overridden_server_name my_wonderfull_app 555 some_unique_msgid [sd_id_1@32473 key1="value1"][sd_id2@32473 key3="value3" key2="value2"] This is an interesting message

With logger adapter

There’s also an LoggerAdapter subclass available that makes it more easy to send structured data or a message ID with every message

import logging
from rfc5424logging import Rfc5424SysLogHandler, Rfc5424SysLogAdapter

logger = logging.getLogger('syslogtest')
logger.setLevel(logging.INFO)

sh = Rfc5424SysLogHandler(address=('10.0.0.1', 514))
logger.addHandler(sh)
adapter = Rfc5424SysLogAdapter(logger)

msg_type = 'interesting'
adapter.info('This is an %s message',
             msg_type, structured_data={'sd_id2': {'key2': 'value2', 'key3': 'value3'}})
adapter.info('This is an %s message', msg_type, msgid='some_unique_msgid')
adapter.info('This is an %s message',
             msg_type,
             structured_data={'sd_id2': {'key2': 'value2', 'key3': 'value3'}}, msgid='some_unique_msgid')

From a logging config dictionary

Python supports configuring the logging system from a dictionary. Below is an example using the rfc5424 log handler to log to syslog and the stream handler to log to console.

import logging
import logging.config

log_settings = {
    'version': 1,
        'formatters': {
        'console': {
            'format': '[%(asctime)s] [%(levelname)s] [%(name)s] %(message)s',
        },
    },
    'handlers': {
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'console'
        },
        'syslog': {
            'level': 'INFO',
            'class': 'rfc5424logging.handler.Rfc5424SysLogHandler',
            'address': ('127.0.0.1', 514),
            'hostname': 'overridden_server_name',
            'enterprise_id': 32473,
            'appname': 'my_wonderfull_app',
        },
    },
    'loggers': {
        'syslogtest': {
            'handlers': ['console', 'syslog'],
            'level': 'DEBUG',
         },
    }
}
logging.config.dictConfig(log_settings)

logger = logging.getLogger('syslogtest')
logger.info('This message appears on console and is sent to syslog')
logger.debug('This debug message appears on console only')

Changelog

0.0.3 (2017-01-22)

  • Adapter class to make it easier to log message IDs or structured data

  • Logging of EMERGENCY, ALERT and NOTICE syslog levels by using the adapter class

  • Extensive test suite

0.0.2 (2017-01-18)

  • Introduced Python 2.7 compatibility

0.0.1 (2017-01-11)

  • Initial release

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rfc5424-logging-handler-0.1.0.tar.gz (7.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rfc5424_logging_handler-0.1.0-py3-none-any.whl (10.2 kB view details)

Uploaded Python 3

File details

Details for the file rfc5424-logging-handler-0.1.0.tar.gz.

File metadata

File hashes

Hashes for rfc5424-logging-handler-0.1.0.tar.gz
Algorithm Hash digest
SHA256 2e1325864f17364c28d44c777dcbbe08927bfb483159f6136016ea47ba8c17fd
MD5 1e620ed654081e7032264904c8f0dd37
BLAKE2b-256 e5b0ef1c9942caafbe275c59cbacf7b986d04fb11f7bd877c2494919df3fe68c

See more details on using hashes here.

File details

Details for the file rfc5424_logging_handler-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for rfc5424_logging_handler-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9b4242e840ace19b6712eddfd577f908eea75b5ea13426343d79a8e747e3c384
MD5 e9a01c230b5b9ffafa56f5ff915062d1
BLAKE2b-256 780d0ab9b426cf16575d330b43d440ffced3ae4a53ea86b807cda59f34150a18

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page