これは、logging cookbook hereの助けを借りて書き ました。このコードは、「デバイス」ごとに 1 行を書き込むことになっていました。ログファイルを作成しますが、中には何も書き込まれません。
#!/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()
これがうまくいかない理由を誰か教えてください。