Esempio n. 1
0
def get_instance(plugin):
    name = plugin.get_name()
    logger.info("Get a Syslog broker for plugin %s" % (name))

    # syslog.syslog priority defaults to (LOG_INFO | LOG_USER)
    facility = syslog.LOG_USER
    priority = syslog.LOG_INFO

    # Get configuration values, if any
    if hasattr(plugin, 'facility'):
        facility = plugin.facility
    if hasattr(plugin, 'priority'):
        priority = plugin.priority

    # Ensure config values have a string type compatible with
    # SysLogHandler.encodePriority
    if type(facility) in types.StringTypes:
        facility = types.StringType(facility)
    if type(priority) in types.StringTypes:
        priority = types.StringType(priority)

    # Convert facility / priority (integers or strings) to aggregated
    # priority value
    sh = SysLogHandler()
    try:
        priority = sh.encodePriority(facility, priority)
    except TypeError, e:
        logger.error("[%s] Couldn't get syslog priority, "
                     "reverting to defaults" % (name))
Esempio n. 2
0
    def __init__(self,
                 name=__name__,
                 syslog=False,
                 console=False,
                 logfile=False,
                 logfile_size=0,
                 logfile_keep=0,
                 loglevel="info",
                 encoded_priorities={"daemon": "alert"}):
        super(Logger, self).__init__(name,
                                     LOGLEVELS.get(loglevel, logging.INFO))

        if syslog:
            if sys.platform in ('win32', 'cygwin'):
                getDefaultLogger().warning("Current platform does " +
                                           "not support syslog")
            else:
                if syslog is True:
                    syslog = self.get_syslog_socket()

                handler = SysLogHandler(address=syslog)
                for k, v in encoded_priorities.items():
                    handler.encodePriority(k, v)
                handler.setFormatter(logging.Formatter(fmt=LOG_FORMAT))
                self.addHandler(handler)

        if logfile:
            handler = RotatingFileHandler(logfile,
                                          maxBytes=logfile_size,
                                          backupCount=logfile_keep)
            handler.setFormatter(logging.Formatter(fmt=LOG_FORMAT))
            self.addHandler(handler)

        if console:
            handler = StreamHandler(sys.stderr)
            handler.setFormatter(logging.Formatter(fmt=LOG_FORMAT))
            self.addHandler(handler)

        self.addHandler(NullHandler())
Esempio n. 3
0
    def __init__(self,
                 name=__name__,
                 syslog=False,
                 console=False,
                 logfile=False,
                 logfile_size=0,
                 logfile_keep=0,
                 loglevel="info",
                 encoded_priorities={"daemon": "alert"}):
        super(Logger, self).__init__(name, LOGLEVELS.get(loglevel,
                                                         logging.INFO))

        if syslog:
            if sys.platform in ('win32', 'cygwin'):
                getDefaultLogger().warning("Current platform does " +
                                           "not support syslog")
            else:
                if syslog is True:
                    syslog = self.get_syslog_socket()

                handler = SysLogHandler(address=syslog)
                for k, v in encoded_priorities.items():
                    handler.encodePriority(k, v)
                handler.setFormatter(logging.Formatter(fmt=LOG_FORMAT))
                self.addHandler(handler)

        if logfile:
            handler = RotatingFileHandler(logfile,
                                          maxBytes=logfile_size,
                                          backupCount=logfile_keep)
            handler.setFormatter(logging.Formatter(fmt=LOG_FORMAT))
            self.addHandler(handler)

        if console:
            handler = StreamHandler(sys.stderr)
            handler.setFormatter(logging.Formatter(fmt=LOG_FORMAT))
            self.addHandler(handler)

        self.addHandler(NullHandler())
    def run(self, message, facility, priority):

        host = self.config['server']
        port = self.config['port']
        protocol = self.config['protocol']

        syslog_proto = {
            "tcp": socket.SOCK_STREAM,
            "udp": socket.SOCK_DGRAM
        }.get(protocol.lower(), "udp")

        handler = SysLogHandler(
            address=(host, port),
            facility=SysLogHandler.facility_names.get(facility, "user"),
            socktype=syslog_proto
        )
        formatter = logging.Formatter('%(module)s[%(process)d]: %(message)s')
        handler.setFormatter(formatter)
        handler.encodePriority(facility, priority)
        log.addHandler(handler)

        log.info(message)