How to log a Python exception? Last Updated : 12 Feb, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report To log an exception in Python we can use a logging module and through that, we can log the error. The logging module provides a set of functions for simple logging and the following purposes DEBUGINFOWARNINGERRORCRITICALLog a Python Exception Examples Example 1:Logging an exception in Python with an error can be done in the logging.exception() method. This function logs a message with level ERROR on this logger. The arguments are interpreted as for debug(). Exception info is added to the logging message. This method should only be called from an exception handler. Python3 # importing the module import logging try: printf("GeeksforGeeks") except Exception as Argument: logging.exception("Error occurred while printing GeeksforGeeks") Output ERROR:root:Error occurred while printing GeeksforGeeks Traceback (most recent call last): File "/home/gfg.py", line 3, in printf("GeeksforGeeks") NameError: name 'printf' is not defined Example 2We can also log the error message into a different file without showing error in the console by the following method: Python3 try: printf("GeeksforGeeks") except Exception as Argument: # creating/opening a file f = open("demofile2.txt", "a") # writing in the file f.write(str(Argument)) # closing the file f.close() Error message will be stored in file name demofille2.txt in same directory as code. Output Traceback (most recent call last): File "/home/gfg.py", line 5, in printf("GeeksforGeeks") NameError: name 'printf' is not defined Comment More infoAdvertise with us Next Article How to log a Python exception? S shivanshsaxena1 Follow Improve Article Tags : Python python-utility Python-exceptions Practice Tags : python Similar Reads Python Exception Handling Python Exception Handling handles errors that occur during the execution of a program. Exception handling allows to respond to the error, instead of crashing the running program. It enables you to catch and manage errors, making your code more robust and user-friendly. Let's look at an example:Handl 7 min read How to Break a Function in Python? In Python, breaking a function allows us to exit from loops within the function. With the help of the return statement and the break keyword, we can control the flow of the loop.Using return keywordThis statement immediately terminates a function and optionally returns a value. Once return is execut 2 min read How to Ignore an Exception and Proceed in Python There are a lot of times when in order to prototype fast, we need to ignore a few exceptions to get to the final result and then fix them later. In this article, we will see how we can ignore an exception in Python and proceed. Demonstrate Exception in Python Before seeing the method to solve it, l 3 min read How to handle KeyError Exception in Python In this article, we will learn how to handle KeyError exceptions in Python programming language. What are Exceptions?It is an unwanted event, which occurs during the execution of the program and actually halts the normal flow of execution of the instructions.Exceptions are runtime errors because, th 3 min read How to pass argument to an Exception in Python? There might arise a situation where there is a need for additional information from an exception raised by Python. Python has two types of exceptions namely, Built-In Exceptions and User-Defined Exceptions.Why use Argument in Exceptions? Using arguments for Exceptions in Python is useful for the fol 2 min read Like