Python Logging Tutorials and Examples
Python Logging
Logging helps to monitor the state of the application. We can see if there are any warnings or errors raised by the program. We can log details like timestamp, message, stack, etc.
In this tutorial, we shall learn about the logging library, like how to get started with it, and what are the tools available in this library, etc.
Import logging library
You have to explicitly import Python Logging library, before using any of the logging functions in your program.
import logging
# then some logging statement(s)
Here is a basic example of using logging in Python.
Python Program
import logging
#logging warning
logging.warning('This is a simple Python logging example')
We have imported logging module, and logged a warning. The console output would be as shown below.
Output
WARNING:root:This is a simple Python logger example
Logging Tutorials
Following examples deal with how to configure logger; different levels in logging; how to set logging level; format logging values; etc.
- Python Logging Basic Configuration
- Python Logging Messages to a Log File
- Python Logging - Set DEBUG Level
- Python Logging - Set INFO Level
- Python Logging - Set WARNING Level
- Python Logging - Set ERROR Level
- Python Logging - Set CRITICAL Level
- Python Logging - Set Logger Level
- Python Logging - Format Record
- Python Logging - Format Time
- Python Logging - Format Exception
- Python Logging - Format Stack
Summary
In this tutorial of Python Examples, we learned how to import python logging library, some of the logging levels available in this library.