首頁 > 後端開發 > Python教學 > Python 中的錯誤處理與日誌記錄

Python 中的錯誤處理與日誌記錄

PHPz
發布: 2024-08-13 06:40:03
原創
800 人瀏覽過

編寫軟體是一項遠非完美的活動。從構思到生產,可能會出現錯誤,在某些情況下,可能會故意發生失敗。這就是為什麼理解主要程式語言中的錯誤處理和日誌記錄是一項需要掌握的關鍵技能。

錯誤可能會發生,情況也可能會出現,但是您的應對方式(包括有關錯誤的準備和資訊)將使您盡快擺脫困境。

在本文中,我們將了解 Python 中的錯誤處理和日誌記錄。我們將主要探討異常以及如何使用Python的logging套件來編寫各種類型的日誌。

如果您對此類主題的更多內容感興趣,請訂閱我的 時事通訊 有關軟體程式設計、架構和技術的定期更新與技術相關的見解。

Python 中的異常

與許多其他程式語言一樣,Python 能夠在發生錯誤時引發異常。在程式設計中,異常是在程式執行期間​​發生的事件,擾亂了正常的指令流。

在Python中,異常是在執行過程中偵測到的錯誤。當異常發生時,Python 會停止執行程式碼並尋找特殊的程式碼區塊(try/ except 區塊)來處理錯誤。

以下是 Python 程式中可能發生的一些常見異常:

  • ZeroDivisionError:嘗試將數字除以零時發生。

  • FileNotFoundError:嘗試開啟不存在的檔案時發生。

  • ValueError:當字串不代表數字時嘗試將字串轉換為整數時發生。

  • IndexError: 嘗試從索引不存在的清單中檢索元素時發生。

還有更多的異常,如果您需要自訂行為,Python 使您能夠創建自己的異常。我們將在本文後面探討這個功能。

要處理Python異常,你需要捕捉它們。捕獲異常需要一種稱為 try/ except 的簡單語法。讓我們來探索一下。

嘗試/除外

try/ except 區塊用於處理異常。可能引發異常的程式碼放在try區塊中,如果發生異常,則執行 except區塊。以下是程式碼區塊中 try/ except 的語法:

try:
    # Code that might raise an exception
    pass
except ExceptionType as e:
    # Code to handle the exception
    pass
登入後複製

可能失敗的程式碼放在 try 區塊內。如果出現問題,程式的執行將進入 except 區塊。

這是一個流程圖,說明了 try/ except 的工作原理:

Error Handling and Logging in Python

讓我們來看看如何用這種方法處理除以零的情況:

# Handling division by zero
try:
    result = 10 / 0
except ZeroDivisionError:
    print("Error: Cannot divide by zero.")
# The code will continue its execution
登入後複製

try/ except 語法還有其他區塊,例如 else 和 finally:

try:
    # Code that might raise an exception
    pass
except ExceptionType as e:
    # Code to handle the exception
    pass
else:
    # Code to run if no exception is raised
    pass
finally:
    # Code that always runs, regardless of whether an exception was raised or not
    pass
登入後複製

這些塊是可選的,但有特定用途:

  • else 區塊(可選):包含在 try 區塊中沒有引發異常時執行的程式碼。它對於只應在 try 區塊成功時運行的程式碼很有用。

  • finally 區塊(可選):包含始終運行的程式碼,無論是否引發異常。這通常用於清理操作,例如關閉檔案或釋放資源。

這是一個範例,我們在出現錯誤時在 finally 中處理文件的關閉:

try:
    # Open the file
    file = open('example.txt', 'r')

    # Read from the file
    content = file.read()

    # Print file content (this will only execute if no exceptions are raised)
    print(content)
except FileNotFoundError as e:
    # Handle the specific exception
    print(f"Error: {e}")
except Exception as e:
    # Handle any other exceptions
    print(f"An unexpected error occurred: {e}")
else:
    # Code that runs if no exception was raised in the try block
    print("File read successfully.")
finally:
    # Ensure the file is closed, regardless of whether an exception was raised
    try:
        file.close()
        print("File closed.")
    except:
        # Handle the case where file was never opened (e.g., if open() failed)
        print("File was not opened or already closed.")
登入後複製

