2
我是新來的Python和編程,我試圖做一個簡單的日誌記錄器。我想使用TimedRotatingFileHandler
,因爲它符合我的需求,儘管我無法更改後綴,這很重要。我將需要我的日誌使用「.csv」擴展名。 所以我找到了一個類MyTimedRotatingFileHandler
應該添加後綴,但不幸的是,我沒有得到我犯的錯誤。Python每日記錄器後綴爲TimedRotatingFileHandler
下面是我試圖實現的代碼,由從網上截取的幾個片段組成。
# -*- coding: utf-8 -*-
import serial
import os
import time
import logging
from logging.handlers import TimedRotatingFileHandler
ser = serial.Serial('/dev/ttyACM0', 38400, timeout=15)
class MyTimedRotatingFileHandler(logging.handlers.TimedRotatingFileHandler):
def __init__(self,dir_log):
self.dir_log = dir_log
filename = self.dir_log+time.strftime("%m%d%Y")+".csv"
logging.handlers.TimedRotatingFileHandler.__init__(self,filename,
when='S', interval=5, backupCount=0, encoding=None)
def doRollover(self):
line = ser.readline()
self.baseFilename = self.dir_log+time.strftime("%d/%m/%Y %H:%M:%S")+".csv"
self.stream = open(self.baseFilename, 'w')
self.rolloverAt = self.rolloverAt + self.interval
a="%s,%s,%s,%s" % (self.baseFileName,'Température :',line,"\n")
#a = "{0},{1},{2},{3}".format(self.baseFileName,'Température :',line,"\n")
print a
self.info(a)
# logger.flush()
ser.close()