Tutorial Example

Fix Python logging module not writing to file – Python Tutorial

In python, we can use logging model to print and save log or message to a file. Here is an tutorial:

Save Python Message into a Log File with logging – Deep Learning Tutorial

However, we may find it does not work in some situations. Here is an example:

#-*- coding: utf-8 -*-

import datetime
import os, time, pickle
import numpy as np
import tensorflow as tf
import logging
##loging 
logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s %(message)s',
                    datefmt='%a, %d %b %Y %H:%M:%S',
                    filename= 'log.txt',
                    filemode='w')
console = logging.StreamHandler()
console.setLevel(logging.INFO)
# add the handler to the root logger
logging.getLogger().addHandler(console)

logging.info("\nParameters:")
for i in range(10):
    logging.info(i)
logging.info("end!")

This example code will save log message to log.txt. However, log.txt is not created after running this code.

Why logging model does not work?

Look at the source code of logging, we can find the reason.

If root.handlers is not empty, log.txt will not be created. We should empty root.handlers before calling basicConfig() method.

Here is a solution:

for handler in logging.root.handlers[:]:
    logging.root.removeHandler(handler)

This code will remove all handlers in logging.root.handlers. You must call this code before logging.basicConfig()

The full code is below:

import logging
##loging
for handler in logging.root.handlers[:]:
    logging.root.removeHandler(handler)
logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s %(message)s',
                    datefmt='%a, %d %b %Y %H:%M:%S',
                    filename= 'log.txt',
                    filemode='w')
console = logging.StreamHandler()
console.setLevel(logging.INFO)
# add the handler to the root logger
logging.getLogger().addHandler(console)

logging.info("\nParameters:")
for i in range(10):
    logging.info(i)
logging.info("end!")

Run this code, we will find log.txt is create. Here is the result: