无插件-简易版-输出日志文件模板
无插件-简易版-输出日志文件模板

无插件-简易版-输出日志文件模板

import logging

# 第一步,创建一个logger
logger = logging.getLogger()
logger.setLevel(logging.INFO)  # Log等级总开关

# 第二步,创建一个handler,用于写入日志文件
logfile = './log.log'
fh = logging.FileHandler(logfile, mode='a')
fh.setLevel(logging.DEBUG)  # 用于写到file的等级开关

# 第三步,再创建一个handler,用于输出到控制台
ch = logging.StreamHandler()
ch.setLevel(logging.WARNING)  # 输出到console的log等级的开关

# 第四步,定义handler的输出格式
formatter = logging.Formatter('[%(asctime)s][%(levelname)s] - %(message)s')
fh.setFormatter(formatter)
ch.setFormatter(formatter)

# 第五步,将logger添加到handler里面
logger.addHandler(fh)
logger.addHandler(ch)

if __name__ == '__main__':
    logger.debug('debug message')
    logger.info('info message')
    logger.warning('warning message')
    logger.error('error message')
    logger.critical('critical message')

5条评论

  1. 010

    Definiteloy beliieve that which youu stated. Yoour favorite justificatiopn seemmed tto
    bbe on tthe internnet tthe simplestt thig too be awaare
    of. I saay to you, I definitrly gett annhoyed
    while people think about wodries that they plainly ddo
    nnot know about. Yoou managed to hiit the nal upon thee top andd
    alkso defined out thee whole thning wjthout having sude efffect , people could takie a signal.
    Wiill peobably be back to geet more. Thanks

  2. Itts like yyou read my mind! You ssem to know a llot abot this, like yyou
    wrolte thee bopok iin it oor something. I tink tht
    you can ddo with a few piocs to drive the message ome a little bit, buut other tthan that, this
    is wonderful blog. A grea read. I’ll certainly be back.

  3. 684

    Linkk exchange iis nothing els howeve it iss just pplacing
    the other person’s weblog lik on your page aat suitable lace andd otyher person wilol alo do sane in fsvor of you.

回复 684 取消回复

您的电子邮箱地址不会被公开。