Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Python
  3. Question
Python

How to log the Exception message using by logging module?

Asked by Cameron Oliver Jul 22, 2024 997 views 2 answers
Share

About this question

I am currently engaged in a particular task that is related to working on a Python-based application that can process data from multiple sources. Occasionally, some of the data sources may fail or return invalid data, which can cause exceptions to be raised. To handle these errors gracefully, I would need to log the exception message to debug and alter the purpose. 

I have the following code snippet which can process data from a list of sources:

Data_sources = [“source1”, “source2”, “source3”]
For source in data_sources:
    Try:
        # Simulating data processing
        If source == “source2”:
            Raise ValueError(“Invalid data from source2”)
        Print(f”Processed data from {source}”)
    Except Exception as e:
        # Log the exception message
        Pass

How can I modify the code to log the exception message by using the built-in logging module to ensure that the exception message should be retrieved and included in the log output? 

Your answer

2 Answers

Surijulia Latest answer

Answered on Sep 18, 2026

The issue is that the exception is being caught but discarded with Retro Bowl pass, so the error message never reaches your logs; using Python’s built-in logging module with logging.exception() is a straightforward way to capture both the message and traceback.

Was this helpful?

More Python discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Python Blogs

Guides, tips and career advice on Python from JanBask experts.