午夜视频在线网站,日韩视频精品在线,中文字幕精品一区二区三区在线,在线播放精品,1024你懂我懂的旧版人,欧美日韩一级黄色片,一区二区三区在线观看视频

分享

python日志包從頭到尾講清楚

 網(wǎng)摘文苑 2022-11-26 發(fā)布于新疆
文章圖片1

大綱

今天我們就來一起挖一挖python里面的各種日志的用法,廢話不多說,日志包我為了講清楚,涉及到的內(nèi)容大綱如下:

文章圖片2

基本打印

文章圖片3

如上圖看到的那么簡(jiǎn)單,在python中打印日志只要import logging包即可。直接執(zhí)行效果如下圖:

文章圖片4

日志配置

如果沒有日志配置,日志只能打印到stdout,那就會(huì)很糟糕,因此我們需要有一些配置可以輸出到文件

import logging# add filemode='w' to overwritelogging.basicConfig(filename='sample.log', level=logging.INFO)logging.debug('This is a debug message') # This one won't loglogging.info('Informational message')logging.error('An error has happened!')

增加basicconfig后日志就可以打印到指定文件,但是日志我們還需要設(shè)置日志格式

import logginglogger = logging.getLogger('stream_logger')logger.setLevel(logging.INFO)console = logging.StreamHandler()formatter = logging.Formatter('%(asctime)s - %(name)s - %(message)s')console.setFormatter(formatter)logger.addHandler(console)logger.info('This is an informational message')

在python中還可以從配置文件讀取日志規(guī)則

import loggingimport logging.configlogging.config.fileConfig('logging.conf')logger = logging.getLogger('exampleApp')logger.info('Program started')logger.info('Done!')

logging.conf配置如下:

[loggers]keys=root,exampleApp[handlers]keys=fileHandler, consoleHandler[formatters]keys=myFormatter[logger_root]level=CRITICALhandlers=consoleHandler[logger_exampleApp]level=INFOhandlers=fileHandlerqualname=exampleApp[handler_consoleHandler]class=StreamHandlerlevel=DEBUGformatter=myFormatterargs=(sys.stdout,)[handler_fileHandler]class=FileHandlerformatter=myFormatterargs=('config.log',)[formatter_myFormatter]format=%(asctime)s - %(name)s - %(levelname)s - %(message)sdatefmt=

日志過濾

通過Formatter就可以搞定日志格式問題,如果生產(chǎn)環(huán)境日志太多,又想有一些日志過濾。

import loggingimport sysclass MyFilter(logging.Filter): def filter(self, record): if record.funcName == 'a': return False return Truelogger = logging.getLogger('filter_test')logger.addFilter(MyFilter())def a(): ''' Ignore this function's log messages ''' logger.debug('Message from function a')def b(): logger.debug('Message from B')if __name__ == '__main__': logging.basicConfig(stream=sys.stderr, level=logging.DEBUG) a() b()

這樣你的日志就可以實(shí)現(xiàn)過濾效果。

日志切割

可是生產(chǎn)環(huán)境真正運(yùn)行的服務(wù),又不僅僅是運(yùn)行一天,日志一直打印到同一個(gè)文件非常不方便debug和定位,有時(shí)候一天幾個(gè)G的文件,如果運(yùn)行30天去查日志時(shí)會(huì)非常耗時(shí),我們需要一個(gè)日志切割的邏輯來保障日志可以按照時(shí)間動(dòng)態(tài)切換文件。

  • 按照時(shí)間切割
import loggingimport timefrom logging.handlers import TimedRotatingFileHandlerdef create_timed_rotating_log(path):    ''''''    logger = logging.getLogger('Rotating Log')    logger.setLevel(logging.INFO)    handler = TimedRotatingFileHandler(path,                                       when='s',                                       interval=5,                                       backupCount=5)    logger.addHandler(handler)    for i in range(6):        logger.info('This is a test!')        time.sleep(75)if __name__ == '__main__':    log_file = 'timed_rotation.log'    create_timed_rotating_log(log_file)
  • 按照大小切割,并設(shè)置保留數(shù)
import loggingimport timefrom logging.handlers import RotatingFileHandlerdef create_rotating_log(path): ''' Creates a rotating log ''' logger = logging.getLogger('Rotating Log') logger.setLevel(logging.INFO) # add a rotating handler handler = RotatingFileHandler(path, maxBytes=20, backupCount=5) logger.addHandler(handler) for i in range(10): logger.info('This is test log line %s' % i) time.sleep(1.5)if __name__ == '__main__': log_file = 'rotated.log' create_rotating_log(log_file)

多進(jìn)程日志

在python中因?yàn)槭蹽IL的影響,涉及到并發(fā)性能問題時(shí)經(jīng)常使用多進(jìn)程方式來解決,此時(shí)多進(jìn)程日志打印會(huì)受到文件鎖的影響,為了避免日志打印錯(cuò)亂,日志異常,我們針對(duì)這種場(chǎng)景需要定制化處理下日志:

import loggingfrom logging.handlers import RotatingFileHandler, QueueHandlerfrom multiprocessing import Processclass MultiProcessQueueLoggingListner(Process):    def __init__(self, name, queue):        super().__init__()        self.name = name        self.queue = queue        self.logger = logging.getLogger(name)        self.file_handler = RotatingFileHandler(name, maxBytes=536870912, backupCount=2)        self.formatter = logging.Formatter('%(asctime)s %(processName)-10s %(name)s %(levelname)-8s %(message)s')        self.file_handler.setFormatter(self.formatter)        self.logger.addHandler(self.file_handler)    def run(self):        while True:            try:                record = self.queue.get()                if record is None:                    break                self.logger.handle(record)            except Exception:                import sys, traceback                print('Whoops! Problem:', file=sys.stderr)                traceback.print_exc(file=sys.stderr)class MulitProcessQueueLogger(object):    def __init__(self, name, queue):        self.name = name        self.queue = queue        self.queue_handler = QueueHandler(queue)        self.logger = logging.getLogger(name)        self.logger.addHandler(self.queue_handler)        self.logger.setLevel(logging.DEBUG)

測(cè)試代碼如下:

import multi_process_loggingimport multiprocessingfrom time import sleepdef worker(po): name = multiprocessing.current_process().name po = multi_process_logging.MulitProcessQueueLogger('test.log', q) print('In worker') for i in range(10): po.logger.info(f'Logging from {name} line {i}') po.queue.put(None)def main(): q = multiprocessing.Queue() lp = multi_process_logging.MultiProcessQueueLoggingListner('test.log', q) lp.start() p = multiprocessing.Process(target=worker, args=(q,)) p.start() p.join() lp.join()if __name__ == '__main__': main()

總結(jié)

至此,大部分python打印日志的場(chǎng)景使用方式都講解到了,希望對(duì)讀者有所幫助。

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多