示例#1
0
def setup_logging(option_group):
    """Sets up logging in a syslog format by log level
    :param option_group: options as returned by the OptionParser
    """
    stderr_log_format = "%(levelname) -10s %(asctime)s %(funcName) -20s line:%(lineno) -5d: %(message)s"
    file_log_format = "%(asctime)s - %(levelname)s - %(message)s"
    logger = logging.getLogger()
    if option_group.debug:
        logger.setLevel(level=logging.DEBUG)
    elif option_group.verbose:
        logger.setLevel(level=logging.INFO)
    else:
        logger.setLevel(level=logging.WARNING)

    handlers = []
    if option_group.syslog:
        handlers.append(logging.SyslogHandler(facility=option_group.syslog))
        # Use standard format here because timestamp and level will be added by syslogd.
    if option_group.logfile:
        handlers.append(logging.FileHandler(option_group.logfile))
        handlers[0].setFormatter(logging.Formatter(file_log_format))
    if not handlers:
        handlers.append(logging.StreamHandler())
        handlers[0].setFormatter(logging.Formatter(stderr_log_format))
    for handler in handlers:
        logger.addHandler(handler)
def setup_logging(option_group):
    """Sets up logging in a syslog format by log level
    :param option_group: options as returned by the OptionParser
    """
    stderr_log_format = "%(levelname) -8s %(asctime)s %(funcName)s line:%(lineno)d: %(message)s"
    file_log_format = "%(asctime)s - %(levelname)s - %(message)s"
    logger = logging.getLogger()
    if option_group.debug:
        logger.setLevel(level=logging.DEBUG)
    elif option_group.verbose:
        logger.setLevel(level=logging.INFO)
    else:
        logger.setLevel(level=logging.WARNING)

    # First, clear out any default handlers
    for handler in logger.handlers:
        logger.removeHandler(handler)

    if option_group.syslog:
        # Use standard format here because timestamp and level will be added by
        # syslogd.
        logger.addHandler(logging.SyslogHandler(facility=option_group.syslog))
    if option_group.logfile:
        logger.addHandler(logging.FileHandler(option_group.logfile))
        logger.handlers[-1].setFormatter(logging.Formatter(file_log_format))
    if not logger.handlers:
        logger.addHandler(logging.StreamHandler())
        logger.handlers[-1].setFormatter(logging.Formatter(stderr_log_format))
    return
示例#3
0
    def setup_logging(self):
        root_logger = logging.getLogger()

        if self.args.debug:
            root_logger.setLevel(logging.DEBUG)
        else:
            root_logger.setLevel(logging.INFO)

        if not self.args.quiet:
            h = logging.StreamHandler()
            if self.args.journald:
                h.setFormatter(JournaldFormatter())
            else:
                h.setFormatter(
                    logging.Formatter(
                        "%(asctime)-15s %(levelname)s %(message)s"))
            if self.args.debug:
                h.setLevel(logging.DEBUG)
            elif self.args.verbose:
                h.setLevel(logging.INFO)
            else:
                h.setLevel(logging.WARN)
            h.addFilter(DefaultLogFilter(self.args.accesslog))
            root_logger.addHandler(h)

        if self.args.accesslog:
            from logging.handlers import WatchedFileHandler
            h = WatchedFileHandler(self.args.accesslog)
            h.setFormatter(logging.Formatter("%(message)s"))
            h.setLevel(logging.INFO)

            class Filter:
                def filter(self, record):
                    if not getattr(record, "access_log", False):
                        return False
                    return record.levelno < logging.WARN

            h.addFilter(Filter())
            root_logger.addHandler(h)

        if self.args.perflog:
            from logging.handlers import WatchedFileHandler
            h = WatchedFileHandler(self.args.perflog)

            class PerfFormatter(logging.Formatter):
                def format(self, record):
                    import json
                    info = getattr(record, "perf", None)
                    return json.dumps(info, sort_keys=True)

            h.setFormatter(PerfFormatter())
            h.setLevel(logging.INFO)

            class Filter:
                def filter(self, record):
                    return getattr(record, "perf", None) is not None

            h.addFilter(Filter())
            root_logger.addHandler(h)

        if self.args.errorlog:
            from logging.handlers import WatchedFileHandler
            h = WatchedFileHandler(self.args.errorlog)
            h.setFormatter(
                logging.Formatter("%(asctime)-15s %(levelname)s %(message)s"))
            h.setLevel(logging.WARN)
            root_logger.addHandler(h)

        if self.args.syslog:
            h = logging.SyslogHandler()
            h.setFormatter(
                logging.Formatter("%(asctime)-15s %(levelname)s %(message)s"))
            root_logger.addHandler(h)