python输出日志到控制台和文件

使用配制文件的方法,配制文件logging.conf的内容如下:


[handlers]

keys=fileHandler,consoleHandler

[formatters]
keys=fileFormatter,consoleFormatter

[logger_root]
level=DEBUG
handlers=fileHandler

[logger_fileExample]
level=DEBUG
handlers=fileHandler
qualname=fileExample
propagate=0

[handler_fileHandler]
class=FileHandler
level=DEBUG
formatter=fileFormatter
filemode='w'
args=("myapp.log", "a")

[logger_consoleExample]
level=DEBUG
handlers=consoleHandler
qualname=consoleExample
propagate=0

[handler_consoleHandler]
class=StreamHandler
level=DEBUG
formatter=consoleFormatter
args=(sys.stdout,)

[formatter_fileFormatter]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
datefmt=

[formatter_consoleFormatter]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
datefmt=

这个配制文件同时指明了输出到控制台和文件的参数。

测试代码如下:

 
import logging
import logging.config

logging.config.fileConfig('logging.conf')

logger1 = logging.getLogger('fileExample')
logger1.debug('debug message')

logger2 = logging.getLogger('consoleExample')
logger2.debug('debug message')


 

参考资料:

1.python官方文档https://docs.python.org/2/howto/logging.html#logging-advanced-tutorial

2.python 的日志logging模块学习 http://blog.csdn.net/yatere/article/details/6655445

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值