I have a python script that runs via crontab.
This is the crontab:
0 14 * * * python3 /opt/app/script.py >> /var/log/app/script_log.txt
Previously, I had a print statement at the end of the script that would write to /var/log/app/script_log.txt
but after switching to logging
, nothing gets written there (script successfully writes to data.log
- as set up in logging handlers).
This is my logging configuration:
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler("data.log"),
logging.StreamHandler()
]
)
Is there a way to set up logger to write to both locations?
Add another FileHandler
for the desired file to the handlers
list.
handlers=[
logging.FileHandler("data.log"),
logging.FileHandler("/var/log/app/script_log.txt"),
logging.StreamHandler()
]