我有兴趣在 C 程序中使用 Python 日志记录模块。我已经尝试阅读http://docs.python.org/2/extending/embedding.html上的文档,但我很困惑。
这就是我想要完成的(Python 等价物):
import logging, logging.handlers
logger = logging.getLogger('')
logger.setLevel(logging.DEBUG)
socketHandler = logging.handlers.SocketHandler('localhost', logging.handlers.DEFAULT_TCP_LOGGING_PORT)
logger.addHandler(socketHandler)
logger.debug("whats up, doc?")
这是我在 C 中尝试的:
PyObject *logging, *get_logger, *pValue, *set_level, *strargs;
*logging = PyImport_ImportModule("logging")
*get_logger = PyObject_GetAttrString(logging, "getLogger")
*pValue = PyObject_CallObject(get_logger, NULL)
*set_level = PyObject_GetAttrString(logging, "setLevel")
*strargs = PyString_FromString("logging.DEBUG")
pValue = PyObject_CallObject(set_level, strargs);
不过,我不确定如何创建 socketHandler。有什么提示吗?谢谢!