Send logs to Syslog server in Python using SysLogHandler

logging_syslog

Skill - Send logs to Syslog server in Python using SysLogHandler

Table of Contents

Skills Required

Please make sure to have all the skills mentioned above to understand and execute the code mentioned below. Go through the above skills if necessary for reference or revision


  • In this post we will learn how to send logs to a syslog server from python using python logging module and SysLogHandler
  • A simple syslog server can be setup in windows or Debian based systems like Ubuntu as shown in this blogpost

Python code to send logs to Syslog server

  • SysLogHandler of python logging module can be used to send logs to syslog server
import logging
from logging.handlers import SysLogHandler

remoteHost = '127.0.0.1'
remotePort = 514

logger = logging.getLogger()
logger.setLevel(logging.INFO)
logger.addHandler(SysLogHandler(address=(remoteHost, remotePort)))

logger.info("Hello World!!!")
logger.error("This is error message")

  • As shown in the above example, the SysLogHandler is configured to send logs to syslog server running at host ‘127.0.0.1’ and UDP port 514

Video

You can see the video for this post here


References

Table of Contents

Comments