Python

P
PythonHub
Progress 0%

Python

  • Home
  • History of Python
  • Applications of Python
  • Introduction To Python
    • What is Python
    • Character Set
    • Tokens in Python
    • Python Execution Mode
    • Variable And Identifiers
    • Data Types in Python
    • Operators And Expressions
    • Constants in Python
    • Assignment Statement
    • Input / Output in Python
    • Simple Python Scripts
    • Namespace in Python
    • 📝 Assignments
  • Operators in Python
    • Arithmetic Operators
    • Assignment Operators
    • Shorthand Assignment Operators
    • Relational Operators
    • Logical Operators
    • Bitwise Operators
    • Special Operators
    • 📝 Assignments
  • Input Output in Python
    • Accept Input
    • Output Formatting
    • 📝 Assignments
  • Conditional Statement
    • Decision Making
    • if Statement
    • IF-ELSE STATEMENT
    • IF-ELSE LADDER
    • NESTED IF-ELSE
    • Short Hand IF-ELSE
    • 📝 Assignments
  • Loops
    • Introduction to Loops
    • While Loop
    • Nested While Loop
    • 📝 While Loop Assignments
    • For Loop
    • For Loop Examples
    • Nested For Loop
    • Nested For Loop Examples
    • Infinite While Loops
    • Infinite For Loops
    • Break, Continue and Else in Loops
    • Difference Between For and While Loop
    • For Each Loop
    • 📝 For Each Assignments
    • 📝 All Loops Assignments
  • List
    • List in Python
    • Access List Elements
    • List Functions
    • Iterate (Loop) List
    • List Comprehension
    • 📝 Assignments
  • Tuple
    • Tuple in Python
    • Access Tuple Elements
    • Tuple Functions
    • Iterate (Loop) Tuple
    • Unpack Tuple
    • Tuple Comprehension
    • 📝 Assignments
  • Set
    • Set in Python
    • Access Set Elements
    • Set Methods
    • Iterate (Loop) Set
    • Pack/Unpack Set
    • Set Comprehension
    • 📝 Assignments
  • Dictionary
    • Dictionary
    • Access Dictionary Items
    • Dictionary Methods
    • Iterate (Loop) Dictionary
    • Formatting Dictionaries
    • Nested Dictionaries
    • Dictionary Comprehension
    • 📝 Assignments
  • Diff List Tuple Set Dictionary
    • List vs Tuple
    • List vs Set
    • List vs Dictionary
    • Tuple vs Set
    • Tuple vs Dictionary
    • Dictionary vs Set
    • 📝 Assignments
  • Exception
    • Error vs Exception
    • Types of Exception
    • Exception Handling
    • User Defined Exception
    • Logging Exception
    • 📝 Assignments
  • Functions
    • Introduction to Functions
    • Modular Programming
    • Types of Functions
    • Inbuilt Functions
    • Need For User-Defined Function
    • Elements of User Defined Function
    • Function Arguments
    • Nesting of Functions
    • Recursion
    • Global Local and Non Local
    • Python Lambda Functions
    • 📝 Assignments
  • Python Module
    • Introduction to Module
    • Inbuilt Modules in Python
    • User Defined Module
    • 📝 Assignments
  • File Handling
    • Introduction to Files
    • Create File
    • Read Files
    • Write to File
    • Rename File
    • Copy File
    • Move File
    • List Files in Directory
    • Binary Files
    • Zipping and Unzipping Files
    • 📝 Assignments
  • Strings
    • Basics of Strings
    • String Special Operators
    • String Formatting Operators
    • String Methods
    • 📝 Assignments
  • Regular Expressions
  • Python OOPS
    • Basics of Object Oriented
    • What are Classes and Objects?
    • Creating Class and Object
    • OOP vs Procedural Programming
    • Difference Between Classes and Objects
    • Constructors
    • Destructor
    • Built Class Methods and Attributes
    • Class and Instance Variables
    • Inheritance in Python
    • Single Inheritance
    • Multiple Inheritance
    • Multilevel Inheritance
    • Hierarchical Inheritance
    • Hybrid Inheritance
    • Abstraction
    • Method Overriding
    • Abstract Method
    • Interfaces in Python
    • Abstract Class vs Interface
    • Public, Private and Protected
    • Overloading vs Overriding
    • Inheritance vs Composition
    • Encapsulation
    • Polymorphism
    • Inner Classes
    • 📝 Assignments
  • Advanced Python
    • Iterator in Python
    • Generator in Python
    • Decorator in Python
    • Type Hints in Python
    • Async/Await Programming
    • Dataclasses in Python
    • Context Managers in Python
    • Property Decorator in Python
    • __slots__ in Python
    • Enums in Python
    • Walrus Operator in Python
    • Match-Case in Python
    • 📝 Assignments
  • Python Standard Library
    • Collections Module
    • Itertools Module
    • Functools Module
    • Datetime Module
    • JSON Module
    • OS Module
    • Sys Module
    • Random Module
    • Math Module
    • 📝 Assignments
  • Python Testing
    • Unit Testing in Python
    • Pytest Framework
    • Mocking in Python
    • 📝 Assignments
  • Python Best Practices
    • PEP 8 Style Guide
    • Docstrings in Python
    • Logging in Python
    • Code Optimization Tips
    • Debugging Techniques
    • 📝 Assignments
  • MySQL Database in Python
    • Introduction to MySQL with Python
    • DBMS vs File System
    • Connecting to MySQL Database
    • Create Database in MySQL
    • Create Table in MySQL
    • Insert Data in MySQL
    • Insert Multiple Rows
    • Select Data from MySQL
    • WHERE Clause in MySQL
    • Update Data in MySQL
    • Delete Data from MySQL
    • Parameterized Queries
    • Transaction Management
    • Error Handling
    • Connection Pooling
    • MySQL Drivers Guide
    • Joins in MySQL
    • Aggregation Functions
    • Backup MySQL Database
    • Best Practices
    • 📝 Assignments
  • MySQL Database Operations
    • SELECT Statement
    • MySQL Operators
    • DDL Statements
    • DML Statements
    • Subqueries
    • JOIN Operations
    • Aggregation
    • Case Study
    • 📝 Assignments
  • Graphics in Python
  • Threads in Python
    • Introduction to Threads
    • Process vs Threads
    • Concurrent Programming & GIL
    • Uses of Threads
    • Creating Threads
    • Single Tasking
    • Multi Tasking
    • Thread Synchronization
    • 📝 Assignments
  • Interview Questions & Answers
  • Python Case Studies
  • Multiple Choice Questions
  • 📝 Practice Problems
