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