Пример #1
0
def configure_logging(opts, dotaspen, conf):
    """Given an opts object, configure logging.
    
    Logging can be configured from four places, in this order of 
    precedence:
    
      1) some other module (theoretically, I'm not sure an aspen user 
         could make this happen easily)
      2) the command line
      3) a logging.conf file
      4) a [logging] section in the aspen.conf file
    
    These are not layered; only one is used.

    """

    #FMT = "%(process)-6s%(levelname)-9s%(name)-14s%(message)s"

    kw = None
    logging_configured = bool(len(logging.getLogger().handlers))
                         # this test is taken from logging.basicConfig.

    if logging_configured:              # some other module
        log.warn("Logging is already configured, so aspen won't perform "
                 "any logging configuration.")

    if not logging_configured:          # command line
        kw = {'dotaspen': dotaspen}
        kw['filename'] = opts.log_file
        kw['filter'] = opts.log_filter
        kw['format'] = opts.log_format
        kw['level'] = opts.log_level
        if kw.values() != [None, None, None, None]: # at least one knob set
            if kw['format'] is None:
                kw['format'] = LOG_FORMAT
            if kw['level'] is None:
                kw['level'] = LOG_LEVEL
            simple_logging(**kw)
            log.info("Logging configured from the command line.")
            logging_configured = True

    if not logging_configured:          # logging.conf
        # TODO /etc/aspen/logging.conf
        # TODO /usr/local/etc/aspen/logging.conf
        logging_conf = join(dotaspen, 'logging.conf')
        if exists(logging_conf):
            logging.config.fileConfig(logging_conf) 
            log.info("logging configured from logging.conf")
            logging_configured = True

    if not logging_configured:          # aspen.conf [logging]
        kw = {'dotaspen': dotaspen}
        kw['filename'] = conf.logging.get('file')
        kw['filter'] = conf.logging.get('filter')
        kw['format'] = conf.logging.get('format', LOG_FORMAT)

        log_level = conf.logging.get('level')
        if log_level is not None:
            log_level = validate_log_level(log_level) 
        else:
            log_level = LOG_LEVEL
        kw['level'] = log_level

        simple_logging(**kw)
        log.info("Logging configured from aspen.conf.")
        logging_configured = True

    if kw is not None:
        return kw['filename'], kw['filter'], kw['format'], kw['level']
    else:
        return None, None, None, None
Пример #2
0
    def __init__(self, argv):
        """Takes an argv list, and gives it straight to optparser.parse_args.
        """

        # Initialize parsers.
        # ===================

        opts, args = optparser.parse_args(argv)

        # Root
        # ====
        # This can only be passed on the command line.

        if args:
            root = args[0]
        else:
            try:
                # Under supervisord, the following raises
                #   OSError: [Errno 2] No such file or directory
                # So be sure to pass a directory in on the command line, or cwd
                # using supervisord's own facility for that.
                root = os.getcwd()
            except OSError:
                raise ConfigurationError(
                    "Could not get a current working "
                    "directory. You can specify the site "
                    "root on the command line."
                )
        root = realpath(root)
        if not isdir(root):
            msg = "%s does not point to a directory" % root
            raise ConfigurationError(msg)

        # sys.path
        # ========

        dotaspen = join(root, ".aspen")
        if isdir(dotaspen):
            sys.path.insert(0, dotaspen)

        # aspen.conf
        # ==========

        conf = AspenConf(
            "/etc/aspen/aspen.conf",
            "/usr/local/etc/aspen/aspen.conf",
            expanduser("~/.aspen/aspen.conf"),
            join(dotaspen, "aspen.conf"),
        )  # later overrides earlier

        self.root = root
        self.args = args
        self.conf = conf
        self.optparser = optparser
        self.opts = opts

        # hooks
        # =====

        self.hooks = HooksConf(
            "/etc/aspen/hooks.conf",
            "/usr/local/etc/aspen/hooks.conf",
            expanduser("~/.aspen/hooks.conf"),
            join(dotaspen, "hooks.conf"),
        )  # later comes after earlier, per section

        # address/sockfam
        # ===============
        # These can be set either on the command line or in the conf file.

        if getattr(opts, "have_address", False):  # first check CLI
            address = opts.address
            sockfam = opts.sockfam
        elif "address" in conf.main:  # then check conf
            address, sockfam = validate_address(conf.main["address"])
        else:  # default from optparse
            address = opts.address
            sockfam = socket.AF_INET

        self.address = address
        self.sockfam = sockfam

        # Logging
        # =======
        # Logging can be configured from four places, in this order of
        # precedence:
        #
        #   1) some other module (theoretically, I'm not sure an aspen user
        #      could make this happen easily)
        #   2) the command line
        #   3) a logging.conf file
        #   4) a [logging] section in the aspen.conf file
        #
        # These are not layered; only one is used.

        # FMT = "%(process)-6s%(levelname)-9s%(name)-14s%(message)s"

        logging_configured = bool(len(logging.getLogger().handlers))
        # this test is taken from logging.basicConfig.

        if logging_configured:  # some other module
            log.warn("logging is already configured")

        if not logging_configured:  # command line
            kw = dict()
            kw["filename"] = opts.log_file
            kw["filter"] = opts.log_filter
            kw["format"] = opts.log_format
            kw["level"] = opts.log_level
            if kw.values() != [None, None, None, None]:  # at least one knob set
                if kw["format"] is None:
                    kw["format"] = LOG_FORMAT
                if kw["level"] is None:
                    kw["level"] = LOG_LEVEL
                self.configure_logging(**kw)
                log.info("logging configured from the command line")
                logging_configured = True

        if not logging_configured:  # logging.conf
            # TODO /etc/aspen/logging.conf
            # TODO /usr/local/etc/aspen/logging.conf
            logging_conf = join(root, ".aspen", "logging.conf")
            if exists(logging_conf):
                logging.config.fileConfig(logging_conf)
                log.info("logging configured from logging.conf")
                logging_configured = True

        if not logging_configured:  # aspen.conf [logging]
            kw = dict()
            kw["filename"] = conf.logging.get("file")
            kw["filter"] = conf.logging.get("filter")
            kw["format"] = conf.logging.get("format", LOG_FORMAT)

            log_level = conf.logging.get("level")
            if log_level is not None:
                log_level = validate_log_level(log_level)
            else:
                log_level = LOG_LEVEL
            kw["level"] = log_level

            self.configure_logging(**kw)
            log.info("logging configured from aspen.conf")
            logging_configured = True