Python Logging Module – Explore Logging File & Levels
1. Python Logging – Objective
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.
2. 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')
WARNING:root:This is a warning
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-
Have a look at the Python Library.
- Diagnostic Logging- To record events that revolve around the application’s operation.
- Audit Logging- To record events for business analysis.
3. 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.
a. 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.
Let’s revise the Python Zip File
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')
WARNING:root:You are warned
>>> logging.info('Deal with it') >>>
4. Displaying Date/Time For Python Logging
To enable the time of logging in Python, you can use the following piece of Python code-
>>> import logging >>> logging.basicConfig(format='%(asctime)s %(message)s', level=logging.DEBUG) >>> logging.info('Began to log')
2018-08-21 16:09:28,100 Began to log
>>> logging.warning('This is a warning'); logging.warning('Your code could run into issues')
2018-08-21 16:10:07,071 This is a warning
2018-08-21 16:10:07,121 Your code could run into issues
Have a look at Python 3 extension
a. 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')
08/21/2018 05:02:01 PM Does this work
5. 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.
6. 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')
WARNING:root:Service before self
Do you know about Python Packages?
So, this was all in Python Logging Tutorial. Hope you like our explanation.
7. Conclusion – Logging in Python
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. Still, if you have any confusion, ask in the comment.
See also –
Python NumPy Tutorial