UPDATE
Looks like it's not logging because the logging level of the logger
is set to logging.WARN
. You must explicitly set the level on the logger to logging.DEBUG
as well. I think the log/factory.log
file is not being created because the log messages are not making it to that point yet. See http://dbgr.cc/v for a live demo of the code below:
import logging
import os
os.makedirs("log")
logger = logging.getLogger('factory')
fh = logging.FileHandler('log/factory.log')
fh.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s %(levelname)s: %(funcName)s:%(lineno)d %(message)s')
fh.setFormatter(formatter)
logger.addHandler(fh)
# notice that only the warning message is written
logger.debug("DEBUG MESSAGE")
logger.warn("WARNING MESSAGE")
logger.info("INFO MESSAGE")
with open("log/factory.log", "r") as f:
print f.read()
# now update the level on the Logger to logging.DEBUG:
logger.setLevel(logging.DEBUG)
logger.debug("DEBUG MESSAGE")
logger.warn("WARNING MESSAGE")
logger.info("INFO MESSAGE")
with open("log/factory.log", "r") as f:
print f.read()
Demo the code below at http://dbgr.cc/7:
import logging
import os
os.makedirs("log")
formatter = logging.Formatter('%(asctime)s %(levelname)s: %(funcName)s:%(lineno)d %(message)s')
# create logger with 'spam_application'
logger = logging.getLogger('factory')
logger.setLevel(logging.DEBUG)
fh = logging.FileHandler('log/factory.log')
fh.setLevel(logging.DEBUG)
fh.setFormatter(formatter)
# just so we can see things as they happen in stdout/stderr
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
ch.setFormatter(formatter)
# add the handlers to the logger
logger.addHandler(fh)
logger.addHandler(ch)
logger.debug("Debug log msg")
logger.info("Info log msg")
logger.warn("Warning log msg")
with open("log/factory.log", "r") as f:
print f.read()