-1

I am writing a small plugin for Postfix using python and want to it to emit logging messages. I am not particularly familiar with python and was advised to use loguru. This was certainly easy when I was just sending stuff to stderr, but for production I need a better solution. While loguru does lots of clever things, integrating with the native logging system does not appear to be something it capable of out of the box, nor that anybody seems concerned about implementing.

My first thought was to simply write the output to /dev/log, however on the debian 12 lxc containers I am using for testing, that's not connected to anything. Or more specifically /dev/log is a symlink to /run/systemd/journal/dev-log and that's not connected, despite being opened by pid 1.

rsyslogd is running on the host, but I can't see any sockets opened by this.

If I run logger at the command line, stuff magically appears in /var/log/messages (rsyslog has an open file handle on this).

How do I get my log messages to rsyslogd? In a portable way? (i.e. that will work on LXC, full Linux host, docker etc)

4
  • In general, writing to stderr is the right thing to do. In a modern system running systemd/journald, this output will be directed into the system log. If you run your application in a container, the container runtime will capture and log output to stderr/stdout. There is very little reason to do anything more complicated than that. Systems that use rsyslogd will typically have rsyslogd configured to receive logs from journald. Commented Mar 20, 2024 at 23:14
  • This is not running from systemd (in theory it could but at a massive performance cost). Are you saying that Python can't talk to rsyslog? Commented Mar 21, 2024 at 0:15
  • No. I'm saying that logging to stderr is generally the most flexible way to handle logging. It brings you maximum compatibility with a variety of environments. Python's standard logging module has a SyslogHandler that can write to a unix socket or a network socket (which mechanism to choose depends on your rsyslog configuration). Commented Mar 21, 2024 at 0:21
  • @larsks journald is a pox of a pathetically unreliable CPU hog. It takes at least 10-15 times more CPU resources than straight-to-[r]syslog. journald will drop log messages whenever it decides to and there's no way to prevent that. I've seen journald suck up 3-4 entire CPUs and drop log messages willy-nilly on systems that log a lot of data. Going straight to rsyslog took about 10% of a single CPU with zero dropped messages under the same load. Commented Mar 22, 2024 at 4:49

1 Answer 1

0

Turned out to be as simple as....

import syslog

syslog.syslog(syslog.LOG_INFO, "Hello world")

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.