Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to log python program activity in Mac OS X

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)
like image 551
CastleDweller Avatar asked Feb 11 '10 11:02

CastleDweller


1 Answers

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

like image 119
CastleDweller Avatar answered Oct 02 '22 23:10

CastleDweller