| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- # -*- coding: UTF-8 -*-
- import logging
- # logging.info('老弟,来了') # 不打印,info级别
- # logging.warning('来错地方来') # 输出屏幕,警告级别
- logging.basicConfig(filename='myapp.log',level=logging.INFO)
- logging.info('info msg')
- logging.warning('WR msg')
- logging.debug('DUG msg')
- #======================= 示例 =========================#
- # -*- encoding:utf-8 -*-
- import logging
- # create logger
- logger_name = "example"
- logger = logging.getLogger(logger_name)
- logger.setLevel(logging.DEBUG)
- # create file handler
- log_path = "./log.log"
- fh = logging.FileHandler(log_path)
- fh.setLevel(logging.WARN)
- # create formatter
- fmt = "%(asctime)-15s %(levelname)s %(filename)s %(lineno)d %(process)d %(message)s"
- datefmt = "%a %d %b %Y %H:%M:%S"
- formatter = logging.Formatter(fmt, datefmt)
- # add handler and formatter to logger
- fh.setFormatter(formatter)
- logger.addHandler(fh)
- # print log info
- logger.debug('debug message')
- logger.info('info message')
- logger.warn('warn message')
- logger.error('error message')
- logger.critical('critical message')
- """
- ##### 文件配置
- 配置文件logging.conf如下:
- ```[loggers]
- keys=root,example01
- [logger_root]
- level=DEBUG
- handlers=hand01,hand02
- [logger_example01]
- handlers=hand01,hand02
- qualname=example01
- propagate=0
- [handlers]
- keys=hand01,hand02
- [handler_hand01]
- class=StreamHandler
- level=INFO
- formatter=form02
- args=(sys.stderr,)
- [handler_hand02]
- class=FileHandler
- level=DEBUG
- formatter=form01
- args=('log.log', 'a')
- [formatters]
- keys=form01,form02
- [formatter_form01]
- format=%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s```
- 使用程序logger.py如下:
- ```#!/usr/bin/python
- # -*- encoding:utf-8 -*-
- import logging
- import logging.config
- logging.config.fileConfig("./logging.conf")
- # create logger
- logger_name = "example"
- logger = logging.getLogger(logger_name)
- logger.debug('debug message')
- logger.info('info message')
- logger.warn('warn message')
- logger.error('error message')
- logger.critical('critical message')
- """
- ##### 字典配置
- # 有兴趣的童靴可以使用```logging.config.dictConfig(config)```编写一个示例程序发给我,以提供给我进行完善本文。
- ##### 监听配置
- # 有兴趣的童靴可以使用```logging.config.listen(port=DEFAULT_LOGGING_CONFIG_PORT)```编写一个示例程序发给我,以提供给我进行完善本文。
- # 更多详细内容参考[logging.config日志配置](http://python.usyiyi.cn/python_278/library/logging.config.html#module-logging.config)
|