Python Logging Module – Explore Logging File & Levels
Master Python with 70+ Hands-on Projects and Get Job-ready - Learn Python
In this Python tutorial, we will discuss how to perform Python Logging. Moreover, we will use the Python logging module for this.
Also, we will discuss debug, set level and error in Python Logging.
So, let’s start the Python Logging Tutorial.
What is Logging in Python?
Logging, in software applications, is a way to track events. Before we can proceed, telling you more about it, we want to exemplify.
>>> import logging >>> logging.warning('This is a warning')
Output
Basically, Logging is a module with the Python Standard Library ever since version 2.3. Effectively, logging is a way to track events occurring when we run a piece of software.
As a developer, you add logging calls to your code denoting the occurrence of certain events. Purposes of logging in Python are two-
- Diagnostic Logging- To record events that revolve around the application’s operation.
- Audit Logging- To record events for business analysis.
Python Logging to File
What if we wanted to save these messages to a text file instead of throwing them to the Logging console?
>>> import logging >>> logging.basicConfig(filename='demolog.log',level=logging.DEBUG) >>> logging.warning('This is a warning'); logging.warning('You may run into issues with your code')
We pass a filename argument to the logging.basicConfig() method. Here, we call our file demolog.log. Such a file is one we can consult over time.
1. Python Logging Levels – Severity
To the basicConfig() method above, we passed the severity level logging.DEBUG. This is the importance the developer ascribes to an event. We have several other values-
- DEBUG- Information for problem diagnostics only.
- INFO- The program runs as expected.
- WARNING- To indicate that something went wrong.
- ERROR- This means the software no longer functions.
- CRITICAL- For a very serious error.
Here, WARNING is the default logging level; this ignores other messages.
With this default, nothing shows up for a call to info().
>>> import logging >>> logging.warning('You are warned')
Output
>>> logging.info('Deal with it') >>>
Displaying Date/Time For Python Logging
To enable the time of logging in Python, you can use the following piece of Python code-
logging.basicConfig(format=’%(asctime)s %(message)s’)
>>> import logging >>> logging.basicConfig(format='%(asctime)s %(message)s', level=logging.DEBUG) >>> logging.info('Began to log')
Output
>>> logging.warning('This is a warning'); logging.warning('Your code could run into issues')
Output
2018-08-21 16:10:07,121 Your code could run into issues
1. Setting a Python Logging Format
Let’s see Logging Formatter example in Python-
>>> import logging >>> logging.basicConfig(format='%(asctime)s %(message)s', level=logging.DEBUG,datefmt='%m/%d/%Y %I:%M:%S %p') >>> logging.warning('Does this work')
Output
Python Logging Functions
- logging.info() or logging.debug() for the detailed output of events that occur during normal operation of a program.
- warnings.warn() issues a warning for a runtime event if the issue is avoidable.
- logging.warning() issues a warning for a runtime event if we need to note the event even when the client can do nothing about it.
- logging.error(), logging.exception(), or logging.critical() report the suppression of an error without raising an exception.
Logging Variable Data
Let’s take a few more examples of Logging in Python before we can bid goodbye for the day.
It is possible to use a format string to describe an event and then append variable data as arguments.
Let’s take an example, shall we?
>>> logging.warning('%s before %s','Service','self')
Output
So, this was all in Python Logging Tutorial. Hope you like our explanation.
Python Interview Questions on Logging
- What is Python Logging?
- How logging is used in Python?
- How do you create a logging level in Python?
- What is the importance of logging in Python?
- What are the Python logging best practices?
Conclusion
With this, we conclude our tutorial on Logging in Python.
We saw the logging module, levels of severity, how to log to a file, and how to display date/time for Python Logging.
We also learned how to log variable data and took a look at which function to call and when.
Your opinion matters
Please write your valuable feedback about DataFlair on Google