Ghi nhật ký là phần quan trọng nhất của bất kỳ ứng dụng hoặc quy trình nào bạn tạo, vì nó giúp bạn gỡ lỗi hoặc theo dõi những gì đang diễn ra. Bản thân việc ghi nhật ký là một chủ đề rất rộng lớn nhưng cực kỳ hữu ích khi bạn muốn thực hiện một số tác dụng phụ hoặc chuyển hướng đầu ra của mình sang một số dịch vụ khác hoặc thực hiện một số tính toán phụ, v.v.
Có thể định cấu hình cao theo thiết kế, chúng tôi có thể thêm các chức năng mở rộng của các trình ghi nhật ký hiện có, sử dụng
import logging
class MailHandler[logging.Handler]:
def emit[self, record: logging.LogRecord] -> None:
if record.exc_info:
exception = "".join[traceback.format_exception[*record.exc_info]]
else:
exception = "".join[traceback.format_exception[*sys.exc_info[]]]
self._send_mail[exception]
3 tùy chỉnhChúng tôi sẽ cố gắng mở rộng chức năng của trình ghi nhật ký hiện có tới
import logging
class MailHandler[logging.Handler]:
def emit[self, record: logging.LogRecord] -> None:
if record.exc_info:
exception = "".join[traceback.format_exception[*record.exc_info]]
else:
exception = "".join[traceback.format_exception[*sys.exc_info[]]]
self._send_mail[exception]
4 trường hợp ngoại lệ xảy ra trong quá trình thực thi mã. Hãy để chúng tôi tạo một import logging
class MailHandler[logging.Handler]:
def emit[self, record: logging.LogRecord] -> None:
if record.exc_info:
exception = "".join[traceback.format_exception[*record.exc_info]]
else:
exception = "".join[traceback.format_exception[*sys.exc_info[]]]
self._send_mail[exception]
5 bằng thư viện tích hợp sẵn import logging
class MailHandler[logging.Handler]:
def emit[self, record: logging.LogRecord] -> None:
if record.exc_info:
exception = "".join[traceback.format_exception[*record.exc_info]]
else:
exception = "".join[traceback.format_exception[*sys.exc_info[]]]
self._send_mail[exception]
6import logging
logger: logging.Logger = logging.getLogger[]
Vào chế độ toàn màn hình Thoát chế độ toàn màn hình
Đoạn mã trên sẽ trả về một phiên bản trình ghi nhật ký
import logging
class MailHandler[logging.Handler]:
def emit[self, record: logging.LogRecord] -> None:
if record.exc_info:
exception = "".join[traceback.format_exception[*record.exc_info]]
else:
exception = "".join[traceback.format_exception[*sys.exc_info[]]]
self._send_mail[exception]
0, vì chúng tôi không đề cập rõ ràng bất kỳ tên nào. Nếu bạn muốn một phiên bản import logging
class MailHandler[logging.Handler]:
def emit[self, record: logging.LogRecord] -> None:
if record.exc_info:
exception = "".join[traceback.format_exception[*record.exc_info]]
else:
exception = "".join[traceback.format_exception[*sys.exc_info[]]]
self._send_mail[exception]
1 được đặt tên, bạn có thể chuyển hàm import logging
class MailHandler[logging.Handler]:
def emit[self, record: logging.LogRecord] -> None:
if record.exc_info:
exception = "".join[traceback.format_exception[*record.exc_info]]
else:
exception = "".join[traceback.format_exception[*sys.exc_info[]]]
self._send_mail[exception]
2 sang hàm import logging
class MailHandler[logging.Handler]:
def emit[self, record: logging.LogRecord] -> None:
if record.exc_info:
exception = "".join[traceback.format_exception[*record.exc_info]]
else:
exception = "".join[traceback.format_exception[*sys.exc_info[]]]
self._send_mail[exception]
3Bây giờ chúng ta hãy tạo một
import logging
class MailHandler[logging.Handler]:
def emit[self, record: logging.LogRecord] -> None:
if record.exc_info:
exception = "".join[traceback.format_exception[*record.exc_info]]
else:
exception = "".join[traceback.format_exception[*sys.exc_info[]]]
self._send_mail[exception]
4 tùy chỉnh để xử lý các bản ghi và thực hiện một số tác dụng phụ. Chúng ta có thể kế thừa từ lớp cơ sở trừu tượng import logging
class MailHandler[logging.Handler]:
def emit[self, record: logging.LogRecord] -> None:
if record.exc_info:
exception = "".join[traceback.format_exception[*record.exc_info]]
else:
exception = "".join[traceback.format_exception[*sys.exc_info[]]]
self._send_mail[exception]
5 để tạo Trình xử lý tùy chỉnh của mìnhLớp cơ sở
import logging
class MailHandler[logging.Handler]:
def emit[self, record: logging.LogRecord] -> None:
if record.exc_info:
exception = "".join[traceback.format_exception[*record.exc_info]]
else:
exception = "".join[traceback.format_exception[*sys.exc_info[]]]
self._send_mail[exception]
5 cung cấp nhiều hook mà bạn có thể ghi đè. Chúng tôi sẽ ghi đè móc import logging
class MailHandler[logging.Handler]:
def emit[self, record: logging.LogRecord] -> None:
if record.exc_info:
exception = "".join[traceback.format_exception[*record.exc_info]]
else:
exception = "".join[traceback.format_exception[*sys.exc_info[]]]
self._send_mail[exception]
7 cho yêu cầu của chúng tôiimport logging
class MailHandler[logging.Handler]:
def emit[self, record: logging.LogRecord] -> None:
if record.exc_info:
exception = "".join[traceback.format_exception[*record.exc_info]]
else:
exception = "".join[traceback.format_exception[*sys.exc_info[]]]
self._send_mail[exception]
Vào chế độ toàn màn hình Thoát chế độ toàn màn hình
Chúng tôi đã thêm tập hợp các câu lệnh để chặn ngoại lệ khỏi bản ghi và tạo định dạng
import logging
class MailHandler[logging.Handler]:
def emit[self, record: logging.LogRecord] -> None:
if record.exc_info:
exception = "".join[traceback.format_exception[*record.exc_info]]
else:
exception = "".join[traceback.format_exception[*sys.exc_info[]]]
self._send_mail[exception]
8Móc
import logging
class MailHandler[logging.Handler]:
def emit[self, record: logging.LogRecord] -> None:
if record.exc_info:
exception = "".join[traceback.format_exception[*record.exc_info]]
else:
exception = "".join[traceback.format_exception[*sys.exc_info[]]]
self._send_mail[exception]
7 sẽ nhận được import logging
class MailHandler[logging.Handler]:
def emit[self, record: logging.LogRecord] -> None:
if record.exc_info:
exception = "".join[traceback.format_exception[*record.exc_info]]
else:
exception = "".join[traceback.format_exception[*sys.exc_info[]]]
self._send_mail[exception]
20 sẽ chứa tất cả các chi tiết liên quan đến bản ghi như tin nhắn, dấu thời gian, số dòng, thông tin ngoại lệ, v.v. Chúng tôi cũng đã thêm một phương thức mẫu import logging
class MailHandler[logging.Handler]:
def emit[self, record: logging.LogRecord] -> None:
if record.exc_info:
exception = "".join[traceback.format_exception[*record.exc_info]]
else:
exception = "".join[traceback.format_exception[*sys.exc_info[]]]
self._send_mail[exception]
21 để gửi dấu vết ngăn xếp được định dạng cho người dùngBây giờ chúng ta hãy hoàn thành chức năng
import logging
class MailHandler[logging.Handler]:
def emit[self, record: logging.LogRecord] -> None:
if record.exc_info:
exception = "".join[traceback.format_exception[*record.exc_info]]
else:
exception = "".join[traceback.format_exception[*sys.exc_info[]]]
self._send_mail[exception]
21. Chúng tôi sẽ sử dụng import logging
class MailHandler[logging.Handler]:
def emit[self, record: logging.LogRecord] -> None:
if record.exc_info:
exception = "".join[traceback.format_exception[*record.exc_info]]
else:
exception = "".join[traceback.format_exception[*sys.exc_info[]]]
self._send_mail[exception]
23 để gửi e-mail. Bạn cũng có thể sử dụng import logging
class MailHandler[logging.Handler]:
def emit[self, record: logging.LogRecord] -> None:
if record.exc_info:
exception = "".join[traceback.format_exception[*record.exc_info]]
else:
exception = "".join[traceback.format_exception[*sys.exc_info[]]]
self._send_mail[exception]
24 để thay thếimport logging
class MailHandler[logging.Handler]:
def emit[self, record: logging.LogRecord] -> None:
if record.exc_info:
exception = "".join[traceback.format_exception[*record.exc_info]]
else:
exception = "".join[traceback.format_exception[*sys.exc_info[]]]
self._send_mail[exception]
2Vào chế độ toàn màn hình Thoát chế độ toàn màn hình
Chúng tôi đang sử dụng thư viện
import logging
class MailHandler[logging.Handler]:
def emit[self, record: logging.LogRecord] -> None:
if record.exc_info:
exception = "".join[traceback.format_exception[*record.exc_info]]
else:
exception = "".join[traceback.format_exception[*sys.exc_info[]]]
self._send_mail[exception]
25 để kết nối với import logging
class MailHandler[logging.Handler]:
def emit[self, record: logging.LogRecord] -> None:
if record.exc_info:
exception = "".join[traceback.format_exception[*record.exc_info]]
else:
exception = "".join[traceback.format_exception[*sys.exc_info[]]]
self._send_mail[exception]
26 và gửi e-mailTôi đang đọc
import logging
class MailHandler[logging.Handler]:
def emit[self, record: logging.LogRecord] -> None:
if record.exc_info:
exception = "".join[traceback.format_exception[*record.exc_info]]
else:
exception = "".join[traceback.format_exception[*sys.exc_info[]]]
self._send_mail[exception]
27 và import logging
class MailHandler[logging.Handler]:
def emit[self, record: logging.LogRecord] -> None:
if record.exc_info:
exception = "".join[traceback.format_exception[*record.exc_info]]
else:
exception = "".join[traceback.format_exception[*sys.exc_info[]]]
self._send_mail[exception]
28 từ các biến môi trường. Chúng sẽ được yêu cầu để gửi e-mail đến địa chỉ đích bằng bản ghi import logging
class MailHandler[logging.Handler]:
def emit[self, record: logging.LogRecord] -> None:
if record.exc_info:
exception = "".join[traceback.format_exception[*record.exc_info]]
else:
exception = "".join[traceback.format_exception[*sys.exc_info[]]]
self._send_mail[exception]
26 được định cấu hình của bạn