Jupyter notebook solves the problem that the print information cannot be viewed after closing the browser

1. Problem description

In the browser, after we close the printing of the model being trained, and open it again, we can see that there is no output of any information. In addition to dealing with caching methods, we can use python's logging to output logs.

2. Code writing

import logging
import sys
import datetime

def init_logger(filename, logger_name):
    '''
    @brief:
        initialize logger that redirect info to a file just in case we lost connection to the notebook
    @params:
        filename: to which file should we log all the info
        logger_name: an alias to the logger
    '''

    # get current timestamp
    timestamp = datetime.datetime.utcnow().strftime('%Y%m%d_%H-%M-%S')
    
    logging.basicConfig(
        level=logging.INFO, 
        format='[%(asctime)s] %(name)s {%(filename)s:%(lineno)d} %(levelname)s - %(message)s',
        handlers=[
            logging.FileHandler(filename=filename),
            logging.StreamHandler(sys.stdout)
        ]
    )

    # Test
    logger = logging.getLogger(logger_name)
    logger.info('### Init. Logger {} ###'.format(logger_name))
    return logger

# Initialize
my_logger = init_logger("./ml_notebook.log", "ml_logger")

Each print output such as:

my_logger.info("XXX")

Another example is to record the time and output of the last cell and print it to the log file, for example, in the first cell:

%%capture out
%%timeit
a = 1+1

 Then in the next cell:

my_logger.info("capture & timeit: " + out.stdout)

You can know the time and content of the last cell operation.

reference:

How does jupyter notebook execute in the background and save the result after closing the browser? - Know (zhihu.com)

logging — Logging facility for Python — Python 3.10.5 documentation

Guess you like

Origin blog.csdn.net/buluxianfeng/article/details/125731803