Python日誌模組logging

2021-09-02 10:21:10 字數 4012 閱讀 1197

logging分為4個模組: loggers, handlers, filters, and formatters.

logger

logger.setlevel() 設定日誌級別

logger.addhandler()和logger.removehandler() 增加和刪除日誌處理器

logger.addfilter()和logger.removefilter() 增加和刪除過濾器

logger.debug(), logger.info(), logger.warning(), logger.error(), and logger.critical() 建立不同的級別的日誌

getlogger() 獲取日誌的根例項

handler

setlevel() 設定日誌級別

setformatter() 設定輸出格式

addfilter() and removefilter() 增加和刪除過濾器

formatter

預設形式為: %y-%m-%d %h:%m:%s.

格式為: %()s

日誌配置管理

硬編碼形式

import logging

# create logger

logger = logging.getlogger('******_example')

logger.setlevel(logging.debug)

# create console handler

andset

level

to debug

ch = logging.streamhandler()

ch.setlevel(logging.debug)

# create formatter

formatter = logging.formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

# add formatter to ch

ch.setformatter(formatter)

# add ch to logger

logger.addhandler(ch)

logger.debug('debug message')

logger.info('info message')

logger.warn('warn message')

logger.error('error message')

logger.critical('critical message')

輸出

$ python

******_logging_module

.py2005-03-19 15:10

:26,618 -

******_example

-debug

-debug

message

2005-03-19 15:10

:26,620 -

******_example

-info

-info

message

2005-03-19 15:10

:26,695 -

******_example

-warning

-warn

message

2005-03-19 15:10

:26,697 -

******_example

-error

-error

message

2005-03-19 15:10

:26,773 -

******_example

-critical

-critical

message

通過檔案配置管理日誌

**:

import logging

import logging.config

logging.config.fileconfig('logging.conf')

# create logger

logger = logging.getlogger('******example')

logger.debug('debug message')

logger.info('info message')

logger.warn('warn message')

logger.error('error message')

logger.critical('critical message')

配置檔案:

[loggers]

keys=root,******example

[handlers]

keys=consolehandler

[formatters]

keys=******formatter

[logger_root]

level=debug

handlers=consolehandler

[logger_******example]

level=debug

handlers=consolehandler

qualname=******example

propagate=0

[handler_consolehandler]

class=streamhandler

level=debug

formatter=******formatter

args=(sys.stdout,)

[formatter_******formatter]

format=%(asctime)s - %(name)s - %(levelname)s - %(message)s

datefmt=

輸出:

$ python

******_logging_config

.py2005-03-19 15:38

:55,977 -

******example

-debug

-debug

message

2005-03-19 15:38

:55,979 -

******example

-info

-info

message

2005-03-19 15:38

:56,054 -

******example

-warning

-warn

message

2005-03-19 15:38

:56,055 -

******example

-error

-error

message

2005-03-19 15:38

:56,130 -

******example

-critical

-critical

message

日誌格式

%(levelno)s: 列印日誌級別的數值

%(levelname)s: 列印日誌級別名稱

%(pathname)s: 列印當前執行程式的路徑,其實就是sys.argv[0]

%(filename)s: 列印當前執行程式名

%(funcname)s: 列印日誌的當前函式

%(lineno)d: 列印日誌的當前行號

%(asctime)s: 列印日誌的時間

%(thread)d: 列印執行緒id

%(threadname)s: 列印執行緒名稱

%(process)d: 列印程序id

%(message)s: 列印日誌資訊

流程圖

PythonStudy 日誌模組 logging

日誌 日之石日常的流水,將程式執行過程中的狀態或資料盡心記錄,一般是記錄到日誌檔案當中的。在正常的專案之中,專案的執行的一些列印資訊,採用logging列印到檔案當中,這個過程就稱作為 日誌記錄模組 以下為預設的操作日誌模組 匯入日誌模組 import logging logging為預設列印者,是...

python日誌模組

logging.debug 10 logging.info 20 logging.warning 30 logging.error 40 logging.critical 50預設級別為warning 預設輸出位置為控制台 import logging logging.basicconfig 可用引...

python 日誌模組

在軟體或者系統發生錯誤時可以通過日誌快速定位到錯誤,從而定位問題,解決問題。logging模組提供的日誌記錄函式所使用的日誌器設定的日誌級別是warning,因此只有warning級別的日誌記錄以及大於它的error和critical級別的日誌記錄被輸出了,而小於它的debug和info級別的日誌記...