My Python & co. stuff

Archive for the ‘Logging’ Category

Python has it’s own logging. Twisted has it’s own logging. Unfortunatley, none of them have a convinient way to log to underlying operating system’s facilities (which is, syslog or Windows Event Log in my case). So, I had to wrote one myself. Here you go – this is MultiLog:

class MultiLog(object):
class MultiLog(object): """ Multiplatform logging component. (C) 2009 FHU KAGAMI <> Distributed under the terms of MIT License """ def __init__(self, appname, interactive = None): self.hostname = socket.gethostname() self.appname = appname self.interactive = interactive if self.interactive is None: self.interactive = '--interactive' in sys.argv = os.getpid() fun = '_init_log_' + sys.platform getattr(self, fun)() def log(self, msg): if type(msg)==unicode: msg = msg.encode('utf8') fun = '_log_' + sys.platform return getattr(self, fun)(msg) __call__ = log def _log_interactive(self, msg): if self.interactive: msg = time.ctime() + ": " + self.hostname + " " + self.appname + "[" + str( + "]: " + msg sys.stderr.write(msg) sys.stderr.write('\n') sys.stderr.flush() def _init_log_unix(self): from syslog import openlog openlog(self.appname + "[" + str( + "]") def _log_unix(self, msg): from syslog import syslog syslog(msg) self._log_interactive(msg) _init_log_linux2 = _init_log_unix _log_linux2 = _log_unix def _init_log_win32(self): import win32evtlog import win32con self._win32_type = win32con.EVENTLOG_INFORMATION_TYPE self._win32_log = win32evtlog.OpenEventLog(None, self.appname) def _log_win32(self, msg): import win32evtlog win32evtlog.ReportEvent(self._win32_log, self._win32_type, 0, 0, None, (msg, 'PID: %s' %, None) self._log_interactive(msg) if __name__ == "__main__": log = MultiLog('my application') log('this is a test')

On Ubuntu, look for /var/log/user.log. On Windows, run mmc.exe and open eventwvr – look for Application Logs.