Didn't know about the logger
module until I was reading this discussion initiated by @phuket2. Thanks @ccc for mentioning about the logging module.
I've made logging_setup.py
just for ease of use. I'm just sharing it if anyone wants to.
This has a few defaults:
- prints to a file (.txt b/c Pythonista doesn't open .log by default)
- Note: if used in different scripts, the 'name' arg is important for logging to different files.
- mode is set to overwrite (you can set it with keyword arg)
- format looks like this: "DEBUG: this is a message." (can change with
fmt
keyword)
A few benefits:
- can have the log file in a tab in the editor
- can use the 'Find' in editor to search the log
- no console take-over when running your script
Quick setup:
import logging
import logging_setup
log = logging_setup.logging_setup('my_log') #use a unique name
log.debug('a message for the log')
log.error('oh no.')
log.warning('oh no.')
log.critical('call 911... or omz')
log.info('well now it seems you have some mighty fine logging going on.')
logging.shutdown() #necessary at the end of the script running
I did test this with using it in a few different files at the same time. I didn't think it was going to work because of name spaces (ex: if you use log
in two files and have imported one file into the other) - but it seems to work fine. One very important thing: if you do not use logging.shutdown()
it will not work properly (seems that logs are carried over from before).