Get in Touch
  • tech2dsm@gmail.com

© Sankalan Data Tech

Python Language Interactive Tutorial

Python: Logging Exceptions

Python Logging Exception - Complete Guide

Learn how to log exceptions in Python using the logging module for debugging and monitoring.

Created by Sankalan Data Tech Team Verified
Data Engineers, Analysts, Scientists & Trainers
Created by experienced Python developers, data engineers, and data scientists to make programming easy through practical examples, real-world experience, and clear explanations.
On this page:
  • What is Logging?
  • Why Log Exceptions?
  • Basic Logging Setup
  • Logging Levels
  • Logging Exceptions
  • Logging Exception Details
  • Logging to Files
  • Formatting Log Messages
  • Best Practices
  • Common Mistakes
  • Try It Yourself
  • Quiz
  • FAQ
Share this tutorial:
Twitter LinkedIn Facebook WhatsApp Reddit Telegram Email Copy Link
What You'll Learn Here
  • What is logging — understanding the logging module
  • Why log exceptions — benefits of logging errors
  • Basic logging setup — configuring the logger
  • Logging levels — DEBUG, INFO, WARNING, ERROR, CRITICAL
  • Logging exceptions — using logging.exception()
  • Logging to files — saving logs to files
  • Formatting — customizing log messages
  • Best practices — writing effective logs

What is Logging?

Logging is the process of recording events, errors, and information that occur during a program's execution. Python's built-in logging module provides a flexible and powerful way to add logging to your applications.

Definition: Logging is a systematic way to record information about a program's execution, including errors, warnings, and informational messages, for debugging, monitoring, and analysis purposes.

