Python Interface to UNIX syslog library routines

The syslog module provides a Python interface to the UNIX system logging facility. It allows your Python programs to send messages to the system log, which administrators can monitor for debugging and system monitoring purposes.

To use this module, we need to import it ?

import syslog

syslog.syslog() Method

This method sends a string message to the system logger. You can specify a priority level for the message.

Syntax

syslog.syslog(message)
syslog.syslog(priority, message)

Parameters:

  • message - String message to log
  • priority - Optional priority level (LOG_DEBUG, LOG_INFO, LOG_NOTICE, LOG_WARNING, LOG_ERR, LOG_CRIT, LOG_ALERT, LOG_EMERG)

syslog.openlog() Method

This method sets logging options for subsequent syslog calls. It allows you to specify an identifier that will be prepended to every message.

Syntax

syslog.openlog([ident[, logoption[, facility]]])

Parameters:

  • ident - String identifier prepended to each message
  • logoption - Logging options (LOG_PID, LOG_CONS, LOG_NDELAY, etc.)
  • facility - System facility (LOG_USER, LOG_MAIL, LOG_DAEMON, etc.)

syslog.closelog() Method

This method resets the syslog module to its default state, closing any open log connections.

Syntax

syslog.closelog()

syslog.setlogmask() Method

This method sets the priority mask to determine which messages are logged. It returns the previous mask value.

Syntax

syslog.setlogmask(maskpri)

Example

Here's a complete example demonstrating how to use the syslog module ?

import syslog
import sys

# Open syslog with program name as identifier
syslog.openlog(sys.argv[0])

# Send messages with different priority levels
syslog.syslog(syslog.LOG_NOTICE, "This is a Log Notice")
syslog.syslog(syslog.LOG_WARNING, "This is a warning message")
syslog.syslog(syslog.LOG_ERR, "This is an error message")

# Close syslog connection
syslog.closelog()

Output

The messages will be written to the system log file (typically /var/log/syslog on Ubuntu/Debian systems). You can view them using ?

$ python3 syslog_example.py
$ sudo tail /var/log/syslog
Oct  7 00:27:32 unix_user-VirtualBox syslog_example.py: This is a Log Notice
Oct  7 00:27:32 unix_user-VirtualBox syslog_example.py: This is a warning message
Oct  7 00:27:32 unix_user-VirtualBox syslog_example.py: This is an error message

Common Priority Levels

Priority Description Use Case
LOG_DEBUG Debug messages Development debugging
LOG_INFO Informational messages General information
LOG_WARNING Warning messages Non-critical issues
LOG_ERR Error messages Error conditions
LOG_CRIT Critical messages Critical conditions

Conclusion

The syslog module provides a standardized way to log messages to the system log. Use appropriate priority levels to categorize your messages and help system administrators monitor your applications effectively.

Updated on: 2026-03-25T04:52:24+05:30

392 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements