免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 11614 | 回复: 12
打印 上一主题 下一主题

[求助]python中logging配置文件的命名空间 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-07-10 15:24 |只看该作者 |倒序浏览
问题描述:
  我想利用logging的配置文件,添加一个新的handler,此handler的输出格式format是自定义的,如XMLLayout


[handlers]
keys = chainsaw

[formatters]
keys = xmllayout

[logger_root]
level = NOTSET
handlers = chainsaw

[handler_chainsaw]
class = xmllayout.RawSocketHandler
args = ('localhost', 444
level = NOTSET
formatter = xmllayout

[formatter_xmllayout]
class = xmllayout.XMLLayout



利用此输出,发送log信息到chainsaw(一个可视性比较好的log viewer)



问题:
python提示:

g.py", line 10, in <module>
  logging.config.fileConfig('log.conf')
  File "E:\Python25\lib\logging\config.py", line 84, in fileConfig
  handlers = _install_handlers(cp, formatters)
  File "E:\Python25\lib\logging\config.py", line 149, in _install_handlers
  klass = eval(klass, vars(logging))
  File "<string>", line 1, in <module>
NameError: name 'xmllayout' is not defined


个人猜想:
因为我直接在py脚本中

import xmllayout

handler = xmllayout.RawSocketHandler('localhost', 444
handler.setFormatter(xmllayout.XMLLayout())
logging.root.addHandler(handler)

是正常的,所以认为,应该是配置文件的命名空间没有xmllayout,故python报错。

但怎样让这个配置文件的命名空间能找到xmllayout呢?还请哪位知道的告知,在线等答案,给分!!!
(ps:我直接在文件"E:\Python25\lib\logging\config.py"中import xmllayout不行, xmllayout模块已正确安装,py脚本中引用没有问题)

论坛徽章:
0
2 [报告]
发表于 2011-07-10 17:53 |只看该作者
自己顶一下

论坛徽章:
0
3 [报告]
发表于 2011-07-10 23:06 |只看该作者
本帖最后由 106033177 于 2011-07-10 23:17 编辑

回复 1# mars6505
没有完全看明白你想表达的意思。
不过推测是有一个模块名写在配置文件里,现在你想导入 可以这样 mod =__import__('xmllaylout')。

论坛徽章:
0
4 [报告]
发表于 2011-07-11 00:10 |只看该作者
回复  mars6505
没有完全看明白你想表达的意思。
不过推测是有一个模块名写在配置文件里,现在你想导入  ...
106033177 发表于 2011-07-10 23:06



    首先,非常感谢您的回复!!!

再感谢您的推测跟我的问题一致!


我在配置文件里按你所说, 添加了

xmllayout = __import__('xmllaylout')

但还是报错:
1。 直接在配置文件添加, 不在[Section_name]下:

g.py", line 10, in <module>
    logging.config.fileConfig('log.conf')
  File "E:\Python25\lib\logging\config.py", line 74, in fileConfig
    cp.read(fname)
  File "E:\Python25\lib\ConfigParser.py", line 267, in read
    self._read(fp, filename)
  File "E:\Python25\lib\ConfigParser.py", line 462, in _read
    raise MissingSectionHeaderError(fpname, lineno, line)
ConfigParser.MissingSectionHeaderError: File contains no section headers.
file: log.conf, line: 1
"xmllayout = __import__('xmllaylout')\n"

2。直接在配置文件添加,在[Section_name]下:

g.py", line 10, in <module>
    logging.config.fileConfig('log.conf')
  File "E:\Python25\lib\logging\config.py", line 84, in fileConfig
    handlers = _install_handlers(cp, formatters)
  File "E:\Python25\lib\logging\config.py", line 149, in _install_handlers
    klass = eval(klass, vars(logging))
  File "<string>", line 1, in <module>
NameError: name 'xmllayout' is not defined

错误依旧,不过还是非常感谢您的帮助。 可能直接在配置文件添加 xmllayout = __import__('xmllaylout') 是不行的,添到.py文件才起作用?

论坛徽章:
0
5 [报告]
发表于 2011-07-11 07:48 |只看该作者
回复 4# mars6505
不是让你写在配置文件里,是在你自己的脚本里用__import__()导入配置文件指定模块。

论坛徽章:
0
6 [报告]
发表于 2011-07-11 13:34 |只看该作者
回复 5# 106033177


    谢谢,不过在.py代码里加入xmllayout = __import__('xmllayout') 或 mod = __import__('xmllayout') 也还是不行

我之前有试过代码中直接 import xmllayout ,再调用解析配置文件,也是不得行。

论坛徽章:
0
7 [报告]
发表于 2011-07-12 11:44 |只看该作者
回复 6# mars6505

给个代码看看吧。

论坛徽章:
0
8 [报告]
发表于 2011-07-12 14:57 |只看该作者
回复 7# 106033177


    test_logging.py
  1. import logging
  2. import logging.config
  3. import xmllayout

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

  5. logging.error('this is error')
  6. logging.info('this is info')
复制代码
log.conf
  1. [loggers]
  2. keys = root

  3. [handlers]
  4. keys = chainsaw

  5. [formatters]
  6. keys = xmllayout

  7. [logger_root]
  8. level = NOTSET
  9. handlers = chainsaw

  10. [handler_chainsaw]
  11. class = xmllayout.RawSocketHandler
  12. args = ('localhost', 4448)
  13. level = NOTSET
  14. formatter = xmllayout

  15. [formatter_xmllayout]
  16. class = xmllayout.XMLLayout
复制代码

论坛徽章:
0
9 [报告]
发表于 2011-07-12 21:54 |只看该作者
回复 8# mars6505

XMLLayout模块你装了没有?

论坛徽章:
0
10 [报告]
发表于 2011-07-13 00:15 |只看该作者
回复 9# 106033177


    装了的,直接在代码里配置(不使用配置文件)是可行的。
  1. import logging
  2. import logging.config
  3. import xmllayout

  4. handler =xmllayout.RawSocketHandler('localhost', 4448)
  5. handler.setFormatter(xmllayout.XMLLayout())
  6. logging.root.addHandler(handler)
  7. logging.getLogger().setLevel(logging.DEBUG)
  8. logging.root.addHandler(logging.StreamHandler())

  9. ##logging.config.fileConfig('log.conf')

  10. logging.error('this is error')
  11. logging.info('this is info')
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP