fishtest
fishtest copied to clipboard
Logging in server code
When running a python program with systemd
both stdout
and stderr
are fully buffered, so there is a delay (sometimes big) to view the entries in the journal.
- test program:
# test_print.py
import time
import sys
for i in range(10)
print(i, file=sys.stderr)
time.sleep(1)
- systemd unit:
# /ets/systemd/system/test_print.service
[Unit]
Description=test print() to journal
After=network.target
[Service]
Type=simple
ExecStart=/home/usr00/env/bin/python3 /home/usr00/test_print.py
User=usr00
WorkingDirectory=/home/usr00
[Install]
WantedBy=multi-user.target
- commands:
sudo systemctl daemon-reload
sudo systemctl start test_print
# check in another terminal
sudo journalctl -f -u test_print
Here some information:
-
stdout buffering types: https://realpython.com/python-print/#buffering-print-calls https://eklitzke.org/stdout-buffering
-
ways to get unbuffered python prints: https://stackoverflow.com/questions/230751/how-can-i-flush-the-output-of-the-print-function-unbuffer-python-output For development/debug the simple ways are:
- use the switch
python3 -u
during development/debug, egpython3 -u pserve development.ini
- redefine the
print()
with thepartial
function in each server module:print = partial(print, flush=True)
- use the switch
-
Pyramid and python logging system: https://docs.pylonsproject.org/projects/pyramid/en/latest/narr/logging.html https://docs.python.org/3/library/logging.html https://docs.python.org/3/howto/logging.html Pyramid creates the configuration to use the standard
logging
python library, see the fishtest code: https://github.com/glinscott/fishtest/blob/2e0b9838d3f7b615a70c5fc2857341038e7213af/server/production.ini#L35-L65