Osc*_*lal 14 python macos logging
我是Python编程的新手,所以我有这个问题:
如何使用Mac OS X将Python应用程序活动记录到/ var/log中?
我尝试使用syslog模块,但它似乎没有写任何东西.我也尝试使用日志记录模块,但我总是遇到权限错误.
我该怎么做?
更新:
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)
Run Code Online (Sandbox Code Playgroud)
Osc*_*lal 25
我找到了解决方案.似乎Mac OS X没有记录任何低于LOG_ALERT的日志活动,所以这就行了
import syslog
# Define identifier
syslog.openlog("Python")
# Record a message
syslog.syslog(syslog.LOG_ALERT, "Example message")
Run Code Online (Sandbox Code Playgroud)
此消息记录在/var/log/system.log中
| 归档时间: |
|
| 查看次数: |
10850 次 |
| 最近记录: |