0

I want to use the python logging facility to log output to different files (each module with its own log file). Therefore, I am adding something like the following to the begin of each python module see examples:

... other imports ...
import logging
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)
... code ...

and then use logger.info("text") to log messages. However, no data is written to the file named log/factory.log although it is being created! The directory log exists and I have permission to write to that directory. Using the logging facility with basicConfig works fine...

Alex
  • 41,580
  • 88
  • 260
  • 469
  • You have to set the logging level also in the handler, as shown in the answer. See [this](http://stackoverflow.com/q/17668633/510937) question for more information. – Bakuriu Jun 18 '14 at 20:04

1 Answers1

5

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()
nOOb cODEr
  • 236
  • 1
  • 4
  • The key difference between your code and his is that you are setting the `fh` level, while he is setting only the `logger` level. You should highlight this difference. – Bakuriu Jun 18 '14 at 20:05
  • Yes it is the 'global' logging level I forgot! I must have overlooked this in the example I have linked. I had the impresion, the level is set via `fh.setLevel(logging.DEBUG)` only... – Alex Jun 18 '14 at 20:13