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.

Python Logging Tutorial - Levels & Examples

Python Logging Tutorial – Levels & Examples

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

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-

  • 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')
Python Logging

Python Logging to Files

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

WARNING:root:You are warned
>>> 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

2018-08-21 16:09:28,100 Began to log
>>> logging.warning('This is a warning'); logging.warning('Your code could run into issues')

Output

2018-08-21 16:10:07,071 This is a warning
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

08/21/2018 05:02:01 PM Does this work

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

WARNING:root:Service before self

So, this was all in Python Logging Tutorial. Hope you like our explanation.

Python Interview Questions on Logging

  1. What is Python Logging?
  2. How logging is used in Python?
  3. How do you create a logging level in Python?
  4. What is the importance of logging in Python?
  5. 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

courses

DataFlair Team

DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.

Leave a Reply

Your email address will not be published. Required fields are marked *