Python Logging 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

List of Python Logging Examples

Following examples deal with how to configure logger; different levels in logging; how to set logging level; format logging values; etc.

Summary

In this tutorial of Python Examples, we learned how to import python logging library, some of the logging levels available in this library.