zoukankan      html  css  js  c++  java
  • Python-logging

    import logging
    def log_system_init(logfile):
        "初始化日志系统,同时将日志信息输出到控制台和logfile日志文件"
        root = logging.getLogger()
        root.setLevel(logging.NOTSET)
         
        filehandler = logging.FileHandler(logfile, "a",encoding='utf-8')
        filehandler.setLevel(logging.INFO)
         
        consolehandler = logging.StreamHandler()
        consolehandler.setLevel(logging.INFO)
         
        formatter0 = logging.Formatter("%(asctime)s %(levelname)s: %(message)s", datefmt='%H:%M:%S')
        formatter1 = logging.Formatter("%(asctime)s %(filename)s [line:%(lineno)d] - %(levelname)s: %(message)s", datefmt='%Y-%m-%d %H:%M:%S')
         
        filehandler.setFormatter(formatter1)
        consolehandler.setFormatter(formatter0)
         
        root.addHandler(filehandler)
        root.addHandler(consolehandler)
        return
    
    def log_critical(msg):
        "输出错误信息"
        logging.critical(msg)    
    def log_error(msg):
        "输出错误信息"
        logging.error(msg)    
    def log_warn(msg): # 常用的方法
        "输出警告信息"
        logging.warn(msg)    
    def log_info(msg): # 常用的方法
        "输出info信息"
        logging.info(msg)    
    def log_debug(msg):
        logging.debug(msg)        
    if __name__ == '__main__':
        log_system_init("1.log")
        log_critical(u"好的...")
        log_error(u"critical log...")
        log_warn(u"critical log...")
        log_info(u"critical log...")
        log_debug(u"critical log...")
  • 相关阅读:
    数据结构总结(UPDATING......)
    课件例题4.11,4.12
    Luogu P1525 关押罪犯
    Luogu P1540 机器翻译
    Luogu P1313 计算系数
    Luogu P1311 选择客栈
    Luogu P1519 穿越栅栏 Overfencing
    Luogu P2863 [USACO06JAN]牛的舞会The Cow Prom
    Tarjan学习笔记
    Luogu P3393 逃离僵尸岛
  • 原文地址:https://www.cnblogs.com/dmtz/p/11059675.html
Copyright © 2011-2022 走看看