我在此处的日志记录食谱的帮助下 编写了此代码,该代码应该为每个“设备”编写一行。它创建日志文件,但里面没有写入任何内容。
#!/usr/bin/env python
from __future__ import unicode_literals
import json
import logging
import glob
import logging.handlers
# This next bit is to ensure the script runs unchanged on 2.x and 3.x
try:
unicode
except NameError:
unicode = str
class LogWriter:
Name = 'LogWriter'
def __init__(self, Device_ID, Severity, Message):
self.id = Device_ID
self.severity = Severity
self.message = Message
self.logfile = './log/log_'+self.id+'.log'
def log(self):
# Set up a specific logger with our desired output level
logwriter = logging.getLogger('LogWriter')
logwriter.setLevel(logging.INFO)
# Add the log message handlers to the logger
timedRotatingLogFileHandler = logging.handlers.TimedRotatingFileHandler(self.logfile, when = 'midnight', backupCount=2)
#setup formatting
formatter = logging.Formatter('%(asctime)s %(processName)-10s %(name)s %(levelname)-8s %(message)s')
timedRotatingLogFileHandler.setFormatter(formatter)
logwriter.addHandler(timedRotatingLogFileHandler)
try:
logging.info(self.message, self.severity)
except:
return 1
return 0
def main():
for i in range(20):
logger = LogWriter(str(i), 'INFO', 'hello'+str(i))
logger.log()
if __name__ == '__main__':
main()
谁能告诉我为什么这不起作用?