Permalink
Browse files

Adds celery.utils.log.get_task_logger

  • Loading branch information...
1 parent c507893 commit e4dbf1098edc681d06f5da35c8192eadc557de83 @ask committed Jun 4, 2012
Showing with 28 additions and 5 deletions.
  1. +2 −5 celery/app/task.py
  2. +7 −0 celery/utils/log.py
  3. +19 −0 docs/whatsnew-2.6.rst
View
@@ -28,7 +28,7 @@
from celery.utils import fun_takes_kwargs, uuid, maybe_reraise
from celery.utils.functional import mattrgetter, maybe_list
from celery.utils.imports import instantiate
-from celery.utils.log import get_logger
+from celery.utils.log import get_task_logger
from celery.utils.mail import ErrorMail
from .annotations import resolve_all as resolve_all_annotations
@@ -796,10 +796,7 @@ def __repr__(self):
def _get_logger(self, **kwargs):
"""Get task-aware logger object."""
- logger = get_logger(self.name)
- if logger.parent is logging.root:
- logger.parent = get_logger("celery.task")
- return logger
+ return get_task_logger(self.name)
logger = cached_property(_get_logger)
@property
View
@@ -43,6 +43,13 @@ def get_logger(name):
task_logger = get_logger("celery.task")
+def get_task_logger(name):
+ logger = get_logger(name)
+ if logger.parent is logging.root:
+ logger.parent = task_logger
+ return logger
+
+
def mlevel(level):
if level and not isinstance(level, int):
return LOG_LEVELS[level.upper()]
View
@@ -310,6 +310,25 @@ Logging support now conforms better with best practices.
a special formatter adding these values at runtime from the
currently executing task.
+- In fact, ``task.get_logger`` is no longer recommended, it is better
+ to add module-level logger to your tasks module.
+
+ For example, like this:
+
+ .. code-block:: python
+
+ from celery.utils.log import get_task_logger
+
+ logger = get_task_logger(__name__)
+
+ @celery.task()
+ def add(x, y):
+ logger.debug("Adding %r + %r" % (x, y))
+ return x + y
+
+ The resulting logger will then inherit from the ``"celery.task"`` logger
+ so that the current task name and id is included in logging output.
+
- Redirected output from stdout/stderr is now logged to a "celery.redirected"
logger.

0 comments on commit e4dbf10

Please sign in to comment.