I'm pretty new to Python programming so I have this question:
How can I log a Python application activity into /var/log with Mac OS X?
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.
How can I do it?
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")
ch = logging.FileHandler(LOG_FILENAME)
format = logging.Formatter(LOG_FORMAT)
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
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
You can use the command line tool "syslog" on os x, to get all syslog events.
Here's a guide with about 5 steps you need to complete - they're straightforward, and it worked for me:
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"
the problem is that the account you are running the script as does not have write permission to
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: message to