Skip to content

Update PackageCache Class to respect external logging configuration #1879

Closed
@ameliendeshams

Description

@ameliendeshams

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
logfilepath = os.path.join(self._log_dir, time.strftime("%Y-%m-%d.log"))
handler1 = logging.FileHandler(logfilepath)
handler2 = ColorizedStreamHandler()
formatter = logging.Formatter(
"%(name)s %(asctime)s PID-%(process)d %(levelname)s %(message)s")
for h in (handler1, handler2):
h.setFormatter(formatter)
logger.addHandler(h)
# delete old logfiles
now = int(time.time())
try:
for name in os.listdir(self._log_dir):
filepath = os.path.join(self._log_dir, name)
st = os.stat(filepath)
age_secs = now - int(st.st_ctime)
age_days = age_secs / (3600 * 24)
if age_days > config.package_cache_log_days:
safe_remove(filepath)
except:
logger.exception("Failed to delete old logfiles")
return logger

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:

rez/src/rez/__init__.py

Lines 21 to 43 in 4e00cfd

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)
_init_logging()

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

Maintainer note:

There is an opportunity to reduce code duplication.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions