Skip to content

Commit

Permalink
Allow a non-default logger to be used in Task
Browse files Browse the repository at this point in the history
By default a Task uses its full name to set its logger (self.log).
This commit allows a Log to be passed in the Task's ctor and used.
  • Loading branch information
Hsin-Fang Chiang committed Aug 29, 2016
1 parent 74870f4 commit 15fba26
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
9 changes: 6 additions & 3 deletions python/lsst/pipe/base/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ def __init__(self, config=None, name=None, parentTask=None, log=None):
@param[in] parentTask the parent task of this subtask, if any.
- If None (a top-level task) then you must specify config and name is ignored.
- If not None (a subtask) then you must specify name
@param[in] log lsst.log Log; if None then the default is used;
in either case a copy is made using the full task name.
@param[in] log a lsst.log Log to be used as the logger
- if None then a Log with the full task name is used
@throw RuntimeError if parentTask is None and config is None.
@throw RuntimeError if parentTask is not None and name is None.
Expand Down Expand Up @@ -121,7 +121,10 @@ def __init__(self, config=None, name=None, parentTask=None, log=None):
if config is None:
config = self.ConfigClass()
self._taskDict = dict()
self.log = Log.getLogger(self._fullName)
if log is not None and isinstance(log, Log):
self.log = log
else:
self.log = Log.getLogger(self._fullName)

self.config = config
self._display = lsstDebug.Info(self.__module__).display
Expand Down
10 changes: 10 additions & 0 deletions tests/testTask.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import lsst.utils.tests
import lsst.daf.base as dafBase
from lsst.log import Log
import lsst.pex.config as pexConfig
import lsst.pipe.base as pipeBase

Expand Down Expand Up @@ -159,6 +160,15 @@ def testNames(self):
self.assertEqual(addMultTask.add._fullName, "addMult.add")
self.assertEqual(addMultTask.mult._fullName, "addMult.mult")

def testLog(self):
"""Test the Task's logger
"""
addMultTask = AddMultTask()
self.assertEqual(addMultTask.log.getName(), addMultTask.getFullName())
log = Log.getLogger("testLogger")
addMultTask = AddMultTask(log=log)
self.assertEqual(addMultTask.log.getName(), "testLogger")

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

0 comments on commit 15fba26

Please sign in to comment.