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

By | July 4, 2019

When we are training our deep learning model, we should save some output string into a file.

On ubuntu, we can use script -f log.txt command to do it.

In this tutorial, we introduce another way to save python output message into a log file, here we use python logging library.

Python logging library provides five log level messages.

logging.debug('debug message')
logging.info('info message')
logging.warning('warning message')
logging.error('error message')
logging.critical('critical message')

The log level is:

CRITICAL > ERROR > WARNING > INFO > DEBUG > NOTSET

Defaultly, logging will save log message above warning level.

Then, to save python output message, we can use logging to do.

Preliminaries

#load logging
import logging

Set logging

These settings contains: the format of logging message, the path of logging file et al.

logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
                    datefmt='%a, %d %b %Y %H:%M:%S',
                    filename='/tmp/test.log',
                    filemode='w')

Execute python code

a = 1
b = 2
c = a + b

Save logging message into a file

logging.debug('debug message')
logging.info('info message')
logging.warning('warning message')
logging.error('error message')
logging.critical('critical message')

logging.info('sum = ' + str(c))

Then we open file: /temp/test.log, we will find content.

python logging output

Leave a Reply

Your email address will not be published. Required fields are marked *