Logging in Python
A tutorial on python logging
In this notebook we will learn how to do logging in python. Logging is very important once the project grows beyond a POC stage. Having a logging leads to better debugging, proper error identification, good standard to have. So we'll going to see how to apply logging through few examples
Logging has differnt configuration, which are important in implementing logging,
- level
- formatter
- Handlers
- Filters
- DEBUG - Detailed information, typically of interest only when diagnosing problems.
- INFO - Confirmation that things are working as expected.
- WARNING - An indication that something unexpected happened, or indicative of some problem in the near future (e.g. ‘disk space low’). The software is still working as expected.
- ERROR - Due to a more serious problem, the software has not been able to perform some function.
- CRITICAL - A serious error, indicating that the program itself may be unable to continue running.
Level Numeric value
=====================
CRITICAL 50
ERROR 40
WARNING 30
INFO 20
DEBUG 10
NOTSET 0
logging formatter helps formating the message which we write to disk or console. Formatters specify the layout of log records in the final output,
Example,
- asctime - %(asctime)s
- created - %(created)f
- filename - %(filename)s
Handlers send the log records (created by loggers) to the appropriate destination. Filters provide a finer grained facility for determining which log records to output.
- queue_handler = QueueHandler(que)
- stream_handler = logging.StreamHandler()
- file_handler = logging.FileHandler("csr_logfile.log")
- Corey Schaffer python Tutorials (https://www.youtube.com/channel/UCCezIgC97PvUuR4_gbFUs5g)
- https://docs.python.org/3/library/logging.handlers.html#module-logging.handlers
- https://docs.python.org/3/library/logging.html#logrecord-attributes
- https://docs.python.org/3/howto/logging-cookbook.html#logging-cookbook
- https://docs.python.org/3/library/logging.html#logrecord-attributes