Say Goodbye to Print(): Use Logging Module for Efficient Debugging – KDnuggets


Picture by Writer | DALLE-3 & Canva

 

Many people begin our programming journey with YouTube movies, and for the sake of simplicity, they typically use print() statements to trace bugs. That is truthful sufficient, however as novices undertake this behavior, it will possibly grow to be problematic. Though these statements may work for easy scripts, as your codebase expands, this strategy turns into extremely inefficient. Due to this fact, on this article, I’ll introduce you to Python’s built-in logging module, which solves this drawback. We’ll see what logging is, the way it differs from the print() statements, and we may also cowl a sensible instance to completely perceive its performance.

 

Why Use the Logging Module As a substitute of Print()?

 

Once we discuss debugging, the Python logging module supplies rather more detailed data than easy print() statements. This contains timestamps, module names, log ranges, and line numbers the place errors occurred, and many others. These additional particulars assist us perceive the habits of our code extra successfully. The knowledge we need to log will depend on the wants of the appliance and the developer’s desire. So, earlier than we proceed additional, let’s talk about log ranges and find out how to set them.

 

Logging Ranges

 
You’ll be able to management the quantity of knowledge you need to see utilizing these log ranges. Every log stage has a numerical worth that denotes its severity, with larger values indicating extra extreme occasions. For instance, for those who set your log stage to WARNING, you are telling the logging module to solely present you messages which are of WARNING stage or larger. This implies you will not see any DEBUG, INFO, or different much less extreme messages. This manner, you may deal with the necessary occasions and ignore the noise

Right here’s a desk that reveals the small print of what every log stage represents:

Log Stage Numerical Worth Goal
DEBUG 10 Supplies detailed data for diagnosing code-related points, resembling printing variable values and performance name traces.
INFO 20 Used to substantiate that this system is working as anticipated, like displaying startup messages and progress indicators.
WARNING 30 Signifies a possible drawback that is probably not crucial to interrupt this system’s execution however might trigger points in a while.
ERROR 40 Represents an surprising habits of the code that impacts its performance, resembling exceptions, syntax errors, or out-of-memory errors.
CRITICAL 50 Denotes a extreme error that may result in the termination of this system, like system crashes or deadly errors.

 

Setting Up the Logging Module

 

To make use of the logging module, you could comply with some steps for configuration. This contains making a logger, setting the logging stage, making a formatter, and defining a number of handlers. A handler principally decides the place to ship your log messages, resembling to the console or a file. Let’s begin with a easy instance. We’ll arrange the logging module to do two issues: first, it’s going to present messages on the console, giving us helpful data (on the INFO stage). Second, it’s going to save extra detailed messages to a file (on the DEBUG stage). I might like it for those who might comply with alongside!

 

1. Setting the log stage

The default stage of the logger is about to WARNING. In our case, our two handlers are set to DEBUG and INFO ranges. Therefore, to make sure all messages are managed correctly, now we have to set the logger’s stage to the bottom stage amongst all handlers, which, on this case, is DEBUG.

import logging

# Create a logger
logger = logging.getLogger(__name__)

# Set logger stage to DEBUG
logger.setLevel(logging.DEBUG)

 

 

2. Making a Formatter

You’ll be able to personalize your log messages utilizing formatters. These formatters determine how your log messages will look. Right here, we’ll arrange the formatter to incorporate the timestamp, the log stage, and the message content material utilizing the command beneath:

formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')

 

 

3. Creating Handlers

As mentioned beforehand, handlers handle the place your log messages will likely be despatched. We’ll create two handlers: a console handler to log messages to the console and a file handler to jot down log messages to a file named ‘app.log’.

console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO)
console_handler.setFormatter(formatter)

file_handler = logging.FileHandler('app.log')
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(formatter)

 

Each handlers are then added to the logger utilizing the addHandler() methodology.

logger.addHandler(console_handler)
logger.addHandler(file_handler)

 

4. Testing the Logging Setup

Now that our setup is full, let’s check if it is working appropriately earlier than shifting to the real-life instance. We are able to log some messages as follows:

logger.debug('This can be a debug message')
logger.data('That is an data message')
logger.warning('This can be a warning message')
logger.error('That is an error message')
logger.crucial('This can be a crucial message')

 

While you run this code, it’s best to see the log messages printed to the console and written to a file named ‘app.log’, like this:

Console

