有时,我们需要记录python脚本执行日志,可以使用内置模块logging
import logging
logger = logging.getLogger(__name__)
logger.setlevel(level=logging.INFO)
handler = logging.FileHandler("/xxx/xxx/xx")
handler.setlever(logging.INFO)
formatter = logging.Formatter('%(asctime)s-%(name)s-%(levelname)s-%(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
接下来就可以通过
logger.info("message") 或logger.debug("debug")记录日志了