Reputation: 123
I wrote this from help from the logging cookbook here this code was supposed to write a line for each "device". It creates the log files, but nothing is written inside.
#!/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()
Can anyone tell me why this is not working please?
Upvotes: 1
Views: 136
Reputation: 123
Both cox and Vinay were correct, but no explicit solution was provided. cox's solution didn't fully work because his suggested definition would hard code the severity not allowing modification by the calls
so I'm no longer passing the device_ID, severity and message to the class, but directly to the log() function
This was the easiest way i got this done.
a = getattr(logwriter, severity)
a(message)
replacing
logging.info(self.message, self.severity)
Best practices are to not create handlers inside the class definition as Vinay said. The solution i found for that is to define a separate logger module, attach the handlers outside the class definition in the module, create the new logger only once in the calling program, and then each new logger request can log to a child log as explained in the logging cookbook.
Upvotes: 0
Reputation: 99365
You're adding a handler to the 'LogWriter' logger, but logging to the root logger, which has no handlers added to it.
It's not good practice to add handlers and set levels except just once from a if __name__ == '__main__'
clause, it's not really clear from your posted code whether you're following the right practice.
Upvotes: 1