1. 程式人生 > >Django 中使用 logging 模組記錄系統日誌

Django 中使用 logging 模組記錄系統日誌

Handlers

The handler is the engine that determines what happens to each message in a logger. It describes a particular logging behavior, such as writing a message to the screen, to a file, or to a network socket.

Like loggers, handlers also have a log level. If the log level of a log record doesn’t meet or exceed the level of the handler, the handler will ignore the message.

A logger can have multiple handlers, and each handler can have a different log level. In this way, it is possible to provide different forms of notification depending on the importance of a message. For example, you could install one handler that forwards ERROR and CRITICAL messages to a paging service, while a second handler logs all messages (including ERROR

 and CRITICAL messages) to a file for later analysis.

handler 是決定 logger 中每個訊息需要如何處理的引擎。它描述特定的記錄行為,比如把訊息列印到螢幕,輸出到檔案,或者通過網路套接字傳送。

與 logger 一樣,handler 也用過有日誌級別。如果一個日誌記錄的日誌級別不等於或不大於 handler 的級別,handler 會忽略此訊息。

一個 logger 可以擁有多個 handler,每個 handler 可以有(與其它 handler )不同的日誌級別。這樣,便可以依據訊息重要程度的不同來提供不同的通知形式。例如,你可以設定一個 handler 來轉發 ERROR 和 CRITICAL 級別的訊息到頁面服務,而另一個 handler 記錄所有的資訊(包括 ERROR 和 CRITICAL)到檔案用於分析。