2

If I get the logger with a name and add a FileHandler it does not write to the file.

This works and writes correctly to the file:

log = logging.getLogger()
fh = logging.FileHandler(logfile)
log.addHandler(fh)
fh_fmt = logging.Formatter("%(asctime)s (%(levelname)s)\t: %(message)s")
fh.setFormatter(fh_fmt) 
log.setLevel(logging.INFO)

This does not write to the file:

log = logging.getLogger(name)
fh = logging.FileHandler(logfile)
log.addHandler(fh)
fh_fmt = logging.Formatter("%(asctime)s (%(levelname)s)\t: %(message)s")
fh.setFormatter(fh_fmt) 
log.setLevel(logging.INFO)

The only difference is that I get a 'named' logger.

8
  • The code works for me. What version are you using and do you have file permissions? Commented Oct 24, 2011 at 20:43
  • 2
    What is the value of 'name' passed to getLogger? Commented Oct 24, 2011 at 20:48
  • i have file permissions because the upper code works. the value for name is a string, whatever i set it does not write to the file. im on python 2.6 Commented Oct 24, 2011 at 22:27
  • You've not shown the code which makes the logging call. Which logger are you logging to? What's its level? Can you update your question to show a complete script which fails? Commented Oct 24, 2011 at 22:43
  • there is not much logic there. after setting up the logger i call it like: log.info('the message...') Commented Oct 25, 2011 at 13:42

1 Answer 1

3

This is a rather old question, but I believe I found the underlying problem and solution, at least with a newer version of Python.

The second code example starts with log = logging.getLogger(name), where name is presumed to be a string representing the name of the logger. Since a name is provided, this log will not be the root logger. According to Logger.setLevel(level) docs for Python 3.6+,

When a logger is created, the level is set to NOTSET (which causes all messages to be processed when the logger is the root logger, or delegation to the parent when the logger is a non-root logger).

This tells us that we have to set the level of our logger so that it will actually process the messages instead of passing it to the root logger.

This is a code example I wrote (in Python 3.7) that does not work:

from pathlib import Path
import logging

formatter = logging.Formatter('%(name)s [%(levelname)s] %(message)s')
log_file_dir = Path('./log/')
config_file = 'config_file.txt'
config_file_path = log_file_dir / config_file

logger = logging.getLogger('example_logger')
fh = logging.FileHandler(config_file_path, mode='w')
fh.setLevel(logging.INFO)
fh.setFormatter(formatter)
logger.addHandler(fh)
logger.info('Start Configuration Log')

And this one works by adding one line:

from pathlib import Path
import logging

formatter = logging.Formatter('%(name)s [%(levelname)s] %(message)s')
log_file_dir = Path('./log/')
config_file = 'config_file_2.txt'
config_file_path = log_file_dir / config_file

logger = logging.getLogger('example_logger')
logger.setLevel(logging.INFO) # <------ Or the applicable level for your use-case
fh = logging.FileHandler(config_file_path, mode='w')
fh.setLevel(logging.INFO)
fh.setFormatter(formatter)
logger.addHandler(fh)
logger.info('Start Configuration Log')

Note: The first code example does create the chosen log file, but does not write 'Start Configuration Log' to the file.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.