With pathlib:
There are different solutions. You can create a handler to log to a file, you can log to syslog and systemd.
If you're not on Linux, you have lesser options.
The provided example has one problem. It opens the file, write to it and then the file is closed.
This happens for each message.
from pathlib import Path def log(msg): cron_log = Path('/var/cron/log') with cron_log.open('r+') as log_fd: print(msg, file=log_fd)Without pathlib
def log(msg): with open('/var/cron/log', 'r+') as log_fd: print(msg, file=log_fd)A better solution is to use logging in Python: https://realpython.com/python-logging/#using-handlers
There are different solutions. You can create a handler to log to a file, you can log to syslog and systemd.
If you're not on Linux, you have lesser options.
The provided example has one problem. It opens the file, write to it and then the file is closed.
This happens for each message.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!