天天看點

logging 日志 子產品

# -*- coding: utf-8 -*-
"""
Created on Tue Jun 30 21:24:16 2020

@author: Administrator
"""

""" 
30 WARNING 06/30/2020 10:27:43 PM haha
50 CRITICAL 06/30/2020 10:27:43 PM heihei
40 ERROR 06/30/2020 10:27:43 PM c
10 DEBUG 06/30/2020 10:27:43 PM sa dassd
20 INFO 06/30/2020 10:27:43 PM das fsadfsa 
"""
import logging
logging.basicConfig(
        filename='E://w.log',
        level=logging.DEBUG,
        
        format="%(levelno)s %(levelname)s %(asctime)s %(message)s",
        datefmt="%m/%d/%Y %I:%M:%S %p"
        )
logging.warning("haha")
logging.critical("heihei")
logging.error("c")
logging.debug("sa dassd")
logging.info("das fsadfsa ")

"""
Python使用logging子產品記錄日志涉及四個主要類,使用官方文檔中的概括最為合适:
●logger提供了應用程式可以直接使用的接口;
●handler将(logger建立的)日 志記錄發送到合适的目的輸出;
●filter提供了細度裝置來決定輸出哪條日志記錄;
●formatter決定 日志記錄的最終輸出格式。

"""


"""=============進階================="""

""" 按照level最高的來 兩個漏鬥,全局先過濾,自己的再過濾 """
import logging

class IgnoreBackupLogFilter(logging.Filter):
    def filter(self,record): #固定寫法
        return "db backup" not in record.getMessage()



""" 生成logger對象 """
logger=logging.getLogger("web")
logger.setLevel(logging.DEBUG)
""" 将filter加入到logger"""
logger.addFilter(IgnoreBackupLogFilter())
""" 生成handler對象"""
ch=logging.StreamHandler()
ch.setLevel(logging.DEBUG)

fh=logging.handlers.RotatingFileHandler("E://web.log",maxBytes=10,backupCount=3)#大小是10位元組,存3份
fh=logging.handlers.TimedRotatingFileHandler("E://web.log",when='S', interval=5)

#fh=logging.FileHandler("E://web.log")
#fh.setLevel(logging.INFO)
""" 将handler對象綁定到logger"""
logger.addHandler(ch)
logger.addHandler(fh)

""" 生成formatter對象"""
file_formatter=logging.Formatter("%(levelno)s %(levelname)s %(asctime)s %(message)s")
console_formatter=logging.Formatter("%(levelno)s %(levelname)s %(asctime)s %(message)s")

""" 将formatter對象綁定到handler對象上"""
ch.setFormatter(console_formatter)
fh.setFormatter(file_formatter)


logging.warning("haha")
logging.critical("heihei")
logging.error("c")
logging.debug("sa dassd")
logging.info("das fsadfsa ")      

1.最近新的了解:

  1.1日志一共分成5個等級,從低到高分别是:DEBUG ,INFO ,WARNING ,ERROR, CRITICAL。

  1.2python中配置logging有三種方式

  第一種:基礎配置,logging.basicConfig(filename="config.log",filemode="w",format="%(asctime)s-%(name)s-%(levelname)s-%                    (message)s",level=logging.INFO)。

       第二種:使用配置檔案的方式配置logging,使用fileConfig(filename,defaults=None,disable_existing_loggers=Ture )函數來讀取配置檔案。

       第三種:使用一個字典方式來寫配置資訊,然後使用dictConfig(dict,defaults=None, disable_existing_loggers=Ture )函數來瓦按成logging的配置.

  1.3對參數解析:

  filename:日志路徑

  filemode:預設"a",意味追加

  format:格式化為處理程式使用指定的格式字元串。

    %(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: 列印日志資訊

  datefmt:指定時間格式,同time.strftime() 預設是年月日時分秒毫秒

  style:

  level:設定日志級别,預設為logging.WARNING

  stream:指定将日志的輸出流,可以指定輸出到sys.stderr,sys.stdout或者檔案,預設輸出到sys.stderr,當stream和filename同時指定時,stream被         忽略