ソフトウェアの作成は、完璧とは程遠い作業です。構想から制作までの間にエラーが発生する可能性があり、場合によっては意図的に失敗が発生することもあります。このため、主なプログラミング言語でのエラー処理とログ記録を理解することが、習得すべき重要なスキルです。
エラーが発生したり、状況が発生したりする可能性がありますが、準備とエラーに関する情報を用意してどのように対応するかによって、できるだけ早くその状況から抜け出すことができます。
この記事では、Python でのエラー処理とログ記録について学びます。ここでは主に、例外と、Python のログ パッケージを使用してさまざまな種類のログを書き込む方法について説明します。
このようなトピックをカバーするコンテンツに興味がある場合は、ソフトウェア プログラミング、アーキテクチャ、テクノロジー関連の洞察。 Python の例外 他の多くのプログラミング言語と同様、Python にはエラーが発生したときに例外を発生させる機能があります。プログラミングにおける例外とは、プログラムの実行中に発生し、通常の命令の流れを中断するイベントです。 Python では、例外は実行中に検出されるエラーです。例外が発生すると、Python はコードの実行を停止し、エラーを処理するための特別なコード ブロック (try/Except ブロック) を探します。
ZeroDivisionError
: 数値をゼロで除算しようとすると発生します。
FileNotFoundError: 存在しないファイルを開こうとすると発生します。
ValueError: 文字列が数値を表さない場合に、文字列を整数に変換しようとすると発生します。
IndexError: インデックスが存在しないリストから要素を取得しようとすると発生します。
さらに多くの例外があり、カスタム動作が必要な場合、Python では独自の例外を作成できます。これは、この記事の後半で説明する機能です。 Python 例外を処理するには、例外をキャッチする必要があります。例外をキャッチするには、try/excel として知られる単純な構文が必要です。これについて調べてみましょう。
try/excel ブロックは例外を処理するために使用されます。例外を発生させる可能性のあるコードは try ブロックに配置され、例外が発生した場合は、Except ブロックが実行されます。コード ブロック内の try/excel の構文は次のとおりです。
これは、try/Except がどのように機能するかを示すフローチャートです。
try: # Code that might raise an exception pass except ExceptionType as e: # Code to handle the exception pass
このアプローチでゼロ除算をどのように処理できるかを見てみましょう:
try/Except 構文には、else やfinally などの追加のブロックもあります。
# Handling division by zero try: result = 10 / 0 except ZeroDivisionError: print("Error: Cannot divide by zero.") # The code will continue its execution
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
免責事項:
上記の例は、エラーが発生した場合でもファイルが適切に閉じられるように、try/excel/finally を使用したファイル処理を示しています。ただし、このアプローチは日常的なファイル操作には理想的ではありません。実際には、Python でのファイル処理には with ステートメントを使用することをお勧めします。 with ステートメントは、ファイルのオープンとクローズを自動的に管理し、例外が発生した場合でも、スイートの終了後にファイルが適切に閉じられるようにします。
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/excel の仕組みです。ここで、if/else に関して混乱が生じる可能性があります。 Try/Except をいつ使用する必要がありますか?try/excel と if/else の違いは何ですか?エラーが発生する前に予測して処理できる条件を確認する場合は if/else を使用し、コード実行中に発生する例外、特に簡単に予測できないエラーをキャッチして管理するには try/excel を使用します。
以下の場合、if/else は正しく動作しません:
ここでは try/excel を使用したより良い解決策を示します:
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.
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 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:
DEBUG: Detailed information, typically of interest only when diagnosing problems. Used for debugging purposes during development.
INFO: Confirmation that things are working as expected. This is the level you would use for normal operations and informational messages.
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.
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.
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.
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.
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 中国語 Web サイトの他の関連記事を参照してください。