python 管理日誌

2021-09-24 19:05:40 字數 4853 閱讀 2103

io操作,不要太頻繁

log的作用

日誌資訊

成熟的第三方日誌

初始化/寫日誌例項需要制定級別,只有級別等於或者高於制定級別時才被記錄

使用方式

import logging

logging.debug(

'this is a debug log.'

)logging.info(

'this is a info log.'

)logging.warning(

'this is a warning log.'

)logging.error(

'this is a error log.'

)logging.critical(

'this is a caitical log.'

)#另一種寫法

logging.log(logging.debug,

"this is a debug log."

)logging.log(logging.info,

"this is a info log."

)logging.log(logging.warning,

"this is a warning log."

)logging.log(logging.error,

"this is a error log."

)logging.log(logging.critical,

"this is a caitical log."

)

import logging

logging.basicconfig(filename=

'日誌檔案'

,level=logging.debug)

logging.debug(

'this is a debug log.'

)logging.info(

'this is a info log.'

)logging.warning(

'this is a warning log.'

)logging.error(

'this is a error log.'

)logging.critical(

'this is a caitical log.'

)#另一種寫法

logging.log(logging.debug,

"this is a debug log."

)logging.log(logging.info,

"this is a info log."

)logging.log(logging.warning,

"this is a warning log."

)logging.log(logging.error,

"this is a error log."

)logging.log(logging.critical,

"this is a caitical log."

)

import logging

log_format =

'%(asctime)s----%(levelname)s----%(message)s'

logging.basicconfig(filename=

'日誌檔案'

,level=logging.debug,

format

=log_format)

logging.debug(

'this is a debug log.'

)logging.info(

'this is a info log.'

)logging.warning(

'this is a warning log.'

)logging.error(

'this is a error log.'

)logging.critical(

'this is a caitical log.'

)#另一種寫法

logging.log(logging.debug,

"this is a debug log."

)logging.log(logging.info,

"this is a info log."

)logging.log(logging.warning,

"this is a warning log."

)logging.log(logging.error,

"this is a error log."

)logging.log(logging.critical,

"this is a caitical log."

)

asctime  %

(asctime)s

created  %(created)f
relativecreated  %

(relativecreated)d

levelname   %(levelname)s
msecs  %

(msecs)d

levelno  %

(levelno)s

name  %(name)s
message  %

(message)s

pathname  %

(pathname)s

filename  %(filename)s
module  %

(module)s

lineno  %

(lineno)d

funcname  %

(funcname)s

process  %

(process)d

processname  %

(processname)s

thread  %

(thread)d

threadname  %

(threadname)s

logger

handler

format類

fitler類

#需求

#1、要求將所有級別的的所有日誌都寫入磁碟檔案

#2、all.log檔案記錄所有的日誌資訊,日誌格式為:日期和時間 - 日誌級別 - 日誌資訊

#3、error.log檔案中單獨記錄error及以上級別的日誌資訊,日誌格式:日期和時間 - 日誌級別 - 檔名[:行號] - 日誌資訊

#4、要求all.log在每天凌晨進行日誌切割

#分析#1、要記錄所有的級別的日誌,因此日誌器的而有效level需要設定為最低級別--dubeg

#2、日誌需要被傳送到兩個不同的目的地,因此需要為日誌設定兩個handler;另外,兩個目的地都是磁碟檔案,因此這兩個handler都是與filehandler

#3、all.log要求按照時間進行日誌切割,因此需要使用logging.handlers.timedrotatingfilehandler;而error.log沒有要求日誌切割

#4、兩個日誌檔案的格式不同,因此需要對這兩個handler分別設定格式器

import logging

import logging.handlers

import datetime

#定義loggerlogger = logging.getlogger(

'mylogger'

)logger.setlevel(logging.debug)

rf_handler = logging.handlers.timedrotatingfilehandler(

'all.log'

,when=

'midnight'

,interval=

1,backupcount=

7,attime=

none

)rf_handler.setformatter(logging.formatter(

'%(asctima)s - %(levelname)s - %(message)s'))

f_handler = logging.filehandler(

'error.log'

)f_handler.setlevel(logging.error)

f_handler.setformatter(logging.formatter(

'%(asctima)s - %(levelname)s - %(filename)s[:(loneno)d] - %(message)s'))

logger.addhandler(rf_handler)

logger.addhandler(f_handler)

logger.debug(

'debug message'

)logger.info(

'info message'

)logger.warning(

'warning message'

)logger.error(

'error message'

)logger.critical(

'critical message'

)

python 日誌控制管理

logging.basicconfig level logging.info,日誌的級別 必須設定 format asctime s name s levelname s message s 列印的日誌格式 logging.debug debug級別的,下邊的日誌都列印輸出 logging.info...

MySql DDL日誌 mysql日誌管理

一 錯誤日誌 1.作用 排查mysql執行時的故障 2.自定義錯誤檔案日誌位置 在配置檔案中,加入 log error 檔案目錄,在重啟資料庫 3.檢視錯誤檔案位置 select log error 二 二進位制日誌 1.作用 主從依賴二進位制檔案 資料恢復靠二進位制檔案 2.開啟二進位制文集 在配...

php mysql 日誌 MySQL日誌管理

任何一種資料庫中,都有各種各樣的日誌。mysql也不例外,在mysql中有4種不同的日誌 分別錯誤日誌 二進位制日誌 查詢日誌和慢查詢日誌。這些日誌記錄著mysql資料庫不同方面的蹤跡。下文將介紹這4種不同的日誌作用和用途。一.錯誤日誌 錯誤日誌在mysql資料庫中很重要,它記錄著mysqld啟動和...