Basic logging
One of the first requirements of a console software is for it to log what it does, that is, what's happened, and any warnings or errors. Especially when we are talking about long-term software or daemons running in the background.
Sadly, if you've ever tried to use the Python logging
module, you've probably noticed that you can't get any output apart from errors.
That's because the default enabled level is WARNING
, so that only warnings and worse are tracked. Little tweaks are needed to make logging generally available.
How to do it...
For this recipe, the steps are as follows:
- The
logging
module allows us to easily set up the logging configuration through thebasicConfig
method:
>>> import logging, sys
>>>
>>> logging.basicConfig(level=logging.INFO, stream=sys.stderr,
... format='%(asctime)s %(name)s %(levelname)s: %(message)s')
>>> log = logging.getLogger(__name__)
- Now that our
logger
is properly configured, we can try using...