How to get the current set LOG_MASK in Python's syslog module?

Chris Angelico rosuav at gmail.com
Thu Sep 22 23:32:51 EDT 2022


On Thu, 22 Sept 2022 at 23:46, Richard Moseley
<richardmoseley4 at gmail.com> wrote:
>
> According to documentation syslog.setlogmask returns the current mask so
> save the value to reset later on.
>
> Oldval = syslog.setlogmask(newmask)
>
> This sets oldval to original mask.

This on its own suggests an odd technique that should work but won't be great:

oldval = syslog.setlogmask(1234)
syslog.setlogmask(oldval)

But the Python function just passes the value straight to the
underlying system call, and thus treats zero specially:

"""
The setlogmask() function sets this logmask for the calling
process, and returns the previous mask.  If the mask argument is
0, the current logmask is not modified.
"""

So you should be able to do:

current_mask = syslog.setlogmask(0)

The Python docs do say to refer to the man pages, but IMO it would be
worth mentioning this feature specifically in the docs.

ChrisA


More information about the Python-list mailing list