I am trying to switch from simple printing to proper logging.
I want to use two different loggers, the first one displaying information on screen and the other one in a file.
My problem is that even though I set my handler level to DEBUG, messages are only displayed from WARNING.
Here is a sample of my code :
def setup_logger(self):
"""
Configures our logger to save error messages
"""
# create logger for 'facemovie'
self.my_logger = logging.getLogger('FileLog')
# create file handler which logs even debug messages
fh = logging.FileHandler('log/fm.log')
fh.setLevel(logging.DEBUG)
# create console handler with a higher log level
self.console_logger = logging.getLogger('ConsoleLog')
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
# create formatter and add it to the handlers
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
#ch.setFormatter(formatter)
##Start logging in file
self.my_logger.info("######")
# add the handlers to the logger
self.my_logger.addHandler(fh)
self.console_logger.addHandler(ch)
# DEBUG
self.console_logger.info("MFCKR")
self.console_logger.debug("MFCKR")
self.console_logger.warning("MFCKR")
self.console_logger.error("MFCKR")
self.console_logger.critical("MFCKR")
self.my_logger.info("MFCKR")
self.my_logger.debug("MFCKR")
self.my_logger.warning("MFCKR")
self.my_logger.error("MFCKR")
self.my_logger.critical("MFCKR")
And the output :
[jll@jll-VirtualBox:~/Documents/FaceMovie]$ python Facemoviefier.py -i data/inputs/samples -o data/
Selected profile is : frontal_face
MFCKR
MFCKR
MFCKR
Starting Application !
Output #0, avi, to 'data/output.avi':
Stream #0.0: Video: mpeg4, yuv420p, 652x498, q=2-31, 20780 kb/s, 90k tbn, 5 tbc
FaceMovie exited successfully!
[jll@jll-VirtualBox:~/Documents/FaceMovie]$ cat log/fm.log
2012-07-15 22:23:24,303 - FileLog - WARNING - MFCKR
2012-07-15 22:23:24,303 - FileLog - ERROR - MFCKR
2012-07-15 22:23:24,303 - FileLog - CRITICAL - MFCKR
I red the doc and searched for similar errors on the web, but couldn’t find any.
Would you have ideas about the reason why the logger doesn’t display DEBUGs and INFOs ?
Thx !
Just found the answer here
Don’t know why I didn’t find it before.
The whole logger level has to be set, even though the handler also is.
I was not setting up the logger’s level, and I guess in this case default is Warning.
Problem solved !