Unlike print() statements, logging:

  • Allows different severity levels
  • Can be directed to files and console
  • Provides timestamps and context
  • Can be configured without changing code
  • Is production-ready

💡 Key concept: Logging is essential for production applications. It helps you understand what happened when something goes wrong.

Why Log Exceptions?

1

Benefits of Exception Logging

# The problem with print()
def process_data(data):
    try:
        result = complex_operation(data)
        return result
    except Exception as e:
        print(f"Error: {e}")  # Error is printed but lost

# With logging
import logging

logging.basicConfig(level=logging.INFO)

def process_data(data):
    try:
        result = complex_operation(data)
        return result
    except Exception as e:
        logging.error(f"Failed to process data: {e}")
        # Error is logged with timestamp and context
        # Can be sent to files, monitoring systems, etc.

# Benefits of logging:
# 1. Persistent records
# 2. Timestamps for debugging
# 3. Different severity levels
# 4. Can be filtered and searched
# 5. Production-ready

Key benefits:

  • Persistence — logs remain after program ends
  • Context — timestamps, module names, line numbers
  • Organization — different levels for different severity
  • Analysis — search, filter, and analyze errors
  • Monitoring — integrate with monitoring tools

Quick Check: Why is logging better than print() for errors? (Answer: Logs are persistent, have timestamps, and can be filtered)

Basic Logging Setup

2

Setting Up the Logger

import logging

# Basic setup
logging.basicConfig(level=logging.INFO)

# Logging messages
logging.debug("This is a debug message")
logging.info("This is an info message")
logging.warning("This is a warning message")
logging.error("This is an error message")
logging.critical("This is a critical message")

# Output:
# INFO:root:This is an info message
# WARNING:root:This is a warning message
# ERROR:root:This is an error message
# CRITICAL:root:This is a critical message

# Note: DEBUG messages are not shown because level is INFO

Key points:

  • basicConfig() sets up the logging system
  • level controls which messages are shown
  • Default format: LEVEL:logger_name:message
  • Root logger name is root

Quick Check: What function is used to configure logging? (Answer: logging.basicConfig())

Logging Levels

3

Understanding Severity Levels

import logging

logging.basicConfig(level=logging.DEBUG)

# All levels
logging.debug("Debug - detailed information for debugging")
logging.info("Info - confirmation that things are working")
logging.warning("Warning - something unexpected happened")
logging.error("Error - a serious problem occurred")
logging.critical("Critical - a fatal error occurred")

# Severity levels (increasing severity):
# DEBUG     = 10
# INFO      = 20
# WARNING   = 30
# ERROR     = 40
# CRITICAL  = 50

# Setting the level
logging.basicConfig(level=logging.WARNING)
# Only WARNING, ERROR, and CRITICAL will be shown

# Programmatic levels
logging.log(logging.DEBUG, "Debug message")

When to use each level:

  • DEBUG — detailed information for developers
  • INFO — confirming things are working as expected
  • WARNING — something unexpected but not fatal
  • ERROR — a serious problem, operation failed
  • CRITICAL — a fatal error, program may stop

Quick Check: Which logging level is the most severe? (Answer: CRITICAL)

Logging Exceptions

4

Using logging.exception()

import logging

logging.basicConfig(level=logging.ERROR)

def divide(a, b):
    try:
        return a / b
    except ZeroDivisionError as e:
        # logging.exception() automatically includes traceback
        logging.exception("Division by zero occurred")
        return None

result = divide(10, 0)

# Output:
# ERROR:root:Division by zero occurred
# Traceback (most recent call last):
#   File "", line 3, in divide
# ZeroDivisionError: division by zero

# Using error() with exc_info=True
def process_data(data):
    try:
        # Some operation
        result = risky_operation(data)
        return result
    except Exception as e:
        # This also logs the traceback
        logging.error(f"Error processing data: {e}", exc_info=True)
        return None

Key points:

  • logging.exception() logs at ERROR level
  • It automatically includes the traceback
  • Use exc_info=True with other logging methods
  • This is the preferred way to log exceptions

Quick Check: What method automatically includes the traceback when logging? (Answer: logging.exception())

