日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python多线程写日志_python 应用logging多线程写多个log文件

發(fā)布時間:2023/12/15 python 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python多线程写日志_python 应用logging多线程写多个log文件 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

python 使用logging多線程寫多個log文件

import threading

import time

import logging

import logging.handlers

LEVELS={'notset':logging.DEBUG,

'debug':logging.DEBUG,

'info':logging.INFO,

'warning':logging.WARNING,

'error':logging.ERROR,

'critical':logging.CRITICAL}

LOG_FILENAME = 'test.log'

LOG_BACKUPCOUNT = 5

LOG_LEVEL = 'notset'

def InitLog(file_name,logger):

LOG_FILENAME = file_name

handler = logging.handlers.RotatingFileHandler(LOG_FILENAME,maxBytes=10*1024*1024,backupCount=LOG_BACKUPCOUNT)

#handler = logging.FileHandler(LOG_FILENAME)

formatter = logging.Formatter("[ %(asctime)s ][ %(levelname)s ] %(message)s\n")

handler.setFormatter(formatter)

#logger = logging.getLogger()

logger.addHandler(handler)

logger.setLevel(LEVELS.get(LOG_LEVEL.lower()))

return logger

class t1(threading.Thread):

def __init__(self,threadName):

threading.Thread.__init__(self,name = threadName)

logger1 = logging.getLogger("thread.a")

self.logger = InitLog("thread1.log",logger1)

def run(self):

global i

self.logger.info("test1")

while True:

print '*************hello t1*****************'

self.logger.info("******hello t1******")

self.logger.debug("t1 debug")

self.logger.warning("t1 warning")

self.logger.error("t1 error")

i += 1

print i

if i> 10:

break

time.sleep(3)

self.logger.info("test1 over")

class t2(threading.Thread):

def __init__(self,threadName):

threading.Thread.__init__(self,name = threadName)

logger2 = logging.getLogger("thread.b")

self.logger = InitLog("thread2.log",logger2)

print "self.logger = %s" %self.logger

None

def run(self):

global i

self.logger.info("test2")

while True:

print 'hello t2'

self.logger.info("hello t2")

self.logger.debug("t2 debug")

self.logger.warning("t2 warning")

self.logger.error("t2 error")

i += 1

print i

if i > 10:

break

time.sleep(5)

self.logger.info("test2.over")

i = 0

p1 = t1('p1')

p1.start()

p2 = t2('p2')

p2.start()

總結(jié)

以上是生活随笔為你收集整理的python多线程写日志_python 应用logging多线程写多个log文件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。