Python Logging Export to File
-
date_range Sept. 01, 2020 - Tuesday info
As for the need for logging from Python program, here is a memo for the use of the Python built-in module
logging
Out-of-the-Box Python snippet
import logging
log_save_dir = '/home'
# Setup Logging
# create a logger
logger = logging.getLogger(KFZ)
logger.setLevel(level=logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(message)s')
# set file handler
handler = logging.FileHandler(os.path.join(log_save_dir, 'log.txt'))
handler.setFormatter(formatter)
logger.addHandler(handler)
# set stream handler
console = logging.StreamHandler()
console.setFormatter(formatter)
logger.addHandler(console)
Reference:
Official Doc https://www.jianshu.com/p/feb86c06c4f4 https://www.cnblogs.com/liujiacai/p/7804848.html
KF