Một trong những hàm được sử dụng nhiều nhất trong ngôn ngữ lập trình Python là câu lệnh
import logging >>> logging.debug["debug message"]0; . Về chức năng, không có bất cứ thứ gì đến gần. Nhưng nó không nên được sử dụng cho tất cả các trường hợp như khi bạn gỡ lỗi — đặc biệt nếu bạn đang làm việc trên một chương trình phức tạp
>>> logging.info["info message"]
>>> logging.warning["warning message"]
WARNING:root:This is a warning message
>>> logging.error["error message"]
ERROR:root:error message
>>> logging.critical["critical message"]
CRITICAL:root:critical message
Bạn có thể bỏ qua việc sử dụng các câu lệnh
import logging >>> logging.debug["debug message"]0 trong một chương trình đơn giản, nhưng cá nhân tôi không khuyên bạn nên làm như vậy. Bạn nên tập thói quen sử dụng nhật ký để gỡ lỗi mã của mình. Ghi nhật ký là một trong những công cụ hữu ích nhất trong hộp công cụ của lập trình viên. Nó được sử dụng như một công cụ hỗ trợ để hiểu rõ hơn về quy trình của chương trình và xác định các tình huống có thể chưa được xem xét trong khi chương trình đang được phát triển
>>> logging.info["info message"]
>>> logging.warning["warning message"]
WARNING:root:This is a warning message
>>> logging.error["error message"]
ERROR:root:error message
>>> logging.critical["critical message"]
CRITICAL:root:critical message
Python cung cấp cho chúng tôi một mô-đun ghi nhật ký tích hợp có tên là
import logging >>> logging.debug["debug message"]1. Trong bài viết này, chúng tôi sẽ đề cập đến các khái niệm quan trọng cho phép bạn bắt đầu thêm
>>> logging.info["info message"]
>>> logging.warning["warning message"]
WARNING:root:This is a warning message
>>> logging.error["error message"]
ERROR:root:error message
>>> logging.critical["critical message"]
CRITICAL:root:critical message
import logging >>> logging.debug["debug message"]1 vào ứng dụng của mình
>>> logging.info["info message"]
>>> logging.warning["warning message"]
WARNING:root:This is a warning message
>>> logging.error["error message"]
ERROR:root:error message
>>> logging.critical["critical message"]
CRITICAL:root:critical message
Table of contents:
--> A simple introduction to Python's logging module
--> Configuring the logger
--> Creating your own logger object
--> Understanding when to use logging
Giới thiệu đơn giản về mô-đun ghi nhật ký của PythonNhật ký là tài liệu được tạo tự động, được đánh dấu thời gian về các sự kiện có liên quan đến chương trình. Để tạo ra thông tin như vậy, bộ ghi nhật ký liên tục theo dõi luồng ứng dụng và lưu trữ thông tin cần thiết [i. e. ai đã truy cập chương trình và vào thời điểm nào]
Trong Python, bạn có thể sử dụng mô-đun
import logging >>> logging.debug["debug message"]1 để tạo nhật ký. Đó là một Python tích hợp sẵn phổ biến được hầu hết các thư viện bên thứ ba sử dụng, có nghĩa là bạn có thể tích hợp thông điệp tường trình của mình với thông điệp tường trình của các thư viện bạn đang sử dụng, nhưng đây không phải là điều thú vị duy nhất về nó
>>> logging.info["info message"]
>>> logging.warning["warning message"]
WARNING:root:This is a warning message
>>> logging.error["error message"]
ERROR:root:error message
>>> logging.critical["critical message"]
CRITICAL:root:critical message
Một tính năng thú vị khác của mô-đun
import logging >>> logging.debug["debug message"]1 là tập hợp các chức năng mà nó cung cấp để làm cho quá trình ghi nhật ký trở nên đơn giản. Mỗi chức năng ghi nhật ký được đặt tên theo mức độ nghiêm trọng của sự kiện — điều này cho phép ghi lại các sự kiện khẩn cấp nhất trong chương trình của bạn
>>> logging.info["info message"]
>>> logging.warning["warning message"]
WARNING:root:This is a warning message
>>> logging.error["error message"]
ERROR:root:error message
>>> logging.critical["critical message"]
CRITICAL:root:critical message
Các cấp độ được xác định, theo thứ tự mức độ nghiêm trọng tăng dần, như sau
- GỠ LỖI. Được sử dụng để ghi lại các chi tiết cấp thấp liên quan đến mã nguồn, ví dụ, thông báo cho các nhà phát triển rằng một chức năng đã được gọi hoặc giá trị của một đối tượng được lưu trữ trong một biến
- THÔNG TIN. Được sử dụng cho mục đích thông tin chung liên quan đến những thứ xảy ra trong chương trình, chẳng hạn như tệp đang được đọc hoặc email đang được gửi
- CẢNH BÁO. Được sử dụng như một cảnh báo để thông báo cho bạn biết rằng chưa có lỗi nhưng có thể có lỗi trong tương lai
- LỖI. Được sử dụng cho các lỗi có thể ngăn ngừa được
- PHÊ BÌNH. Dùng cho các lỗi khiến hệ thống bị lỗi hoặc không thể khôi phục
Nói cách khác, không phải tất cả các thông điệp tường trình đều được tạo như nhau
Hãy xem bộ ghi mặc định đi kèm với mô-đun
import logging >>> logging.debug["debug message"]1
>>> logging.info["info message"]
>>> logging.warning["warning message"]
WARNING:root:This is a warning message
>>> logging.error["error message"]
ERROR:root:error message
>>> logging.critical["critical message"]
CRITICAL:root:critical message
import logging >>> logging.debug["debug message"]
>>> logging.info["info message"]
>>> logging.warning["warning message"]
WARNING:root:This is a warning message
>>> logging.error["error message"]
ERROR:root:error message
>>> logging.critical["critical message"]
CRITICAL:root:critical message
Bạn có thể nhận thấy hai điều
- Các hàm
import logging >>> logging.debug["debug message"]
6 và
>>> logging.info["info message"]
>>> logging.warning["warning message"]
WARNING:root:This is a warning message
>>> logging.error["error message"]
ERROR:root:error message
>>> logging.critical["critical message"]
CRITICAL:root:critical messageimport logging >>> logging.debug["debug message"]
7 không được ghi lại. Điều này là do cấu hình mặc định chỉ ghi nhật ký các thông báo có mức độ nghiêm trọng từ
>>> logging.info["info message"]
>>> logging.warning["warning message"]
WARNING:root:This is a warning message
>>> logging.error["error message"]
ERROR:root:error message
>>> logging.critical["critical message"]
CRITICAL:root:critical messageimport logging >>> logging.debug["debug message"]
8 trở lên
>>> logging.info["info message"]
>>> logging.warning["warning message"]
WARNING:root:This is a warning message
>>> logging.error["error message"]
ERROR:root:error message
>>> logging.critical["critical message"]
CRITICAL:root:critical message - Định dạng đầu ra của các tin nhắn đã ghi là
import logging >>> logging.debug["debug message"]
9
>>> logging.info["info message"]
>>> logging.warning["warning message"]
WARNING:root:This is a warning message
>>> logging.error["error message"]
ERROR:root:error message
>>> logging.critical["critical message"]
CRITICAL:root:critical message
Chúng tôi có thể định cấu hình thông báo nhật ký của mình như bạn sẽ thấy trong phần tiếp theo
Định cấu hình bộ ghiTrong phần trên, chúng tôi đã đề cập đến mô-đun ghi nhật ký với các cài đặt mặc định nhưng đôi khi các giá trị mặc định không như chúng tôi muốn. Trong những tình huống như vậy, chúng tôi có thể thiết lập trình ghi nhật ký của mình bằng cách định cấu hình nó với
import logging >>> logging.debug["debug message"]10
>>> logging.info["info message"]
>>> logging.warning["warning message"]
WARNING:root:This is a warning message
>>> logging.error["error message"]
ERROR:root:error message
>>> logging.critical["critical message"]
CRITICAL:root:critical message
Các tham số được sử dụng phổ biến nhất của
import logging >>> logging.debug["debug message"]10 bao gồm
>>> logging.info["info message"]
>>> logging.warning["warning message"]
WARNING:root:This is a warning message
>>> logging.error["error message"]
ERROR:root:error message
>>> logging.critical["critical message"]
CRITICAL:root:critical message
import logging >>> logging.debug["debug message"]
12— Chỉ định một
>>> logging.info["info message"]
>>> logging.warning["warning message"]
WARNING:root:This is a warning message
>>> logging.error["error message"]
ERROR:root:error message
>>> logging.critical["critical message"]
CRITICAL:root:critical messageimport logging >>> logging.debug["debug message"]
13 được tạo bằng cách sử dụng
>>> logging.info["info message"]
>>> logging.warning["warning message"]
WARNING:root:This is a warning message
>>> logging.error["error message"]
ERROR:root:error message
>>> logging.critical["critical message"]
CRITICAL:root:critical messageimport logging >>> logging.debug["debug message"]
12 đã chỉ định thay vì một
>>> logging.info["info message"]
>>> logging.warning["warning message"]
WARNING:root:This is a warning message
>>> logging.error["error message"]
ERROR:root:error message
>>> logging.critical["critical message"]
CRITICAL:root:critical messageimport logging >>> logging.debug["debug message"]
15;
>>> logging.info["info message"]
>>> logging.warning["warning message"]
WARNING:root:This is a warning message
>>> logging.error["error message"]
ERROR:root:error message
>>> logging.critical["critical message"]
CRITICAL:root:critical messageimport logging >>> logging.debug["debug message"]
17 — Nếu một
>>> logging.info["info message"]
>>> logging.warning["warning message"]
WARNING:root:This is a warning message
>>> logging.error["error message"]
ERROR:root:error message
>>> logging.critical["critical message"]
CRITICAL:root:critical messageimport logging >>> logging.debug["debug message"]
12 được chỉ định thì tệp sẽ được mở trong
>>> logging.info["info message"]
>>> logging.warning["warning message"]
WARNING:root:This is a warning message
>>> logging.error["error message"]
ERROR:root:error message
>>> logging.critical["critical message"]
CRITICAL:root:critical messageimport logging >>> logging.debug["debug message"]
17 đã cho. Chế độ mặc định là 'a'. tìm hiểu thêm về các chế độ khác nhau
>>> logging.info["info message"]
>>> logging.warning["warning message"]
WARNING:root:This is a warning message
>>> logging.error["error message"]
ERROR:root:error message
>>> logging.critical["critical message"]
CRITICAL:root:critical messageTable of contents:
10 — Chuỗi định dạng đã chỉ định sẽ được sử dụng cho trình xử lý;
--> A simple introduction to Python's logging module
--> Configuring the logger
--> Creating your own logger object
--> Understanding when to use loggingTable of contents:
12 — Mức độ nghiêm trọng được chỉ định mà trình ghi nhật ký gốc phải được đặt thành
--> A simple introduction to Python's logging module
--> Configuring the logger
--> Creating your own logger object
--> Understanding when to use logging
Hãy khám phá thêm các thông số này
Chúng tôi có thể đặt mức độ nghiêm trọng của thông điệp tường trình của mình bằng cách sử dụng tham số
Table of contents:
--> A simple introduction to Python's logging module
--> Configuring the logger
--> Creating your own logger object
--> Understanding when to use logging
12import logging >>> logging.debug["debug message"]1
>>> logging.info["info message"]
>>> logging.warning["warning message"]
WARNING:root:This is a warning message
>>> logging.error["error message"]
ERROR:root:error message
>>> logging.critical["critical message"]
CRITICAL:root:critical message
Nếu chúng tôi muốn gửi thông điệp tường trình của mình tới một tệp thay vì bảng điều khiển, chúng tôi phải chuyển đối số
import logging >>> logging.debug["debug message"]12 tới
>>> logging.info["info message"]
>>> logging.warning["warning message"]
WARNING:root:This is a warning message
>>> logging.error["error message"]
ERROR:root:error message
>>> logging.critical["critical message"]
CRITICAL:root:critical message
import logging >>> logging.debug["debug message"]10 của mình và cung cấp tên mà chúng tôi muốn lưu tệp dưới
>>> logging.info["info message"]
>>> logging.warning["warning message"]
WARNING:root:This is a warning message
>>> logging.error["error message"]
ERROR:root:error message
>>> logging.critical["critical message"]
CRITICAL:root:critical message
Ghi chú.
Table of contents:
--> A simple introduction to Python's logging module
--> Configuring the logger
--> Creating your own logger object
--> Understanding when to use logging
16 chỉ có thể được gọi nếu bộ ghi gốc chưa được định cấu hình — nó chỉ có thể được gọi một lần. Vì vậy, hãy tưởng tượng một vài đoạn mã tiếp theo đang cập nhật tập lệnh ở trên thay vì mở rộng nóTable of contents:
--> A simple introduction to Python's logging module
--> Configuring the logger
--> Creating your own logger object
--> Understanding when to use logging
1Việc thực hiện cập nhật này cho mã của chúng tôi giờ đây sẽ nối tất cả các thông báo nhật ký tiếp theo vào tệp có tên
Table of contents:
--> A simple introduction to Python's logging module
--> Configuring the logger
--> Creating your own logger object
--> Understanding when to use logging
17 trong thư mục làm việc hiện tại của bạn — điều này có nghĩa là nếu tệp đã tồn tại thì trình ghi nhật ký sẽ chỉ cần thêm các nhật ký mới của bạn vào cuối tệpBạn có thể thay đổi hành vi này bằng cách đặt
import logging >>> logging.debug["debug message"]17. Theo mặc định, tham số
>>> logging.info["info message"]
>>> logging.warning["warning message"]
WARNING:root:This is a warning message
>>> logging.error["error message"]
ERROR:root:error message
>>> logging.critical["critical message"]
CRITICAL:root:critical message
import logging >>> logging.debug["debug message"]17 được đặt thành
>>> logging.info["info message"]
>>> logging.warning["warning message"]
WARNING:root:This is a warning message
>>> logging.error["error message"]
ERROR:root:error message
>>> logging.critical["critical message"]
CRITICAL:root:critical message
import logging >>> logging.debug["debug message"]30, có nghĩa là tệp “được mở để ghi, nối thêm vào cuối tệp nếu nó tồn tại” [Nguồn. Tài liệu ghi nhật ký]. Hãy thay đổi
>>> logging.info["info message"]
>>> logging.warning["warning message"]
WARNING:root:This is a warning message
>>> logging.error["error message"]
ERROR:root:error message
>>> logging.critical["critical message"]
CRITICAL:root:critical message
import logging >>> logging.debug["debug message"]17 thành
>>> logging.info["info message"]
>>> logging.warning["warning message"]
WARNING:root:This is a warning message
>>> logging.error["error message"]
ERROR:root:error message
>>> logging.critical["critical message"]
CRITICAL:root:critical message
import logging >>> logging.debug["debug message"]32. “mở để viết, trước tiên hãy cắt bớt tệp. ”
>>> logging.info["info message"]
>>> logging.warning["warning message"]
WARNING:root:This is a warning message
>>> logging.error["error message"]
ERROR:root:error message
>>> logging.critical["critical message"]
CRITICAL:root:critical message
import logging >>> logging.debug["debug message"]3
>>> logging.info["info message"]
>>> logging.warning["warning message"]
WARNING:root:This is a warning message
>>> logging.error["error message"]
ERROR:root:error message
>>> logging.critical["critical message"]
CRITICAL:root:critical message
Mỗi khi chương trình được chạy, tệp nhật ký sẽ được viết lại do thay đổi chúng tôi đã thực hiện đối với
import logging >>> logging.debug["debug message"]17
>>> logging.info["info message"]
>>> logging.warning["warning message"]
WARNING:root:This is a warning message
>>> logging.error["error message"]
ERROR:root:error message
>>> logging.critical["critical message"]
CRITICAL:root:critical message
Hiện tại, thông điệp bản ghi đang được lưu trữ là từ định dạng mặc định —
Table of contents:
--> A simple introduction to Python's logging module
--> Configuring the logger
--> Creating your own logger object
--> Understanding when to use logging
11. Chúng tôi có thể thay đổi điều này bằng cách đặt tham số Table of contents:
--> A simple introduction to Python's logging module
--> Configuring the logger
--> Creating your own logger object
--> Understanding when to use logging
10 trong cấu hình mặc định của chúng tôiimport logging >>> logging.debug["debug message"]0
>>> logging.info["info message"]
>>> logging.warning["warning message"]
WARNING:root:This is a warning message
>>> logging.error["error message"]
ERROR:root:error message
>>> logging.critical["critical message"]
CRITICAL:root:critical message
Thông báo sẽ giống như thế này nhưng sẽ được ghi vào tệp có tên
import logging >>> logging.debug["debug message"]36 thay vì bảng điều khiển
>>> logging.info["info message"]
>>> logging.warning["warning message"]
WARNING:root:This is a warning message
>>> logging.error["error message"]
ERROR:root:error message
>>> logging.critical["critical message"]
CRITICAL:root:critical message
Mô-đun
import logging >>> logging.debug["debug message"]1 cũng cung cấp một số yếu tố tốc ký có thể dễ dàng thêm vào định dạng đầu ra. Ví dụ: chúng ta có thể sử dụng
>>> logging.info["info message"]
>>> logging.warning["warning message"]
WARNING:root:This is a warning message
>>> logging.error["error message"]
ERROR:root:error message
>>> logging.critical["critical message"]
CRITICAL:root:critical message
import logging >>> logging.debug["debug message"]38 để xuất thời gian mà con người có thể đọc được khi
>>> logging.info["info message"]
>>> logging.warning["warning message"]
WARNING:root:This is a warning message
>>> logging.error["error message"]
ERROR:root:error message
>>> logging.critical["critical message"]
CRITICAL:root:critical message
import logging >>> logging.debug["debug message"]39 được tạo
>>> logging.info["info message"]
>>> logging.warning["warning message"]
WARNING:root:This is a warning message
>>> logging.error["error message"]
ERROR:root:error message
>>> logging.critical["critical message"]
CRITICAL:root:critical message
import logging >>> logging.debug["debug message"]5
>>> logging.info["info message"]
>>> logging.warning["warning message"]
WARNING:root:This is a warning message
>>> logging.error["error message"]
ERROR:root:error message
>>> logging.critical["critical message"]
CRITICAL:root:critical message
Thông điệp nhật ký mới của chúng tôi trong tệp
Table of contents:
--> A simple introduction to Python's logging module
--> Configuring the logger
--> Creating your own logger object
--> Understanding when to use logging
17 sẽ như sauimport logging >>> logging.debug["debug message"]7Tạo đối tượng logger của riêng bạn
>>> logging.info["info message"]
>>> logging.warning["warning message"]
WARNING:root:This is a warning message
>>> logging.error["error message"]
ERROR:root:error message
>>> logging.critical["critical message"]
CRITICAL:root:critical message
Cho đến thời điểm này, chúng tôi đã làm việc với trình ghi nhật ký mặc định có tên
import logging >>> logging.debug["debug message"]01. Tài liệu
>>> logging.info["info message"]
>>> logging.warning["warning message"]
WARNING:root:This is a warning message
>>> logging.error["error message"]
ERROR:root:error message
>>> logging.critical["critical message"]
CRITICAL:root:critical message
import logging >>> logging.debug["debug message"]1 khuyên chúng ta nên tạo đối tượng logger của lớp
>>> logging.info["info message"]
>>> logging.warning["warning message"]
WARNING:root:This is a warning message
>>> logging.error["error message"]
ERROR:root:error message
>>> logging.critical["critical message"]
CRITICAL:root:critical message
import logging >>> logging.debug["debug message"]03 — điều này thậm chí còn quan trọng hơn khi có nhiều mô-đun trong ứng dụng của bạn
>>> logging.info["info message"]
>>> logging.warning["warning message"]
WARNING:root:This is a warning message
>>> logging.error["error message"]
ERROR:root:error message
>>> logging.critical["critical message"]
CRITICAL:root:critical message
Trình ghi nhật ký KHÔNG BAO GIỜ được khởi tạo trực tiếp mà phải luôn thông qua hàm cấp mô-đun
import logging >>> logging.debug["debug message"]04. Nhiều cuộc gọi đến
>>> logging.info["info message"]
>>> logging.warning["warning message"]
WARNING:root:This is a warning message
>>> logging.error["error message"]
ERROR:root:error message
>>> logging.critical["critical message"]
CRITICAL:root:critical messageimport logging >>> logging.debug["debug message"]05 có cùng tên sẽ luôn trả về một tham chiếu đến cùng một đối tượng Logger.
>>> logging.info["info message"]
>>> logging.warning["warning message"]
WARNING:root:This is a warning message
>>> logging.error["error message"]
ERROR:root:error message
>>> logging.critical["critical message"]
CRITICAL:root:critical message
— [Nguồn. Tài liệu đối tượng Logger].
Đây là một số mã từ dự án phát hiện gian lận của tôi
Tên của tệp python là
import logging >>> logging.debug["debug message"]06. do đó, chúng tôi đã khởi tạo một trình ghi nhật ký tùy chỉnh trong biến
>>> logging.info["info message"]
>>> logging.warning["warning message"]
WARNING:root:This is a warning message
>>> logging.error["error message"]
ERROR:root:error message
>>> logging.critical["critical message"]
CRITICAL:root:critical message
import logging >>> logging.debug["debug message"]07 có tên là
>>> logging.info["info message"]
>>> logging.warning["warning message"]
WARNING:root:This is a warning message
>>> logging.error["error message"]
ERROR:root:error message
>>> logging.critical["critical message"]
CRITICAL:root:critical message
import logging >>> logging.debug["debug message"]08 [xem dòng 11]
>>> logging.info["info message"]
>>> logging.warning["warning message"]
WARNING:root:This is a warning message
>>> logging.error["error message"]
ERROR:root:error message
>>> logging.critical["critical message"]
CRITICAL:root:critical message
Hiểu khi nào nên sử dụng ghi nhật ký“Chúng tôi khuyên chúng ta nên sử dụng trình ghi nhật ký cấp mô-đun bằng cách chuyển
import logging >>> logging.debug["debug message"]09 làm tham số tên cho
>>> logging.info["info message"]
>>> logging.warning["warning message"]
WARNING:root:This is a warning message
>>> logging.error["error message"]
ERROR:root:error message
>>> logging.critical["critical message"]
CRITICAL:root:critical messageimport logging >>> logging.debug["debug message"]50 để tạo đối tượng trình ghi nhật ký vì chính tên của trình ghi nhật ký sẽ cho chúng tôi biết các sự kiện đang được ghi lại từ đâu.
>>> logging.info["info message"]
>>> logging.warning["warning message"]
WARNING:root:This is a warning message
>>> logging.error["error message"]
ERROR:root:error message
>>> logging.critical["critical message"]
CRITICAL:root:critical messageimport logging >>> logging.debug["debug message"]09 là một biến tích hợp đặc biệt trong Python dùng để đánh giá tên của mô-đun hiện tại. ”
>>> logging.info["info message"]
>>> logging.warning["warning message"]
WARNING:root:This is a warning message
>>> logging.error["error message"]
ERROR:root:error message
>>> logging.critical["critical message"]
CRITICAL:root:critical message
— [Nguồn. Tài liệu đối tượng Logger].
Nếu bạn giống tôi, có lẽ bạn vẫn đang đọc và tự hỏi làm thế nào bạn nên biết một thông điệp tường trình nên được cung cấp những gì
Tài liệu ghi nhật ký cung cấp cho chúng tôi hướng dẫn hữu ích
Khi nào sử dụng ghi nhật ký; . Ghi nhật ký HOWTO]
Còn rất nhiều điều chúng tôi có thể đề cập về việc ghi nhật ký, nhưng điều này là đủ để bạn bắt đầu. Áp dụng càng nhiều điều chúng tôi đã đề cập trong bài viết này vào một dự án cá nhân để củng cố kiến thức. Đừng ngại tham khảo tài liệu khi bạn cần
Cảm ơn vì đã đọc
Kết nối với tôi.
LinkedIn
Twitter
Instagram
Nếu bạn thích đọc những câu chuyện như thế này và muốn hỗ trợ bài viết của tôi, hãy cân nhắc trở thành thành viên Medium. Với cam kết $5 một tháng, bạn mở khóa quyền truy cập không giới hạn vào các câu chuyện trên Phương tiện. Nếu bạn sử dụng liên kết đăng ký của tôi, tôi sẽ nhận được một khoản hoa hồng nhỏ