macos 如何在 Mac OS X 中记录 python 程序活动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2244153/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 06:44:59  来源:igfitidea点击:

How to log python program activity in Mac OS X

pythonmacoslogging

提问by Oscar Carballal

I'm pretty new to Python programming so I have this question:

我对 Python 编程很陌生,所以我有这个问题:

How can I log a Python application activity into /var/log with Mac OS X?

如何使用 Mac OS X 将 Python 应用程序活动记录到 /var/log 中?

I tried using syslog module, but it does not seem to write anything. I tried also with the logging module, but I always run into a permission error.

我尝试使用 syslog 模块,但它似乎没有写任何东西。我也尝试过使用日志记录模块,但我总是遇到权限错误。

How can I do it?

我该怎么做?

Update:

更新:

import logging
import time
LOG_FILENAME = "/var/log/writeup.log" + time.strftime("%Y-%m-%d")
LOG_FORMAT = "%(asctime)s - %(filename)s - %(levelname)s - %(message)s"
log = logging.getLogger("main.py")
log.setLevel(logging.DEBUG)
ch = logging.FileHandler(LOG_FILENAME)
ch.setLevel(logging.DEBUG)
format = logging.Formatter(LOG_FORMAT)
ch.setFormatter(format)
log.addHandler(ch)

回答by Oscar Carballal

I found the solution. It seems that Mac OS X does not record any log activity lower than LOG_ALERT, so this does the trick

我找到了解决方案。似乎 Mac OS X 没有记录任何低于 LOG_ALERT 的日志活动,所以这就是诀窍

import syslog
# Define identifier
syslog.openlog("Python")
# Record a message
syslog.syslog(syslog.LOG_ALERT, "Example message")

This message is recorded on /var/log/system.log

此消息记录在 /var/log/system.log

回答by Arlukin

You can use the command line tool "syslog" on os x, to get all syslog events.

您可以在 os x 上使用命令行工具“syslog”来获取所有 syslog 事件。

回答by Gene Bo

Here's a guide with about 5 steps you need to complete - they're straightforward, and it worked for me:

这是您需要完成的大约 5 个步骤的指南 - 它们很简单,对我有用:

http://vastdevblog.vast.com/blog/2012/04/18/using-syslogappender-on-os-x/

http://vastdevblog.vast.com/blog/2012/04/18/using-syslogappender-on-os-x/

Pay close attention to this step, as you have to make a few command calls there:

请密切注意这一步,因为您必须在那里进行一些命令调用:

sudo /usr/libexec/PlistBuddy /System/Library/LaunchDaemons/com.apple.syslogd.plist

...

Command: Add :Sockets:NetworkListener dict
Command: Add :Sockets:NetworkListener:SockServiceName string "syslog"
Command: Add :Sockets:NetworkListener:SockType string "dgram"

回答by Kimvais

the problem is that the account you are running the script as does not have write permission to /var/log

问题是您运行脚本的帐户没有写入权限 /var/log

I do not know about OS X specifics, but I guess that syslog.syslog("message")should print something like (if it acts the way it does in Linux) Feb 11 14:27:47 hostname python: messageto /var/log/messages

我不知道 OS X 的具体细节,但我想syslog.syslog("message")应该打印一些类似的东西(如果它像在 Linux 中一样) Feb 11 14:27:47 hostname python: message/var/log/messages