On this page
syslog
Prev: sysconfig | Up: stdlib_module | Next: tabnanny
Rel: os, io, time, argparse, getopt
Ref: docs.python.org — syslog
Tags: #public #python3 #stdlib
Import
import syslog
Overview
Unix-only interface to the system logging daemon (openlog(), syslog(), closelog(), setlogmask()). Reach for syslog when writing lightweight Unix daemons or system scripts that emit directly to the local syslog socket; for structured, multi-handler application logging, use logging.handlers.SysLogHandler in logging instead.
Quick Start
import sys
if sys.platform != "win32":
import syslog
# Mask and priority constants are standard POSIX bitmasks
mask = syslog.LOG_UPTO(syslog.LOG_INFO)
assert isinstance(mask, int)
# In a daemon:
# syslog.openlog(ident="myservice", logoption=syslog.LOG_PID, facility=syslog.LOG_DAEMON)
# syslog.syslog(syslog.LOG_INFO, "Service started")
# syslog.closelog()
Implementation Walkthrough
1. Filtering Severity with setlogmask()
Use syslog.LOG_UPTO(priority) to suppress debug messages in production without changing call sites:
import syslog
def configure_daemon_syslog(debug: bool = False) -> None:
syslog.openlog(ident="worker", logoption=syslog.LOG_PID, facility=syslog.LOG_DAEMON)
level = syslog.LOG_DEBUG if debug else syslog.LOG_INFO
syslog.setlogmask(syslog.LOG_UPTO(level))
Gotchas & Boundaries
- Unix-only: Not available on Windows.
- Process-global state:
openlog()andsetlogmask()affect the whole process; avoid calling them from reusable libraries. - Local daemon only:
syslogwrites to the local OS syslog API; to ship logs to a remote syslog server over UDP/TCP, uselogging.handlers.SysLogHandler.
Common Functions
- syslog.closelog() —
closelog()— Reset the syslog module values and call the system library closelog(). - syslog.openlog() —
openlog(...)— Set logging options of subsequent syslog() calls. - syslog.setlogmask() —
setlogmask(maskpri, /)— Set the priority mask to maskpri and return the previous mask value. - syslog.syslog() —
syslog([priority=LOG_INFO,] message)— Send the string message to the system logger.
Constants & Attributes
syslog.LOG_ALERT(int) =1syslog.LOG_AUTH(int) =32syslog.LOG_AUTHPRIV(int) =80syslog.LOG_CONS(int) =2syslog.LOG_CRIT(int) =2syslog.LOG_CRON(int) =72syslog.LOG_DAEMON(int) =24syslog.LOG_DEBUG(int) =7syslog.LOG_EMERG(int) =0syslog.LOG_ERR(int) =3syslog.LOG_FTP(int) =88syslog.LOG_INFO(int) =6syslog.LOG_INSTALL(int) =112syslog.LOG_KERN(int) =0syslog.LOG_LAUNCHD(int) =192
Linked from
Related notes
- tuts/python3/stdlib_module/stdlib_module Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/sysconfig Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/tabnanny Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/argparse Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/getopt Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/io Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/logging Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/os Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/syslog/closelog Linked here; shared tags: #stdlib