免責聲明: 上面的範例示範了使用 try/ except/finally 進行檔案處理,以確保即使發生錯誤也能正確關閉檔案。然而,這種方法對於日常文件操作來說並不理想。在實務中,建議在Python中使用with語句進行檔案處理。 with 語句會自動管理檔案的開啟和關閉,確保檔案在其套件完成後正確關閉,即使發生異常也是如此。

這就是 try/ except 的工作原理。現在,if/else 可能會有些混淆。什麼時候應該使用 try/ except,什麼時候應該使用 if/else?

try/ except 和 if/else 有什麼差別?當您想要檢查可以在導致錯誤之前預測和處理的條件時,請使用 if/else,並使用 try/ except 來捕獲和管理程式碼執行期間發生的異常,特別是對於您無法輕易預見的錯誤。

在下面的情況下,if/else 將無法正常運作:

filename = 'non_existent_file.txt'

if filename:  # This only checks if filename is not empty, not if the file exists
    # The following line will raise an exception if the file doesn't exist
    content = open(filename, 'r').read()  # This will crash if the file does not exist
    if content:
        print("File content exists:")
        print(content)
    else:
        print("File is empty.")
else:
    print("Filename is invalid.")
登入後複製

這裡有一個更好的 try/ except 解決方案:

filename = 'non_existent_file.txt'

try:
    content = open(filename, 'r').read()
    if content:
        print("File content exists:")
        print(content)
    else:
        print("File is empty.")
except FileNotFoundError:
    print("Error: File not found.")
登入後複製

In the solution above, the code attempts to open and read a file, checking if its content exists and printing it if present. If the file does not exist, it catches the FileNotFoundError and prints an error message, preventing the program from crashing.

As mentioned earlier in the article, Python allows for custom exceptions. Let’s learn more about it.

Creating Custom Exceptions in Python

In Python, you can define your own exceptions to handle specific error conditions in a more granular way. Custom exceptions are particularly useful in complex applications, such as fintech, where you may need to enforce business rules or handle specific error cases uniquely.

For example, in a fintech application, you might have a scenario where a wallet’s balance is checked against certain criteria. You may want to raise an exception if a wallet’s balance is not sufficient or does not conform to specific rules. Here’s how you can create and use a custom exception for this purpose:

# Define a custom exception
class WalletBalanceError(Exception):
    def __init__(self, message):
        self.message = message
        super().__init__(self.message)

# Function that checks wallet balance
def check_wallet_balance(wallet_balance, required_balance):
    if wallet_balance < required_balance:
        # Raise the custom exception with a specific message
        raise WalletBalanceError(f"Insufficient balance: Wallet balance of {wallet_balance} is less than the required {required_balance}.")

# Example usage
try:
    # Example wallet balance and required balance
    wallet_balance = 50
    required_balance = 100

    # Check if the wallet balance is sufficient
    check_wallet_balance(wallet_balance, required_balance)
except WalletBalanceError as e:
    # Handle the custom exception
    print(f"Error: {e}")
登入後複製

In this example, we define a custom exception WalletBalanceError to handle cases where a wallet’s balance does not meet the required criteria. The check_wallet_balance function raises this exception if the wallet’s balance is insufficient, providing a clear and specific error message.

Custom exceptions in Python help make the code more readable and maintainable by clearly defining specific error conditions and handling them in a structured manner.

Now that we know how to handle errors in Python, it’s time to understand what to do when these errors occur. There are many strategies, but keeping a log of these errors can help identify issues later and correct them. In the next section of this article, we will explore logging.

Logging in Python

Logging helps developers track errors, events, or any runtime information in an application or program. Logging is an important and crucial aspect of software engineering as it has the ability to record everything that goes right or wrong in a post-development application. Logging is one of the most important pillars of monitoring.

Python provides a built-in module that can be used for logging

purposes. To use this module, the first thing to do is to import it.

import logging
登入後複製

Then, configure the logger using the basicConfig method. You need to pass parameters to it, such as the log level, the format of the message, and the output file to save the log.

import logging

