Anthony Kong
Anthony Kong

Reputation: 40624

Why the RotatingFileHandler does not inherit the log format?

In the console I can see the logging messages formatted in the way I specified. However when I inspect the log file all I can see is the message part of original log entries.

Here is how I set up the logger when the program starts

LOG_FORMAT="%(asctime)-15s pid %(process)6d tid %(thread)4d %(levelname)-8s %(module)-8s %(message)s"

logging.basicConfig(format=LOG_FORMAT)
file_handler = logging.handlers.RotatingFileHandler(logfile_name)
logger = logging.getLogger(module_name)
logger.addHandler(file_handler)

How can make the file logger also respect the format setting?

Upvotes: 1

Views: 847

Answers (1)

ZZY
ZZY

Reputation: 3937

You can set formatter of file_handler:

file_handler = logging.handlers.RotatingFileHandler(logfile_name)
formatter = logging.Formatter(FORMAT)
file_handler.setFormatter(formatter)

More details on this page

Upvotes: 1

Related Questions