python日志
作者:互联网
import logging
def get_logger(filename):
formatter = logging.Formatter( "[%(asctime)s] %(message)s",datefmt='%m/%d%I:%M:%S')
logger = logging.getLogger()
logger.setLevel(logging.INFO)
fh = logging.FileHandler(filename,"w")
fh.setFormatter(formatter)
logger.addHandler(fh)
sh = logging.StreamHandler()
sh.setFormatter(formatter)
logger.addHandler(sh)
return logger
log = get_logger("MyLogger")
log.info("HelloWorld")
效果
[11/17 08:42:49] HelloWorld
标签:logging,python,sh,fh,日志,formatter,logger,setFormatter 来源: https://blog.csdn.net/qq_37252519/article/details/121387792