zoukankan      html  css  js  c++  java
  • Python日志记录

    官方文档:https://docs.python.org/2/library/logging.html
    logging.basicConfig()函数中可通过具体参数来更改logging模块默认行为,可用参数有
    filename:用指定的文件名创建FiledHandler(后边会具体讲解handler的概念),这样日志会被存储在指定的文件中。
    filemode:文件打开方式,在指定了filename时使用这个参数,默认值为“a”还可指定为“w”。
    format:指定handler使用的日志显示格式。
    datefmt:指定日期时间格式。
    level:设置rootlogger; 默认的日志级别设置为WARNING(日志级别等级CRITICAL > ERROR > WARNING > INFO > DEBUG > NOTSET)
    stream:用指定的stream创建StreamHandler。可以指定输出到sys.stderr,sys.stdout或者文件,默认为sys.stderr。若同时列出了filename和stream两个参数,则stream参数会被忽略。

    只打印到控制台

    # coding:utf-8
    import logging
    import sys
    
    # 日志格式化方式
    #LOG_FORMAT = "%(asctime)s - %(levelname)s - %(message)s"
    
    LOG_FORMAT = "%(asctime)s	File "%(filename)s",LINE %(lineno)-4d : %(levelname)-8s %(message)s"
    # 日期格式化方式
    DATE_FORMAT = "%Y-%m-%d %H:%M:%S"
    def initLogging(logFilename):
       
        #logging.basicConfig(filename=logFilename, level=logging.DEBUG, format=LOG_FORMAT)
        #formatter = logging.Formatter(LOG_FORMAT);
    
        #handler=logging.FileHandler(logFilename)
        #handler.setLevel(logging.DEBUG)
        #handler.setFormatter(formatter)
        #logging.getLogger('').addHandler(handler);
    
        logging.basicConfig(stream=sys.stdout, level=logging.DEBUG, format=LOG_FORMAT, datefmt=DATE_FORMAT)
    
        #console = logging.StreamHandler();
        #console.setLevel(logging.INFO);
        #console.setFormatter(formatter);
        #logging.getLogger('').addHandler(console);
        
        
    
    initLogging("mylog.txt")
    
    logging.info("Hello world")
    
    

    只打印到文件

    # coding:utf-8
    import logging
    import sys
    
    # 日志格式化方式
    #LOG_FORMAT = "%(asctime)s - %(levelname)s - %(message)s"
    
    LOG_FORMAT = "%(asctime)s	File "%(filename)s",LINE %(lineno)-4d : %(levelname)-8s %(message)s"
    # 日期格式化方式
    DATE_FORMAT = "%Y-%m-%d %H:%M:%S"
    def initLogging(logFilename):
       
        logging.basicConfig(filename=logFilename, level=logging.DEBUG, format=LOG_FORMAT)
        #formatter = logging.Formatter(LOG_FORMAT);
    
        #handler=logging.FileHandler(logFilename)
        #handler.setLevel(logging.DEBUG)
        #handler.setFormatter(formatter)
        #logging.getLogger('').addHandler(handler);
    
        #logging.basicConfig(stream=sys.stdout, level=logging.DEBUG, format=LOG_FORMAT, datefmt=DATE_FORMAT)
    
        #console = logging.StreamHandler();
        #console.setLevel(logging.INFO);
        #console.setFormatter(formatter);
        #logging.getLogger('').addHandler(console);
        
        
    
    initLogging("mylog.txt")
    
    logging.info("Hello world")
    
    

    同时打印到控制台和文件

    # coding:utf-8
    import logging
    import sys
    
    # 日志格式化方式
    #LOG_FORMAT = "%(asctime)s - %(levelname)s - %(message)s"
    
    LOG_FORMAT = "%(asctime)s	File "%(filename)s",LINE %(lineno)-4d : %(levelname)-8s %(message)s"
    # 日期格式化方式
    DATE_FORMAT = "%Y-%m-%d %H:%M:%S"
    def initLogging(logFilename):
       
        logging.basicConfig(filename=logFilename, level=logging.DEBUG, format=LOG_FORMAT, datefmt=DATE_FORMAT)
        formatter = logging.Formatter(LOG_FORMAT);
    
        #handler=logging.FileHandler(logFilename)
        #handler.setLevel(logging.DEBUG)
        #handler.setFormatter(formatter)
        #logging.getLogger('').addHandler(handler);
    
        #logging.basicConfig(stream=sys.stdout, level=logging.DEBUG, format=LOG_FORMAT, datefmt=DATE_FORMAT)
    
        console = logging.StreamHandler();
        console.setLevel(logging.INFO);
        console.setFormatter(formatter);
        logging.getLogger('').addHandler(console);
        
        
    
    initLogging("mylog.txt")
    
    logging.info("Hello world")
    
    

    https://www.cnblogs.com/heenhui2016/p/11424154.html

    https://blog.csdn.net/energysober/article/details/53263295

  • 相关阅读:
    SQL SERVER使用技巧集
    WIN32串口编程
    经典FLASH收藏
    Windows下WinsockAPI研究
    数据库连接大全[转自中国站长网]
    VirtualBox自动重启之谜
    写个设置命令的VBS脚本工具。
    VB中KeyCode的取法
    实现串口编程的三种方法
    .NET的命名空间
  • 原文地址:https://www.cnblogs.com/zhangww/p/11757655.html
Copyright © 2011-2022 走看看