Practical discussion of how Python handles exceptions and logs

This article is shared from Huawei Cloud Community " Python Exception Handling and Logging to Build Robust and Reliable Applications " by Lemony Hug.

Exception handling and logging are vital components in writing reliable and easy-to-maintain software applications. Python provides a powerful exception handling mechanism and flexible logging functions, making it easier for developers to manage errors in the code and track the execution process of the application. In this article, we'll explore best practices for exception handling and logging in Python, as well as some case code to illustrate these concepts.

The importance of exception handling

Exception handling refers to the process of handling errors or abnormal situations that may occur during program execution. Good exception handling can help us:

  • Improve program stability: By catching and handling exceptions, we can avoid unexpected program crashes and improve application stability.
  • Improve user experience: When an error occurs in the program, friendly error prompts and handling can improve the user experience and avoid users' bad impression of the program.
  • Easier debugging and maintenance: Good exception handling can help us locate and solve problems in the program more easily and improve the maintainability of the code.

Exception handling in Python

In Python, exception handling is implemented through the try-except statement. Here is a simple exception handling example:

try:
    # Attempt to execute code that may throw an exception
    result = 10 / 0
except ZeroDivisionError:
    # Handle specific types of exceptions
    print("A divide-by-zero error occurred!")

In this example, we try to calculate 10 divided by 0, which raises a ZeroDivisionError exception. Then we use the except clause to catch this exception and output the error message. In addition to catching specific types of exceptions, we can also use exceptthe clause to catch all types of exceptions for generic error handling.

In addition to catching exceptions, we can also use elseclauses to execute specific code when no exception occurs in the try block, and finallyclauses to execute specific cleanup code regardless of whether an exception occurs.

The importance of logging

Logging is a technique that records important information during the execution of an application. Good logging helps us:

  • Track the execution process of the application: By recording key events and status information, we can track the execution process of the application and help us understand the behavior of the program.
  • Diagnosis and debugging: When a problem occurs in the program, logging can provide useful debugging information to help us quickly locate and solve the problem.
  • Monitoring and analysis: By analyzing log data, we can understand the performance and usage of the application, helping us optimize and improve the program.

Logging in Python

Modules in the Python standard library loggingprovide powerful and flexible logging capabilities. We can use this module to create a logger, set the log level, define the log format, etc. Here is a simple logging example:

import logging

# Create logger
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

#Create file handler
file_handler = logging.FileHandler('app.log')
file_handler.setLevel(logging.INFO)

#Create log format
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
file_handler.setFormatter(formatter)

# Add handler to logger
logger.addHandler(file_handler)

# Record log information
logger.info('This is an information log')
logger.warning('This is a warning log')
logger.error('This is an error log')

In this example, we first create a logger loggerand set the log level to INFO. We then created a file handler file_handler, set its level to INFO as well, and defined the log format. Finally, we add the file processor to the logger and use methods such as logger.info(), logger.warning()and logger.error()to record different levels of log information.

Best practice examples

Here is an example of best practices combining exception handling and logging:

import logging

# Create logger
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

#Create file handler
file_handler = logging.FileHandler('app.log')
file_handler.setLevel(logging.INFO)

#Create log format
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
file_handler.setFormatter(formatter)

# Add handler to logger
logger.addHandler(file_handler)

def divide(x, y):
    try:
        result = x / y
    except ZeroDivisionError as e:
        logger.error(f"divide by zero error: {e}")
    except Exception as e:
        logger.error(f"Exception occurred: {e}")
    else:
        logger.info(f"Result: {result}")
    finally:
        logger.info("Operation ended")

# Test function
divide(10, 2)
divide(10, 0)

In this example, we define a divide()function called which calculates the quotient of two numbers. Inside the function, we use a try-except statement to catch possible divide-by-zero errors and use a logger to record exception information. At the end of function execution, we use finallya clause to record information about the end of the operation.

To better understand how to use Python for exception handling and logging, and to apply these best practices in real projects. In actual development, in addition to basic exception handling and logging, more complex configuration and optimization can also be performed based on the characteristics and needs of the project, such as:

  • Use custom exception classes : In addition to Python's built-in exception types, we can also define our own exception classes to better organize and manage exception information.
  • Flexible use of log levels : According to different parts and needs of the application, the level of the logger can be flexibly adjusted for debugging and monitoring in different environments.
  • Hierarchical recording of logs : In addition to using different levels of logging methods, logs can also be recorded to different files or data sources based on the importance and type of log messages for subsequent analysis and processing.
  • Integrate third-party log services : For large projects or distributed systems, you can consider integrating third-party log services (such as ELK Stack, Splunk, etc.) to achieve more advanced log management and monitoring functions.

To sum up, exception handling and logging are indispensable and important components in Python application development. By making reasonable use of the exception handling mechanism and logging functions provided by Python, and flexibly configuring and optimizing according to the actual situation of the project, we can write more robust and reliable software applications, improve user experience, and reduce the cost of fault occurrence and processing. , providing strong support for the successful delivery and operation and maintenance of the project.

In real projects, here are some additional tips and best practices that can further improve the efficiency and maintainability of exception handling and logging:

Using Context Managers

The context manager is an elegant resource management tool in Python that ensures the correct allocation and release of resources. By combining context managers and exception handling, we can better manage resources and avoid resource leaks and unexpected errors. For example, you can use withstatements to manage file operations:

try:
    with open('file.txt', 'r') as f:
        content = f.read()
except FileNotFoundError:
    logger.error('File does not exist')
except Exception as e:
    logger.error(f'Exception occurred: {e}')

Using Decorators

Decorators are a powerful feature in Python that can be used to add additional logic before and after function execution. By customizing decorators, we can implement unified exception handling and logging logic and avoid repeatedly writing similar code in each function. For example, you can write a decorator to record function execution time and exception information:

