在C扩展中使用Python的logging模块

1 投票
1 回答
1195 浏览
提问于 2025-04-17 04:38

我正在编写一个Python脚本,并使用Python的日志模块将一些信息写入日志文件(可以查看 http://docs.python.org/howto/logging.html)。

这里有一个小例子(同样来自上面提到的页面):

import logging
import logging.config

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

# create logger
logger = logging.getLogger('simpleExample')

# 'application' code
logger.info('info message')
logger.error('error message')

现在我想为Python创建一个C扩展,并希望在我的程序中导入它。

我的问题是:我该如何从我的C代码中写入日志文件呢?有没有办法在Python的C扩展中使用日志模块?

谢谢大家的评论!

1 个回答

2

这个“非常高级的C接口”提供了一些工具,可以让你从C语言中执行纯Python代码。你可以先看看这个链接:http://docs.python.org/c-api/veryhigh.html#PyRun_String

撰写回答