AcademySoftwareFoundation / rez

An integrated package configuration, build and deployment system for software
https://rez.readthedocs.io
Apache License 2.0
958 stars 339 forks source link

Update PackageCache Class to respect external logging configuration #1879

Open ameliendeshams opened 1 week ago

ameliendeshams commented 1 week ago

Modify the _init_logging method in the PackageCache class of the package_cache.py module to check for the presence of the REZ_LOGGING_CONF environment variable. If this variable is set, use its value as a logging configuration file and return the configured logger object. This ensures that the custom logging setup can override the default "rez-pkg-cache" logger created by the PackageCache class.

Current code:

def _init_logging(self):
    """
    Creates logger that logs to file and stdout. Used for:
    - adding variants in daemonized proc;
    - clean(), which would typically be run as a cron, but can also be run
      manually (hence the logging to stdout also)
    """
    logger = logging.getLogger("rez-pkg-cache")
    logger.setLevel(logging.INFO)
    logger.propagate = False

    [...]

Suggestion:

def _init_logging(self):
    """
    Creates logger that logs to file and stdout. Used for:
    - adding variants in daemonized proc;
    - clean(), which would typically be run as a cron, but can also be run
      manually (hence the logging to stdout also)
    """
    logger = logging.getLogger("rez-pkg-cache")

    logging_conf = os.getenv("REZ_LOGGING_CONF")
    if logging_conf:
        logging.config.fileConfig(logging_conf, disable_existing_loggers=False)
        return logger

    logger.setLevel(logging.INFO)
    logger.propagate = False

    [...]

Motivation Since the rez module has its own _init_logging function that implements custom logging functionality with the environment variable REZ_LOGGING_CONF:

def _init_logging():
    logging_conf = os.getenv("REZ_LOGGING_CONF")
    if logging_conf:
        import logging.config
        logging.config.fileConfig(logging_conf, disable_existing_loggers=False)
        return

    import logging
    from rez.utils.colorize import ColorizedStreamHandler

    formatter = logging.Formatter(
        fmt="%(asctime)s %(levelname)-8s %(message)s",
        datefmt="%X"
    )
    handler = ColorizedStreamHandler(sys.stderr)
    handler.setFormatter(formatter)
    logger = logging.getLogger("rez")
    logger.propagate = False
    logger.setLevel(logging.DEBUG)
    logger.addHandler(handler)

It would be wise to implement the same feature in the _init_logging method of the PackageCache class.

For now, we've set up our own custom handlers on the logger "rez-pkg-cache", but there are still duplicate logs and no way to remove the handlers from "rez-pkg-cache".