2024-05-18 11:51:44,187 - INFO - That is an data message
2024-05-18 11:51:44,187 - WARNING - This can be a warning message
2024-05-18 11:51:44,187 - ERROR - That is an error message
2024-05-18 11:51:44,187 - CRITICAL - This can be a crucial message

 
app.log

2024-05-18 11:51:44,187 - DEBUG - This can be a debug message
2024-05-18 11:51:44,187 - INFO - That is an data message
2024-05-18 11:51:44,187 - WARNING - This can be a warning message
2024-05-18 11:51:44,187 - ERROR - That is an error message
2024-05-18 11:51:44,187 - CRITICAL - This can be a crucial message

 

Logging Person Exercise in a Net Software

 

On this easy instance, we’ll create a primary internet software that logs person exercise utilizing Python’s logging module. This software may have two endpoints: one for logging profitable login makes an attempt and the opposite to doc failed ones (INFO for achievement and WARNING for failures).

 

1. Setting Up Your Setting

Earlier than beginning, arrange your digital setting and set up Flask:

python -m venv myenv

# For Mac
supply myenv/bin/activate

#Set up flask
pip set up flask

 

2. Making a Easy Flask Software

While you ship a POST request to the /login endpoint with a username and password parameter, the server will test if the credentials are legitimate. If they’re, the logger data the occasion utilizing logger.data() to indicate a profitable login try. Nonetheless, if the credentials are invalid, the logger data the occasion as a failed login try utilizing logger.error().

#Making Imports
from flask import Flask, request
import logging
import os

# Initialize the Flask app
app = Flask(__name__)

# Configure logging
if not os.path.exists('logs'):
    os.makedirs('logs')
log_file="logs/app.log"
logging.basicConfig(filename=log_file, stage=logging.DEBUG, format="%(asctime)s - %(levelname)s - %(message)s")
log = logging.getLogger(__name__)


# Outline route and handler
@app.route('/login', strategies=['POST'])
def login():
    log.data('Obtained login request')
    username = request.kind['username']
    password = request.kind['password']
    if username == 'admin' and password == 'password':
        log.data('Login profitable')
        return 'Welcome, admin!'
    else:
        log.error('Invalid credentials')
        return 'Invalid username or password', 401

if __name__ == '__main__':
    app.run(debug=True)

 

3. Testing the Software

To check the appliance, run the Python script and entry the /login endpoint utilizing an internet browser or a software like curl. For instance:

Check Case 01

 curl -X POST -d "username=admin&password=password" http://localhost:5000/login

 
Output

 
Check Case 02

curl -X POST -d "username=admin&password=wrongpassword" http://localhost:5000/login

 
Output

Invalid username or password

 
app.log

2024-05-18 12:36:56,845 - INFO - Obtained login request
2024-05-18 12:36:56,846 - INFO - Login profitable
2024-05-18 12:36:56,847 - INFO - 127.0.0.1 - - [18/May/2024 12:36:56] "POST /login HTTP/1.1" 200 -
2024-05-18 12:37:00,960 - INFO - Obtained login request
2024-05-18 12:37:00,960 - ERROR - Invalid credentials
2024-05-18 12:37:00,960 - INFO - 127.0.0.1 - - [18/May/2024 12:37:00] "POST /login HTTP/1.1" 200 -

 

Wrapping Up

 

And that wraps up this text. I strongly counsel making logging part of your coding routine. It is a good way to maintain your code clear and make debugging simpler. If you wish to dive deeper, you may discover the Python logging documentation for extra options and superior methods. And for those who’re keen to boost your Python abilities additional, be happy to take a look at a few of my different articles:

 
 

Kanwal Mehreen Kanwal is a machine studying engineer and a technical author with a profound ardour for information science and the intersection of AI with medication. She co-authored the e book “Maximizing Productivity with ChatGPT”. As a Google Era Scholar 2022 for APAC, she champions variety and educational excellence. She’s additionally acknowledged as a Teradata Variety in Tech Scholar, Mitacs Globalink Analysis Scholar, and Harvard WeCode Scholar. Kanwal is an ardent advocate for change, having based FEMCodes to empower ladies in STEM fields.

Recent articles

Patch Alert: Essential Apache Struts Flaw Discovered, Exploitation Makes an attempt Detected

î ‚Dec 18, 2024î „Ravie LakshmananCyber Assault / Vulnerability Risk actors are...

Meta Fined €251 Million for 2018 Knowledge Breach Impacting 29 Million Accounts

î ‚Dec 18, 2024î „Ravie LakshmananKnowledge Breach / Privateness Meta Platforms, the...