Skip to content

Commit

Permalink
Automatically prepend module to task logger name
Browse files Browse the repository at this point in the history
Rather than having to edit every single task to specify a module
prefix, determing the module prefix automatically (usually "lsst")
and prepend it if the logger name is being determined
automatically.

Provide a flag to disable this such that specific Task implementations
can declare that the default name should be used on its own.
  • Loading branch information
timj committed Nov 10, 2021
1 parent 4a5c9cc commit 6a82db3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
15 changes: 15 additions & 0 deletions python/lsst/pipe/base/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ class Task:
`~lsst.pipe.base.CmdLineTask` not Task.
"""

_add_module_logger_prefix = True
"""Control whether the module prefix should be prepended to default
logger names."""

def __init__(self, config=None, name=None, parentTask=None, log=None):
self.metadata = dafBase.PropertyList()
self.__parentTask: Optional[weakref.ReferenceType]
Expand Down Expand Up @@ -167,6 +171,17 @@ def __init__(self, config=None, name=None, parentTask=None, log=None):
loggerName = self._fullName
if log is not None and log.name:
loggerName = log.getChild(loggerName).name
elif self._add_module_logger_prefix:
# Prefix the logger name with the root module name.
# We want all Task loggers to have this prefix to make
# it easier to control them. This can be disabled by
# a Task setting the class property _add_module_logger_prefix
# to False -- in which case the logger name will not be
# modified.
module_name = self.__module__
module_root = module_name.split(".")[0] + "."
if not loggerName.startswith(module_root):
loggerName = module_root + loggerName

# Get a logger (that might be a subclass of logging.Logger).
self.log = lsst.utils.logging.getLogger(loggerName)
Expand Down
9 changes: 9 additions & 0 deletions tests/test_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class AddMultConfig(pexConfig.Config):
class AddMultTask(pipeBase.Task):
ConfigClass = AddMultConfig
_DefaultName = "addMult"
_add_module_logger_prefix = False

"""First add, then multiply"""

Expand Down Expand Up @@ -106,6 +107,11 @@ def failCtx(self):
raise RuntimeError("failCtx intentional error")


class AddMultTask2(AddMultTask):
"""Subclass that gets an automatic logger prefix."""
_add_module_logger_prefix = True


class AddTwiceTask(AddTask):
"""Variant of AddTask that adds twice the addend"""

Expand Down Expand Up @@ -172,6 +178,9 @@ def testLog(self):
self.assertEqual(addMultTask.log.name, "tester.addMult")
self.assertEqual(addMultTask.add.log.name, "tester.addMult.add")

addMultTask2 = AddMultTask2()
self.assertEqual(addMultTask2.log.name, "test_task.addMult")

def testGetFullMetadata(self):
"""Test getFullMetadata()
"""
Expand Down

0 comments on commit 6a82db3

Please sign in to comment.