0
votes

I want all loggers, especially loggers from external libraries, to log into the same file. To achieve this I'm creating FileHandler:

filename = ...
mode = ...
level = ...
fmt = ...

handler = logging.FileHandler(filename=filename, mode=mode)
handler.setFormatter(logging.Formatter(fmt))
handler.setLevel(level)

logger = logging.getLogger()
logger.setLevel(level)
logger.addHandler(handler)

I've read that all loggers in the same process "inherit" from root logger, so that all of them should log into the same file after all. I have tested it on two machines, one with python 2.7, the other with 2.6.

On the first one it works as I expected, but on the another one the log file exists but is empty. It seems that handler is registered properly, but logs are lost somewhere.

I have no idea why. Do you have any suggestions?

Thanks in advance,

Grzegorz

EDIT

To provide more comprehensive background: I have some kind of runner which from time to time executes tasks in separate processes. The runner has its own logger. For each task, which is executed in separate process, the first step is to set root logger as it is mentioned above.

1

1 Answers

0
votes

There was a subtle difference between Python2.6 and Python2.7. Method setLevel in 2.6 accepts constants such as logging.DEBUG whereas 2.7 accepts both logging.DEBUG constant and string "DEBUG".