Package: priorityprefix
This Python module adds a prefix to log lines so that Journald
knows which log level it comes from.Features:Can handle custom levels as well as Python's five standard ones.Ensures that each line of a multiline log message is correctly prefixed.Also applies a prefix to uncaught exceptions.And prefixes any messages from thewarningsmodule.Usage in your ApplicationInstall the latest stable version with$ pip install priorityprefixOr install the cutting-edge version with$ pip install git+https://github.com/pscl4rke/priorityprefix.gitIf you've already got aFormatterobject for logging then you
can wrap it withFormattingWrapper:import priorityprefix
my_formatter = priorityprefix.FormattingWrapper(my_formatter)For lightweight usage there is aninstallfunction which integrates
nicely withbasicConfigfrom the standard libraryimport logging
logging.basicConfig(level=logging.INFO)
import priorityprefix
priorityprefix.install()By defaultinstall()will also overridesys.excepthookso that any uncaught exceptions will get an error priority prefix added to
their traceback before getting dumped tostderr.By defaultinstall()will also overridewarnings.formatwarningso that things like aDeprecationWarningwill get a warning priority prefix
before getting emitted.Usage with JournaldIn short, you shouldn't need to change anything.If setting up a Systemd service then you need to have the following
settings in your service definition:StandardOutput=journal
StandardError=journal
SyslogLevelPrefix=trueHowever these are the defaults,
so you don't normally need to change anything.Similarly,
if you feed output throughsystemd-cat(which fulfils the same role aslogger)
then--level-prefixwill also be enabled by default.The Back-StoryTo get your log messages into Systemd's Journald you have a number of options:Send legacy Syslog datagrams to/dev/logand rely on Journald listening there
and translating them.Use something likelibsystemdto send journal datagrams to/run/systemd/journal/socket.Just print stuff tostdoutorstderr.  If Systemd has connected them up to the journal then each line will become a log record.The last option is often the most popular because it's the easiest,
but the major downside is that you can't specify any custom fields.
You only haveMESSAGEand the ones that Journald adds itself.Except... you can precede lines with a special prefix to specify
the severity of the message.
This will be stripped off and turned into thePRIORITY.PrefixPRIORITY<7>Debug<6>Informational<5>Notice<4>Warning<3>Error<2>Critical<1>Alert<0>EmergencyThese prefices areinspired by Syslog,
although it only encodes priority levels and does not include facility data.The RoadmapIn many respects this project is now complete.
Going forwards the things I want to achieve are:Maintain compatibility with new versions of Python as they get released.
Normally an annual thing.Support automatic enabling or disabling of the prefix based on whether
theJOURNAL_STREAMenvironment variable is present (supplied by Systemd)
or absent (when the user is running things from a terminal).
The complication of this is that the user would need to explicitly
enable the prefix any time they were piping intosystemd-cat.When Tox/Nox supports running version-specific containers switch to using
that instead of my homegrown test runner.Typechecking with Mypy et alIf you are checking the type annotations of your application you may
get an error aboutpriorityprefixbeing skipped because of
missing stubs and markers.
This can be suppressed in Mypy with a special comment:import priorityprefix  # type: ignoreSome type annotations could be added to this library easily enough.
Unfortunately tools like Mypy adhere strictly toPEP 561which requires the module to be fully refactored into a package
before it will be recognised.
Until a single-file module marker is available this library has
no way to communicate to the tooling that type information is available.LicenceThis software library is released under the LGPL v3.0.
It may be used by and distributed with both GPL and non-GPL applications.