Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update PackageCache Class to respect external logging configuration #1879

Open
ameliendeshams opened this issue Nov 14, 2024 · 0 comments
Open

Comments

@ameliendeshams
Copy link

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".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant