logger.py
#!/usr/bin/python
# -*- encoding: utf-8 -*-
import os
import sys
import syslog
#---------------------------------------------------------------
# CLASS LOGGER
#---------------------------------------------------------------
class Logger(object):
'''
Interface summary:
from logger import Logger
log = Logger()
log.set_facility('local3')
log.set_priority('info')
log.logging('hoge')
'''
def __init__(self) :
self.proc = os.path.basename(sys.argv[0])
self.pid = syslog.LOG_PID
self.facility = syslog.LOG_LOCAL0
self.priority = syslog.LOG_INFO
def set_facility(self, facility) :
'''
--- facility ---
kern, user, mail, daemon, auth, security, syslog, lpr,news
uudp, cron, authpriv, ftp, local0, local1, local2, local3
local4, local5, local6, local7
----------------
'''
if facility == 'kern' : self.facility = syslog.LOG_KERN
elif facility == 'user' : self.facility = syslog.LOG_USER
elif facility == 'mail' : self.facility = syslog.LOG_MAIL
elif facility == 'daemon' : self.facility = syslog.LOG_DAEMON
elif facility == 'auth' : self.facility = syslog.LOG_AUTH
elif facility == 'security' : self.facility = syslog.LOG_AUTH
elif facility == 'syslog' : self.facility = syslog.LOG_SYSLOG
elif facility == 'lpr' : self.facility = syslog.LOG_LPR
elif facility == 'news' : self.facility = syslog.LOG_NEWS
elif facility == 'uudp' : self.facility = syslog.LOG_UUCP
elif facility == 'cron' : self.facility = syslog.LOG_CRON
elif facility == 'authpriv' : self.facility = 80
elif facility == 'ftp' : self.facility = 88
elif facility == 'local0' : self.facility = syslog.LOG_LOCAL0
elif facility == 'local1' : self.facility = syslog.LOG_LOCAL1
elif facility == 'local2' : self.facility = syslog.LOG_LOCAL2
elif facility == 'local3' : self.facility = syslog.LOG_LOCAL3
elif facility == 'local4' : self.facility = syslog.LOG_LOCAL4
elif facility == 'local5' : self.facility = syslog.LOG_LOCAL5
elif facility == 'local6' : self.facility = syslog.LOG_LOCAL6
elif facility == 'local7' : self.facility = syslog.LOG_LOCAL7
return self.facility
def set_priority(self, priority) :
'''
--- priority ---
emerg, alert,crit, error, warning, notice, info, debug
----------------
'''
if priority == 'emerg' : self.priority = syslog.LOG_EMERG
elif priority == 'alert' : self.priority = syslog.LOG_ALERT
elif priority == 'crit' : self.priority = syslog.LOG_CRIT
elif priority == 'error' : self.priority = syslog.LOG_ERR
elif priority == 'warning' : self.priority = syslog.LOG_WARNING
elif priority == 'notice' : self.priority = syslog.LOG_NOTICE
elif priority == 'info' : self.priority = syslog.LOG_INFO
elif priority == 'debug' : self.priority = syslog.LOG_DEBUG
return self.priority
def logging(self, message) :
'''
output to syslog
'''
syslog.openlog(self.proc, self.pid, self.facility)
syslog.syslog(self.priority, message.replace('\n',' '))
syslog.closelog()
#---------------------------------------------------------------
# MAIN
#---------------------------------------------------------------
def main():
'''
TEST
'''
log = Logger()
log.set_facility('local3')
log.set_priority('info')
log.logging('hoge')
log.logging('foo')
if __name__ == '__main__': main()
Recommended Posts