Skip to content

Helpers

A few helper functions for dealing with log levels, severities and so on.

get_syslog_pri_part

get_syslog_pri_part(log_level: int, facility: int = USER) -> str

Returns a syslog PRI prefix from the provided log level and facility.

See the relevant section of RFC 3164 for details about syslog standard.

Parameters:

Name Type Description Default
log_level int

A Python log level number. The corresponding severity value will be determined via the log_level_severity function and used to calculate the PRI value.

required
facility int

The syslog facility code. Defaults to syslogformat.facility.USER.

USER

Returns:

Type Description
str

A string of the PRI value enclosed in angle brackets.

Examples:

>>> import logging
>>> get_syslog_pri_part(logging.INFO)
<14>
>>> from syslogformat.facility import KERNEL
>>> get_syslog_pri_part(logging.DEBUG, KERNEL)
<7>

normalize_log_level

normalize_log_level(level: int | str) -> int

Returns an integer that can be interpreted as a log level number.

Parameters:

Name Type Description Default
level int | str

If passed an integer, that value is returned unchanged. If passed a string, the corresponding level number is looked up in the level-name-mapping of the logging module; if the name is not found, an error is raised. Otherwise its level number is returned.

required

Returns:

Type Description
int

Valid log level number.

Raises:

Type Description
InvalidLogLevel

If level is a string that is not present in the keys of the level-name-mapping of the logging module.

Examples:

>>> normalize_log_level(42)
42
>>> normalize_log_level("WARNING")
30