17

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?

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)
2
  • show how you used those modules.! Commented Feb 11, 2010 at 12:07
  • I updated the question with the logging code. I tried syslog only in the python interpreter, using "import syslog" and "syslog.syslog('test') Commented Feb 11, 2010 at 12:16

4 Answers 4

28

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

Sign up to request clarification or add additional context in comments.

5 Comments

Yes, but is there a way to use python logging with a syslog handler ?
Yes, through logging.handlers.SysLogHandler. Take a look: docs.python.org/library/…
In OS X 10.8, the default is to save everything has higher priority than LOG_INFO in /var/log/system.log.
Hi, how do exactly parametrize logging.handlers.SysLogHandler? I asked for clarification here, since console can't show human readable messages.
As of MacOS 12.7, this does not seem to work anymore.
1

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

Comments

1

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

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"

Comments

-2

the problem is that the account you are running the script as does not have write permission to /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: message to /var/log/messages

2 Comments

syslog works fine for GNU/Linux, but Mac OS X does not have a "messages" file like Linux, I tried to find some file where the log events where recorded, but without luck. It has /var/log/system.log but it does not record what syslog sends.
awfully late response here I know, but on mac /var/log/system.log does indeed record what you send via syslog, it's just filtering low priority messages out by default. try for example: syslog.syslog(syslog.LOG_ERR, "message")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.