使用 PyObjC 时如何使 NSLog 与 Python 的日志记录模块一起工作?
我正在编写一个基于 Django 的 Web 应用程序,它通过 PyObjC 导入 Cocoa 框架。 Cocoa 框架中散布着 NSLog()
,虽然我可以在非守护进程模式下运行 Django 服务器时看到它们,但一旦我进入守护进程,我就会丢失所有这些有用的 NSLog()
输出。
有没有什么简单的方法可以让 NSLog 内容冒泡到 Python logging
模块的世界中,以便它可以与实际 Python 代码发出的日志消息合并?
做了一点谷歌搜索,似乎你可能必须重定向 stderr 并以某种方式将其吸回到 Python 中才能实现这一目标,这将是一种无赖......
非常感谢任何帮助。
I'm writing a Django-based webapp that imports a Cocoa framework via PyObjC. The Cocoa framework has NSLog()
littered all through it and while I can see them when running the Django server in non-daemon mode, as soon as I go to daemon I simply lose all this useful NSLog()
output.
Is there any easy way to get NSLog stuff to bubble up into the Python logging
module's world so it can be merged in with the log messages being emitted by the actual Python code?
Did a little Googling and it seems like you might have to redirect stderr and somehow suck it back into Python in order to achieve this, which would be kind of a bummer ...
Any help much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据this页面,NSLog基本上是这样工作的
,所以你确实需要捕获/重定向标准错误输出。我写了一篇帖子一些不久前这可能对仅使用 Python 的程序有帮助,但我猜测 Cocoa 代码在幕后访问进程级文件描述符 2 (
stderr
)。因此,您需要对进程的stderr
进行一些低级的调整。下面是一个示例:一旦有了
fd
,您就可以从中创建一个类似文件的对象并将其传递给StreamHandler
,例如,作为合并输出的一种方法来自Python代码和Cocoa代码。According to this page, NSLog basically works like
so you do need to capture / redirect the standard error output. I wrote a post some time ago which might help for Python-only programs, but I would guess that the Cocoa code accesses the process-level file descriptor 2 (
stderr
) under the covers. So you'll need to do some low-level fiddling around with the process'stderr
. Here's an example:Once you have
fd
, you can create a file-like object out of it and pass it toStreamHandler
, for example, as a means to merging the output from Python code and Cocoa code.