Skip to content
27 changes: 27 additions & 0 deletions src/rez/data/tests/config/logging_config_test.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[loggers]
keys=root,rez-pkg-cache

[handlers]
keys=myTestingConsoleHandler

[formatters]
keys=simpleFormatter

[logger_root]
level=DEBUG
handlers=myTestingConsoleHandler

[logger_rez-pkg-cache]
level=DEBUG
handlers=myTestingConsoleHandler
qualname=rez-pkg-cache
propagate=0

[handler_myTestingConsoleHandler]
class=StreamHandler
level=DEBUG
formatter=simpleFormatter
args=(sys.stdout,)

[formatter_simpleFormatter]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
6 changes: 6 additions & 0 deletions src/rez/package_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import platform
import time
import logging
import logging.config
import random
import threading
from contextlib import contextmanager
Expand Down Expand Up @@ -846,6 +847,11 @@ def _init_logging(self):
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

Expand Down
22 changes: 18 additions & 4 deletions src/rez/tests/test_package_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@
"""
Test package caching.
"""
import logging
import os
import os.path
import time
import subprocess

from rez.tests.util import TestBase, TempdirMixin, restore_os_environ, \
install_dependent
from rez.packages import get_package
from rez.package_cache import PackageCache
from rez.resolved_context import ResolvedContext
from rez.exceptions import PackageCacheError
from rez.utils.filesystem import canonical_path
import os
import os.path
import time
import subprocess


class TestPackageCache(TestBase, TempdirMixin):
Expand Down Expand Up @@ -123,6 +125,18 @@ def test_cache_fail_per_package(self):
with self.assertRaises(PackageCacheError):
pkgcache.add_variant(variant)

def test_external_logging_config(self):
"""Test that external logging is respected if configured."""
config_file_path = canonical_path(self.data_path("config", "logging_config_test.conf"))
with restore_os_environ():
os.environ["REZ_LOGGING_CONF"] = config_file_path
pkgcache = self._pkgcache()
pkgcache._init_logging()
logger = logging.getLogger('rez-pkg-cache')
self.assertEqual(len(logger.handlers), 1)
self.assertEqual(logger.handlers[0].__class__, logging.StreamHandler)
self.assertEqual(logger.handlers[0].level, logging.DEBUG)

@install_dependent()
def test_caching_on_resolve(self):
"""Test that cache is updated as expected on resolved env."""
Expand Down
Loading