zoukankan      html  css  js  c++  java
  • python之logging模块

     

    from http://blog.csdn.net/yatere/article/details/6655445

    1.直接打印基本信息

    import logging
    logging.info('this is info message')
    logging.debug('this is debug message')
    logging.warning('this is warning message')

    优先级CRITICAL > ERROR > WARNING > INFO > DEBUG > NOTSET

    2.使用logging配置logging.basicConfig函数对日志的输出格式及方式做相关配置#logging,basicConfig参数:
     level:日志允许的最底级别

     format:输出内容和格式,有以下格式
     %(levelno)s: 打印日志级别的数值
     %(levelname)s: 打印日志级别名称
     %(pathname)s: 打印当前执行程序的路径,其实就是sys.argv[0]
     %(filename)s: 打印当前执行程序名
     %(funcName)s: 打印日志的当前函数
     %(lineno)d: 打印日志的当前行号
     %(asctime)s: 打印日志的时间
     %(thread)d: 打印线程ID
     %(threadName)s: 打印线程名称
     %(process)d: 打印进程ID
     %(message)s: 打印日志信息

     datefmt:时间格式
     filename:日志名称
     filemode: 日志打开模式 'w'或'a'
     stream: 指定将日志的输出流,可以指定输出到sys.stderr,sys.stdout或者文件,默认输出到sys.stderr,当stream和filename同时指定时,stream被忽略

    import logging
     logging.basicConfig(level=logging.DEBUG, format='%(pathname)s %(filename)s %(asctime)s %(levelname)s %(message)s',
     datefmt='%a, %d %b %Y %H:%M:%S',
     filename ='myblog.log',
     filemode='w'
     )
    
    logging.info('this is info message')
    logging.debug('this is debug message')
    logging.warning('this is warning message')

     在myblog.log中打印出来:
     test.py test.py Tue, 18 Jul 2017 14:43:18 INFO this is info message
     test.py test.py Tue, 18 Jul 2017 14:43:18 DEBUG this is debug message
     test.py test.py Tue, 18 Jul 2017 14:43:18 WARNING this is warning message


     3.将日志同时输出在文件和屏幕上

    import logging
    logging.basicConfig(level=logging.DEBUG, format='%(pathname)s %(filename)s %(asctime)s %(levelname)s %(message)s',
           datefmt='%a, %d %b %Y %H:%M:%S',
           filename ='myblog.log',
           filemode='w'
          )
    
    # 定义一个StreamHandler,将INFO及以上打印到标准错误,并且添加到日志对象
    console = logging.StreamHandler()
    console.setLevel(logging.INFO)
    formatter = logging.Formatter('%(name)-12s:%(levelname)-8s %(message)s')
    console.setFormatter(formatter)
    logging.getLogger('').addHandler(console)

    4.logging日志回滚

    import logging
    # from logging.handlers import RotatingFileHandler
    
    #################################################################################################
    #定义一个RotatingFileHandler,最多备份5个日志文件,每个日志文件最大10M
    Rthandler =RotatingFileHandler('myapp.log',maxBytes=10*1024*1024,backupCount=5)
    Rthandler.setLevel(logging.INFO)
    formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
    Rthandler.setFormatter(formatter)
    logging.getLogger('').addHandler(Rthandler)
    ############################################


    5.通过logging.config模块配置日志

    #logger.conf
    ###############################################
    [loggers]
    keys=root,example01,example02
    [logger_root]
    level=DEBUG
    handlers=hand01,hand02
    [logger_example01]
    handlers=hand01,hand02
    qualname=example01
    propagate=0
    [logger_example02]
    handlers=hand01,hand03
    qualname=example02
    propagate=0
    ###############################################
    [handlers]
    keys=hand01,hand02,hand03
    [handler_hand01]
    class=StreamHandler
    level=INFO
    formatter=form02
    args=(sys.stderr,)
    [handler_hand02]
    class=FileHandler
    level=DEBUG
    formatter=form01
    args=('myapp.log', 'a')
    [handler_hand03]
    class=handlers.RotatingFileHandler
    level=INFO
    formatter=form02
    args=('myapp.log', 'a', 10*1024*1024, 5)
    ###############################################
    [formatters]
    keys=form01,form02
    [formatter_form01]
    format=%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s
    datefmt=%a, %d %b %Y %H:%M:%S
    [formatter_form02]
    format=%(name)-12s: %(levelname)-8s %(message)s
    datefmt=
    

      

    上例3:

    import logging
    import logging.config

    logging.config.fileConfig("logger.conf")
    logger = logging.getLogger("example01")

    logger.debug('This is debug message')
    logger.info('This is info message')
    logger.warning('This is warning message')

  • 相关阅读:
    电脑开机小键盘灯不亮,应该怎么设置?
    关于SqlServer数据库日期函数
    SqlServer数据库几种字段类型对比
    如何查找计算机端口?
    实用的几个小命令
    SqlServer中创建Oracle连接服务器
    局域网内设置打印机
    SqlServer2008数据库自动备份设置
    文件内容替换BAT
    Dockerfile文件与常用指令详解(2) Marathon
  • 原文地址:https://www.cnblogs.com/xiangshigang/p/7200911.html
Copyright © 2011-2022 走看看