# Set up the basic configuration for logging
logging.basicConfig(filename='app.log', level=logging.DEBUG,
                    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')

# Log messages of various severity levels
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')
登入後複製

In the example above, logs will be written to a file called app.log. The log message format includes the timestamp, logger name, log level, and the actual message.

Python logging has different log levels that indicate the severity of an event or message. These log levels allow you to categorize and filter messages based on their importance. Here’s a breakdown of the common log levels in Python:

Log Levels

  1. DEBUG: Detailed information, typically of interest only when diagnosing problems. Used for debugging purposes during development.

  2. INFO: Confirmation that things are working as expected. This is the level you would use for normal operations and informational messages.

  3. WARNING: An indication that something unexpected happened, or indicative of some problem in the near future (e.g., "disk space low"). The software is still working as expected.

  4. ERROR: Due to a more serious problem, the software has not been able to perform some function. An error indicates a significant issue that needs attention.

  5. CRITICAL: A very serious error, indicating that the program itself may be unable to continue running. Critical errors often represent severe problems that require immediate action.

The logging module allows you to control which messages are recorded by setting the logging level. Only messages that are equal to or more severe than the set level will be logged. The default level is WARNING, meaning only WARNING, ERROR, and CRITICAL messages are logged unless you change the logging configuration.

In the code example above, we set the logging level to DEBUG, which means all log messages (DEBUG, INFO, WARNING, ERROR, and CRITICAL) will be recorded in the app.log file.

You can also create custom loggers, which give you more control over how messages are logged. Custom loggers allow you to set up multiple loggers with different configurations, such as different log levels, formats, or output destinations. This is particularly useful in larger applications where you need to separate logs for different modules or components.

Here’s how you can create and use a custom logger:

import logging

# Create a custom logger
logger = logging.getLogger('my_custom_logger')

# Set the log level for the custom logger
logger.setLevel(logging.DEBUG)

# Create a file handler to write logs to a file
file_handler = logging.FileHandler('custom.log')

# Create a console handler to output logs to the console
console_handler = logging.StreamHandler()

# Set log levels for the handlers
file_handler.setLevel(logging.ERROR)
console_handler.setLevel(logging.DEBUG)

# Create a formatter for log messages
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

# Add the formatter to the handlers
file_handler.setFormatter(formatter)
console_handler.setFormatter(formatter)

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

# Log messages using the custom logger
logger.debug('This is a debug message')
logger.info('This is an info message')
logger.warning('This is a warning message')
logger.error('This is an error message')
logger.critical('This is a critical message')
登入後複製

In this example, we create a custom logger named my_custom_logger. This logger writes ERROR and more severe messages to a file called custom.log, while DEBUG and more severe messages are output to the console. By customizing the loggers, you can tailor the logging behavior to fit the specific needs of your application.

Real-world Example: Logging in a Web Application

In a web application, logging plays a critical role in monitoring and maintaining the system’s health. For example, in a Flask web application, you might use logging to track incoming requests, errors, and performance metrics.

Here’s a basic example of how you can use logging in a Flask application:

from flask import Flask, request
import logging

app = Flask(__name__)

# Set up the basic configuration for logging
logging.basicConfig(filename='webapp.log', level=logging.INFO,
                    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')

@app.route('/')
def index():
    app.logger.info('Index page accessed')
    return 'Welcome to the Flask Web Application!'

@app.route('/error')
def error():
    app.logger.error('Error page accessed')
    raise ValueError('This is a simulated error')

if __name__ == '__main__':
    app.run(debug=True)
登入後複製

In this Flask application, we configure logging to write logs to a file named webapp.log. Each time the index page is accessed, an informational log message is recorded. If the error page is accessed, an error log message is recorded, and a simulated error is raised.

By implementing logging in your web application, you can gain insights into user activity, system errors, and performance issues. This information is invaluable for debugging, troubleshooting, and optimizing the application.

Conclusion

Error handling and logging are essential aspects of software development, ensuring that applications run smoothly and that any issues are quickly identified and resolved.

In this article, we explored exceptions in Python, including how to handle them using try/except, and the importance of logging for tracking errors and events. We also discussed how to create custom exceptions and custom loggers to suit specific application needs.

By mastering error handling and logging, you’ll be better equipped to build robust and maintainable software that can gracefully handle unexpected situations and provide valuable insights into its operation.

If you enjoyed this article, consider subscribing to my newsletter so you don't miss out on future updates.

Your feedback is valuable! If you have any suggestions, critiques, or questions, please leave a comment below.

以上是Python 中的錯誤處理與日誌記錄的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:dev.to
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板