Logging Exception Details

5

Capturing Error Information

import logging
import traceback

logging.basicConfig(level=logging.ERROR)

def log_error_details():
    try:
        risky_operation()
    except Exception as e:
        # 1. Log the exception with traceback
        logging.exception("An error occurred")

        # 2. Log specific details
        logging.error(f"Error type: {type(e).__name__}")
        logging.error(f"Error message: {str(e)}")

        # 3. Log the stack trace
        tb = traceback.format_exc()
        logging.error(f"Stack trace:\n{tb}")

        # 4. Log custom context
        logging.error(f"Context: user_id=123, operation='calculate'")

# Example output:
# ERROR:root:An error occurred
# Traceback (most recent call last):
#   ...
# ERROR:root:Error type: ValueError
# ERROR:root:Error message: invalid value
# ERROR:root:Stack trace: ...
# ERROR:root:Context: user_id=123, operation='calculate'

Best practices for exception logging:

  • Log the exception type and message
  • Include context (user, operation, data)
  • Use traceback for debugging
  • Log at the appropriate level

Logging to Files

6

Saving Logs to Files

import logging

# Configure logging to write to a file
logging.basicConfig(
    level=logging.INFO,
    filename='app.log',
    filemode='a',  # 'a' for append, 'w' for overwrite
    format='%(asctime)s - %(levelname)s - %(message)s'
)

# Log messages
logging.info("Application started")
logging.warning("Low disk space warning")

try:
    result = 10 / 0
except ZeroDivisionError:
    logging.exception("Division error occurred")

# The logs will be written to app.log

# Creating a custom logger with multiple handlers
logger = logging.getLogger(__name__)

# Console handler
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO)

# File handler
file_handler = logging.FileHandler('app.log')
file_handler.setLevel(logging.ERROR)

# Add handlers
logger.addHandler(console_handler)
logger.addHandler(file_handler)

Key points:

  • filename — specify the log file name
  • filemode — 'a' (append) or 'w' (overwrite)
  • Use handlers for multiple destinations
  • Different handlers can have different levels

Quick Check: What parameter specifies the log file name? (Answer: filename)

Formatting Log Messages

7

Customizing Log Format

import logging

# Basic format
logging.basicConfig(
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)

# Available format attributes:
# %(asctime)s    - timestamp
# %(name)s       - logger name
# %(levelname)s  - severity level
# %(message)s    - the log message
# %(pathname)s   - file path
# %(filename)s   - file name
# %(lineno)d     - line number
# %(funcName)s   - function name
# %(thread)d     - thread ID
# %(process)d    - process ID

# Advanced format with all details
logging.basicConfig(
    format='%(asctime)s - %(levelname)s - %(filename)s:%(lineno)d - %(funcName)s() - %(message)s',
    datefmt='%Y-%m-%d %H:%M:%S'
)

# Custom formatter
formatter = logging.Formatter(
    '%(asctime)s - %(levelname)s - [%(filename)s:%(lineno)d] - %(message)s'
)

# Applying formatter to a handler
handler = logging.StreamHandler()
handler.setFormatter(formatter)

Key format attributes:

  • %(asctime)s — timestamp
  • %(levelname)s — severity level
  • %(filename)s — source file name
  • %(lineno)d — line number
  • %(funcName)s — function name

Quick Check: What attribute gives you the line number? (Answer: %(lineno)d)

Best Practices

8

Writing Effective Logs

import logging

# 1. Use a named logger
logger = logging.getLogger(__name__)

# 2. Configure once at module level
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)

# 3. Log at appropriate levels
def process_user(user_id):
    logger.info(f"Processing user: {user_id}")
    try:
        # Operation
        pass
    except Exception as e:
        logger.exception(f"Failed to process user {user_id}")
        # Don't just log the error message, include context

# 4. Include context in logs
logger.info(f"User {user_id} - Action completed in {duration:.2f}s")

# 5. Avoid logging sensitive information
# WRONG: logger.info(f"User password: {password}")
# CORRECT: logger.info(f"User login attempt: {username}")

# 6. Use structured logging for better searching
logger.info(f"Event: user_login, user: {user_id}, status: success")

