Logging in Python: A Complete Beginner Guide

When writing programs in Python, errors and unexpected behavior are common. To understand what is happening inside your code, developers use a technique called logging. Logging helps you track events, errors, and important information while your program is running.

Unlike simple print statements, logging is more powerful, flexible, and suitable for real-world applications. In this blog, we will understand what logging is, why it is important, and how to use it in Python with simple examples.


What is Logging in Python?

Logging is a way to record messages about the execution of a program. These messages can include:

  • Errors
  • Warnings
  • Information messages
  • Debug details

Python provides a built-in module called logging that makes it easy to implement logging in your applications.


Why Use Logging Instead of Print?

Many beginners use print() statements to debug their code. However, logging is much better because:

  • It provides different severity levels (info, warning, error, etc.)
  • Messages can be saved in files
  • It is easier to control and format output
  • Suitable for large applications
  • Helps in debugging production systems

In short, logging is a professional way to track program behavior.


Basic Logging in Python

Python’s logging module is simple to use. Let’s start with a basic example.

import logginglogging.basicConfig(level=logging.INFO)logging.info("This is an info message")
logging.warning("This is a warning message")
logging.error("This is an error message")

Explanation:

  • basicConfig() sets up basic configuration
  • level=logging.INFO sets the minimum log level
  • Different log functions display different types of messages

Logging Levels in Python

Python provides different logging levels:

1. DEBUG

Used for detailed information, mainly for debugging.

2. INFO

Used for general information about program execution.

3. WARNING

Indicates something unexpected, but not an error.

4. ERROR

Indicates a serious problem in the program.

5. CRITICAL

Indicates a very serious error that may stop the program.


Example of Logging Levels

import logginglogging.basicConfig(level=logging.DEBUG)logging.debug("Debugging information")
logging.info("Program is running")
logging.warning("This is a warning")
logging.error("An error occurred")
logging.critical("Critical issue detected")

Each level helps you understand the severity of events in your program.


Logging to a File

Instead of showing logs on the screen, you can save them in a file.

import logginglogging.basicConfig(
filename='app.log',
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s'
)logging.info("Application started")
logging.warning("Low memory warning")
logging.error("An error occurred")

Benefits:

  • Keeps a record of program activity
  • Useful for debugging later
  • Helps in production environments

Customizing Log Format

You can customize how logs are displayed using format strings.

Example:

import logginglogging.basicConfig(
format='%(levelname)s:%(message)s',
level=logging.DEBUG
)logging.info("Custom formatted log message")

You can also include:

  • Time (%(asctime)s)
  • Log level (%(levelname)s)
  • Function name (%(funcName)s)

Logging in Functions

Logging is very useful inside functions to track execution flow.

import logginglogging.basicConfig(level=logging.INFO)def add(a, b):
logging.info("Adding two numbers")
return a + bresult = add(5, 3)
logging.info(f"Result is {result}")

This helps you understand what is happening inside your program.


Best Practices for Logging

To use logging effectively, follow these best practices:

  • Use appropriate logging levels
  • Avoid using print statements for debugging
  • Always log errors and exceptions
  • Use file logging in production
  • Keep log messages clear and meaningful

Logging vs Debugging

LoggingDebugging
Records program eventsFinds and fixes errors
Runs during executionUsed during development
Stored in files or consoleInteractive process

Logging helps you understand what went wrong, while debugging helps you fix it.


Real-World Uses of Logging

Logging is widely used in:

  • Web applications
  • Banking systems
  • E-commerce platforms
  • APIs and backend systems
  • Machine learning projects

It helps developers monitor system performance and detect issues quickly.


Logging in Python is an essential skill for every developer. It helps you track program behavior, identify errors, and improve code quality.

By using Python’s logging module, you can build more reliable and professional applications. Whether you are a beginner or an advanced programmer, mastering logging will make debugging easier and your projects more efficient.

Start using logging in your Python programs today and take your coding skills to the next level.

For More Information and Updates, Connect With Us

Stay connected and keep learning with Python Training !

Leave a Reply

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

About Us

Luckily friends do ashamed to do suppose. Tried meant mr smile so. Exquisite behaviour as to middleton perfectly. Chicken no wishing waiting am. Say concerns dwelling graceful.

Services

Most Recent Posts

  • All Post
  • Accounting
  • Branding
  • Cybersecurity
  • Data Analytics
  • Development
  • Education
  • Education Technology
  • Health Technology
  • Leadership
  • Management
  • Neuroscience and Technology
  • Programming
  • Programming and Development
  • Programming Languages
  • Technology
  • Technology & Innovation
  • Technology and Creativity
  • Web Development
  • Web Development Guides

Category

© 2025 Created with Emancipation Edutech Pvt Ltd