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
上一篇:Java中在数字前自动补零方法


下一篇:js判断一个字符串是否是回文字符串