# 7. Configure logging in a central place
def setup_logging():
    logging.basicConfig(
        level=logging.INFO,
        format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
        handlers=[
            logging.FileHandler('app.log'),
            logging.StreamHandler()
        ]
    )

Best practices summary:

  • Use named loggers — logging.getLogger(__name__)
  • Log at appropriate levels — INFO for normal flow, ERROR for exceptions
  • Include context — user IDs, operation names, timings
  • Avoid sensitive data — no passwords, personal info
  • Use structured format — for better searching
  • Configure centrally — one place for logging setup

Common Mistakes

9

Things to Watch Out For

Using print() Instead of logging

# WRONG — print() is temporary
print("Error occurred")

# CORRECT — logging is permanent
import logging
logging.error("Error occurred")

Not Including Exception Context

# WRONG — no context
try:
    result = divide(a, b)
except ZeroDivisionError:
    logging.error("Division error")

# CORRECT — include context
try:
    result = divide(a, b)
except ZeroDivisionError:
    logging.error(f"Division error: {a}/{b}")

Logging at the Wrong Level

# WRONG — using INFO for errors
logging.info("Database connection failed")

# CORRECT — use ERROR for errors
logging.error("Database connection failed")

# WRONG — using ERROR for normal operations
logging.error("User logged in")

# CORRECT — use INFO for normal operations
logging.info("User logged in")

Quick Check: What is the most common mistake with logging? (Answer: Using print() instead of logging)

Try It Yourself

Experiment with logging exceptions in the editor below. Modify the code and see what happens.

Loading Pyodide... 0%
Python Code Editor
========================================
LOGGING EXCEPTION PRACTICE
========================================

Logging levels:
INFO message
WARNING message
ERROR message

1. LOGGING EXCEPTION
ERROR:root:Division by zero occurred
Traceback (most recent call last):
File "<stdin>", line 4, in divide
ZeroDivisionError: division by zero

2. LOGGING WITH CONTEXT
ERROR:root:Division error: 10/0

3. DIFFERENT LOGGING LEVELS
INFO:root:This is an informational message
WARNING:root:This is a warning
ERROR:root:This is an error

Logging exception practice complete!
🏆

You've Got It!

You now understand logging exceptions in Python — setting up logging, logging levels, logging.exception(), logging to files, formatting, and best practices. This is a professional skill for production applications!

Quick Quiz

Test what you've learned:

1. What method automatically includes the traceback when logging?
2. What logging level should you use for a fatal error?
3. What function is used to configure logging?
4. What parameter specifies the log file name?
5. What is the difference between logging.info() and print()?

Frequently Asked Questions

What is the difference between logging and print()? ▼

logging provides timestamps, severity levels, persistence to files, and can be configured without changing code. print() is for simple debugging and is not suitable for production.

What is the difference between logging.error() and logging.exception()? ▼

logging.exception() automatically includes the traceback. logging.error() just logs the message. Use exception() when logging exceptions, error() for other errors.

How do I log to a file? ▼

Use logging.basicConfig(filename='app.log') or add a FileHandler. This will write all logs to the specified file.

What are the different logging levels? ▼

From lowest to highest: DEBUG, INFO, WARNING, ERROR, CRITICAL. Each level has a numeric value and is used for different severity of events.

How do I format log messages? ▼

Use the format parameter in basicConfig(). Example: format='%(asctime)s - %(levelname)s - %(message)s'.

What's a common interview question about logging? ▼

Common questions include: "What is the difference between logging and print?" and "How do you log exceptions in Python?" Answer: Use the logging module and logging.exception().

Where to Go From Here

Now that you understand logging exceptions, check out these related topics:

Exception Assignments

Practice what you've learned with assignments.

Learn More →

User Defined Exception

Create your own custom exceptions.

Learn More →

Exception Handling

Review the basics of exception handling.

Learn More →
Interview Resources
  • Python Syntax & Variables Interview Questions
  • Top SQL Interview Questions & Answers
  • SQL Joins: Displaying Data from Multiple Tables FAQ
Previous: User Defined Exception Next: Exception Assignments →