溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Python 之 logging日志模塊

發(fā)布時間:2020-08-05 18:19:22 來源:網(wǎng)絡 閱讀:632 作者:wx592bc92b285c7 欄目:編程語言

代碼

#Author Kang

import logging

logging.basicConfig(filename="app.log",level=logging.WARNING,format='%(asctime)s %(levelname)s: %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')

logging.info("app info test")
logging.warning("app warning message")
logging.error("app error message")

#filename:app.log文件,用于保存日志輸出信息
#level:日志級別
#format:日志輸出的格式

結果:app.log文件信息
02/18/2019 06:27:10 PM WARNING: app warning message
02/18/2019 06:27:10 PM ERROR: app error message

日志格式

Python 之 logging日志模塊

應用代碼

import logging
from logging.handlers import TimedRotatingFileHandler

class Log():
    def __init__(self):
        self.logger = logging.getLogger('Operation Mail')
        self.logger.setLevel(logging.INFO)
        ch = TimedRotatingFileHandler("access.log", when='D', encoding="utf-8")
        ch.setLevel(logging.DEBUG)
        formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
        ch.setFormatter(formatter)
        self.logger.addHandler(ch)

    def debug(self,log_msg):
        self.logger.debug(log_msg)

    def info(self,log_msg):
        self.logger.info(log_msg)

    def error(self,log_msg):
        self.logger.error(log_msg)

    def cirtical(self,log_msg):
        self.logger.critical(log_msg)
向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI