zoukankan      html  css  js  c++  java
  • Python日志(logging)模块使用方法简介

     

    A logger is configured to have a log level. This log level describes the severity of the messages that the logger will handle. Python defines the following log levels:

    § DEBUG: Low level system information for debugging purposes

    § INFO: General system information

    § WARNING: Information describing a minor problem that has occurred.

    § ERROR: Information describing a major problem that has occurred.

    § CRITICAL: Information describing a critical problem that has occurred.

    Each message that is written to the logger is a Log Record. Each log record also has a log level indicating the severity of that specific message. A log record can also contain useful metadata that describes the event that is being logged. This can include details such as a stack trace or an error code.

    When a message is given to the logger, the log level of the message is compared to the log level of the logger. If the log level of the message meets or exceeds the log level of the logger itself, the message will undergo further processing. If it doesnt, the message will be ignored.

    Once a logger has determined that a message needs to be processed, it is passed to a Handler.

    (来源:https://docs.djangoproject.com/en/1.4/topics/logging/ )

    简单的范例

    #-*- coding: utf-8 -*-

    import logging

    """logging的简单使用示例

    """

    if __name__ == '__main__':

        

        # 最简单的用法,不推荐直接用logging模块

        logging.debug(u'这个不会被打印出来')

        logging.info(u'这个也不会被打印出来')

        logging.warning(u'这个就会了,因为logging默认的级别是WARNING,低于这个等级的信息就会被忽略')

    稍微难一点的范例

    #-*- coding: utf-8 -*-

    import logging

    """logging的使用示例

    """

    if __name__ == '__main__':

        

        # python官方文档中提供的一段示例,使用logging模块产生logger对象

        FORMAT = '%(asctime)-15s %(clientip)s %(user)-8s %(message)s'

        logging.basicConfig(format = FORMAT)

        d = { 'clientip' : '192.168.0.1', 'user' : 'fbloggs' }

        # 创建一个日志对象

        logger = logging.getLogger('tcpserver')

        logger.warning('Protocol problem: %s', 'connection reset', extra = d)

        # 设置级别

        logger.setLevel(logging.DEBUG)   

        logger.debug('Hello', extra = d)

    复杂一点的用法,为logger添加了若干个handler

    #-*- coding: utf-8 -*-

    import logging, logging.config

    """logging的使用示例

    """

    if __name__ == '__main__':

        

        # 比较复杂的用法

        LOGGING = {

                   # 版本,总是1

                   'version': 1,

                   'disable_existing_loggers': True,

                   'formatters': {

                                  'verbose': {'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'},

                                   'simple': {'format': '%(levelname)s %(message)s'},

                                   'default': {

                                               'format' : '%(asctime)s %(message)s',

                                               'datefmt' : '%Y-%m-%d %H:%M:%S'

                                    }

                    },

                   'handlers': {

                                'null': {

                                         'level':'DEBUG',

                                         'class':'logging.NullHandler',

                                },

                                'console':{

                                           'level':'DEBUG',

                                           'class':'logging.StreamHandler',

                                           'formatter': 'default'

                                },

                                'file': {

                                         'level': 'INFO',

                                         # TimedRotatingFileHandler会将日志按一定时间间隔写入文件中,并

                                         # 将文件重命名为'原文件名+时间戮'这样的形式

                                         # Python提供了其它的handler,参考logging.handlers

                                         'class': 'logging.handlers.TimedRotatingFileHandler',

                                         'formatter': 'default',

                                         # 后面这些会以参数的形式传递给TimedRotatingFileHandler的

                                         # 构造器

                                         # filename所在的目录要确保存在

                                         'filename' : 'log',

                                         # 每5分钟刷新一下

                                         'when' : 'M',

                                         'interval' : 1,

                                         'encoding' : 'utf8',

                                }

                    },

                   'loggers' : {

                                # 定义了一个logger

                                'mylogger' : {

                                              'level' : 'DEBUG',

                                              'handlers' : ['console', 'file'],

                                              'propagate' : True

                                }

                    } 

        }

        logging.config.dictConfig(LOGGING)

        logger = logging.getLogger('mylogger')

        logger.info('Hello')

        

    另外,还可以通过配置文件来配置logging,方法如下:

    配置文件(log_conf):

    [loggers]

    keys=root,mylogger

    [handlers]

    keys=null,console,file

    [formatters]

    keys=verbose,simple,default

    [formatter_verbose]

    format=%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s

    datefmt=

    class=logging.Formatter

    [formatter_simple]

    format=%(levelname)s %(message)s

    datefmt=

    class=logging.Formatter

    [formatter_default]

    format=%(asctime)s %(message)s

    datefmt=%Y-%m-%d %H:%M:%S

    class=logging.Formatter

    [logger_mylogger]

    level=DEBUG

    handlers=console,file

    propagate=1

    qualname=

    [logger_root]

    level=NOTSET

    handlers=

    [handler_null]

    class=NullHandler

    level=DEBUG

    args=()

    [handler_console]

    class=StreamHandler

    level=DEBUG

    args=()

    [handler_file]

    class=handlers.TimedRotatingFileHandler

    level=INFO

    formatter=default

    args=('log','M',1,0,'utf8')

    Python代码:

    #-*- coding: utf-8 -*-

    import logging, logging.config

    """logging的使用示例

    """

    if __name__ == '__main__':

        

        # 使用配置文件

        logging.config.fileConfig('log_conf')

        logger = logging.getLogger('mylogger')

        logger.info('Hello')

  • 相关阅读:
    [分享] Knockout.js 体验之旅
    [分享] 用NodeJs做一个小爬虫,附源码!
    [分享] 纯CSS完美实现垂直水平居中的6种方式
    [分享] 来,做一个问卷调查(有抽奖!)
    除非你是BAT,前端开发中最好少造轮子
    [分享] 微信支付中商户对个人用户付款案例
    30分钟做一个二维码名片应用,有源码!
    【分享】WeX5的正确打开方式(7)——数据组件详解
    基于phonegap,html5,ratchet,handlebars等技术的微表情APP
    html5实现web app摇一摇换歌
  • 原文地址:https://www.cnblogs.com/jkred369/p/6654988.html
Copyright © 2011-2022 走看看