import time

def log_exceptions(func):
    def wrapper(*args, **kwargs):
        try:
            start_time = time.time()
            result = func(*args, **kwargs)
            end_time = time.time()
            logger.info(f"{func.__name__} execution time: {end_time - start_time} seconds")
            return result
        except Exception as e:
            logger.error(f"Exception occurred in function {func.__name__}: {e}")
    return wrapper

@log_exceptions
def some_function():
    # Function logic
    pass

Combined with Error Codes

In complex applications, error codes can be used to identify different types of errors to better organize and manage exception information. By defining a set of error codes and corresponding error messages, you can make your code more readable and maintainable. For example:

ERROR_CODE_DIVIDE_BY_ZERO = 1001
ERROR_CODE_FILE_NOT_FOUND = 1002

def divide(x, y):
    try:
        result = x / y
    except ZeroDivisionError:
        logger.error(f"divide by zero error: {e}", extra={'error_code': ERROR_CODE_DIVIDE_BY_ZERO})
    except FileNotFoundError:
        logger.error(f"File not found: {e}", extra={'error_code': ERROR_CODE_FILE_NOT_FOUND})

Use third-party logging library

In addition to the logging module in the Python standard library, there are many excellent third-party logging libraries to choose from, such as Loguru, structlog, etc. These libraries provide richer functions and more friendly APIs, and you can choose the appropriate library for logging according to actual needs.

Define clear log level policies

When designing a logging system, a clear log level policy should be defined to ensure the accuracy and readability of log information. Generally, different log levels can be defined based on the importance and urgency of the log message, such as DEBUG, INFO, WARNING, ERROR, and CRITICAL. In daily development, appropriate log levels should be used according to specific situations to ensure that log information is neither too verbose nor critical information is lost.

Consider internationalization and localization needs

For applications targeting global users, internationalization and localization requirements should be considered, and standard internationalized text and formatting methods should be used in log records to ensure the readability and consistency of log information in different language environments. At the same time, time formats and habits of different time zones and regions should also be considered to better understand and analyze log information.

Implement log rotation and archiving

In long-running applications, log files can grow continuously and take up large amounts of disk space. To avoid this situation, you can implement log rotation and archiving functions, and regularly clean and compress old log files to save storage space and ensure the accessibility of log information. You can use third-party libraries in Python (such as LogRotate) to implement log rotation and archiving functions, or implement it yourself according to project needs.

Integrate monitoring and alarm systems

In a production environment, it is crucial to detect and handle exceptions in a timely manner. Therefore, monitoring and alerting systems can be combined to achieve real-time monitoring and alerting of log information. By adding keywords and identifiers to log records and setting up a monitoring system to monitor them, abnormal situations can be discovered in time and corresponding measures can be taken to ensure the stable operation of the application.

Make continuous improvements and optimizations

Exception handling and logging is a continuous improvement process, and existing exception handling and logging strategies should be regularly reviewed and optimized to adapt to project development and changes. Log records can be analyzed and counted regularly to discover potential problems and optimization space, and the processes and mechanisms for exception handling and logging can be adjusted and improved in a timely manner to improve the stability and maintainability of the application.

Through the above tips and best practices, we can better apply Python for exception handling and logging, and build robust and reliable software applications in actual projects. Exception handling and logging are important links in the software development process. They can not only help us find and solve problems, but also improve the maintainability and readability of the code, providing strong support for the successful delivery and operation of the project.

Summarize

Exception handling and logging are essential and critical components in Python application development. Through the introduction and detailed discussion of this article, we delve into the best practices of using Python for exception handling and logging, and provide a wealth of case codes and techniques to help developers better understand and apply these important concepts.

In terms of exception handling, we learned how to use the try-except statement to catch and handle exceptions that may occur, and discussed how to use the else clause and the finally clause to perform related cleanup work. We also explored how to combine advanced techniques such as context managers and decorators to further improve the efficiency and maintainability of exception handling.

In terms of logging, we took an in-depth look at the logging module in the Python standard library and learned how to create a logger, set the log level, and define the log format and other basic operations. In addition, we also discussed how to use different log levels and logging methods according to project requirements, and how to combine technologies such as error codes and third-party log libraries to achieve more flexible and efficient logging functions.

In addition to basic exception handling and logging, we also explore a series of advanced tips and best practices, such as defining clear log level policies, considering internationalization and localization needs, implementing log rotation and archiving, and combining monitoring and alerting system etc. These skills and practices can help developers better cope with complex project requirements and actual situations, and improve the quality and maintainability of code.

In short, by properly applying the best practices of exception handling and logging, we can write robust and reliable Python applications, improve user experience, reduce failure occurrence and processing costs, and provide strong support for the successful delivery and operation of projects. . In future development work, we should continue to pay attention to and continuously optimize exception handling and logging to ensure the stability and maintainability of the application and provide users with better services and experiences.

Click to follow and learn about Huawei Cloud’s new technologies as soon as possible~

 

I decided to give up on open source Hongmeng. Wang Chenglu, the father of open source Hongmeng: Open source Hongmeng is the only architectural innovation industrial software event in the field of basic software in China - OGG 1.0 is released, Huawei contributes all source code Google Reader is killed by the "code shit mountain" Fedora Linux 40 is officially released Former Microsoft developer: Windows 11 performance is "ridiculously bad" Ma Huateng and Zhou Hongyi shake hands to "eliminate grudges" Well-known game companies have issued new regulations: employee wedding gifts must not exceed 100,000 yuan Ubuntu 24.04 LTS officially released Pinduoduo was sentenced for unfair competition Compensation of 5 million yuan
{{o.name}}
{{m.name}}

Guess you like

Origin my.oschina.net/u/4526289/blog/11054810
Recommended