python syslog wrapper class

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

python syslog wrapper class
python subprocess wrapper class
YOLO Python wrapper class
[Python] class, instance
"Kanrika" python class
About python, class
Python class, instance
#Python basics (class)
Python class (Python learning memo ⑦)
case class in python
[Python] Class inheritance (super)
Python self-made class sort
[python] class basic methods
Method_missing-like wrapper in Python
[Python] Class inheritance, override
Class notation in Python
Python exception class list
Python: Class and instance variables
C / C ++ programmer challenges Python (class)
Python class member scope summary
Python class variables and instance variables
Wrapper running Hadoop in Python
How to write a Python class
perl objects and python class part 2.
Python
[Python of Hikari-] Chapter 09-03 Class (inheritance)
Landmines hidden in Python class variables
Python class definitions and instance handling
"The easiest Python introductory class" modified
Read PNG chunks in Python (class)
class
[Python] Road to snake charmer (3) Python class
Examine the object's class in python
perl objects and python class part 1.
class
[Python] Inherit a class with class variables
Generate a first class collection in Python
Create a Python function decorator with Class
[Introduction to Python] How to use class in Python?
Implement __eq__ etc. generically in Python class
[Learning memo] Basics of class by python
Build a blockchain with Python ① Create a class
[Python] How to make a class iterable
Playing card class in Python (with comparison)
Python basic operation 3rd: Object-oriented and class
[Python] Difference between class method and static method
How to use __slots__ in Python class
Generate a class from a string in Python
Implemented Python wrapper for Qiita API v2
Talking about Python class attributes and metaclasses