python写日志
2016-05-10 09:51
453 查看
1.默认日志级别是ERROR
但是你会发现debug()和info()方法没有显示任何信息,这是因为默认的日志级别是ERROR
,所以低于此级别的日志不会记录。
2.自定义logging规则
只需要派生BaseRotatingHandler类就可以了,如
class TestFileHandler(BaseRotatingHandler):
.....
hdlr = TestFileHandler(filename=fname, maxBytes=maxsize, backupCount=backcount)
fmt = "[%(asctime)s] [%(levelname)s] "\
"\[ %(filename)s:%(lineno)s - %(name)s ] %(message)s "
formatter = logging.Formatter(fmt)
hdlr.setFormatter(formatter)
logger = logging.getLogger()
logger.addHandler(hdlr)
return logger
但是你会发现debug()和info()方法没有显示任何信息,这是因为默认的日志级别是ERROR
,所以低于此级别的日志不会记录。
2.自定义logging规则
只需要派生BaseRotatingHandler类就可以了,如
class TestFileHandler(BaseRotatingHandler):
.....
hdlr = TestFileHandler(filename=fname, maxBytes=maxsize, backupCount=backcount)
fmt = "[%(asctime)s] [%(levelname)s] "\
"\[ %(filename)s:%(lineno)s - %(name)s ] %(message)s "
formatter = logging.Formatter(fmt)
hdlr.setFormatter(formatter)
logger = logging.getLogger()
logger.addHandler(hdlr)
return logger
相关文章推荐
- Python已存在对象中如何添加额外功能
- 关于MySQL-python-1.2.3.tar.gz安装失败的解决方案
- 关于MySQL-python-1.2.3.tar.gz安装失败的解决方案
- Python小记
- python字符串的不变性
- Python 解析配置模块之ConfigParser详解
- python对字符串操作
- python模块之itertools
- Python发邮件如何设置多人接收
- Python debug —— 逻辑错误(四)
- Python3控制路由器――使用requests重启极路由.py
- Python再接触
- python学习——Python join()函数
- Python之路,Day1 - Python基础1
- python urllib2
- python 变量命名规范
- python设计模式(一)--简单工厂(上)
- python设计模式(一)--简单工厂(中)
- python占位符
- Python语言实现手机号归属地的方法