On this page
  1. syslog
  2. Import
  3. Overview
  4. Quick Start
  5. Implementation Walkthrough
  6. 1. Filtering Severity with setlogmask()
  7. Gotchas & Boundaries
  8. Common Functions
  9. Constants & Attributes

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

Common Functions

Constants & Attributes

Linked from

Related notes