Skip to content
This repository has been archived by the owner on Oct 28, 2020. It is now read-only.

Commit

Permalink
dependencies.py: log worker is started/finished
Browse files Browse the repository at this point in the history
- add 'data' field to log
  • Loading branch information
vldpro authored and merindorium committed Dec 3, 2018
1 parent 4918ed6 commit 8141f8b
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions nameko_worker_logger/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,21 @@


def _worker_ctx_to_dict(worker_ctx):
return {
ctx = {
'call_id': worker_ctx.call_id,
'call_id_parent': worker_ctx.immediate_parent_call_id,
'call_id_stack': worker_ctx.call_id_stack,
'method_name': worker_ctx.entrypoint.method_name,
'service_name': worker_ctx.service_name
'service_name': worker_ctx.service_name,
'data': worker_ctx.data,
}
return {'worker': ctx}


def _exception_info_to_dict(exc_info):
exc_type, msg, traceback = exc_info
return {'type': exc_type, 'message': msg, 'traceback': traceback}
info = {'type': exc_type, 'message': msg, 'traceback': traceback}
return {'exception_info': info}


class WorkerLogger(DependencyProvider):
Expand All @@ -61,14 +64,20 @@ def __init__(self, logger_name):

def get_dependency(self, worker_ctx):
"""Create logger adapter with worker's contextual data."""
worker_info = {'worker': _worker_ctx_to_dict(worker_ctx)}
worker_info = _worker_ctx_to_dict(worker_ctx)
adapter = logging.LoggerAdapter(self.logger, extra=worker_info)
return adapter

def worker_setup(self, worker_ctx):
"""Log task info, before starting task execution."""
worker_info = _worker_ctx_to_dict(worker_ctx)
self.logger.info("worker is started", extra=worker_info)

def worker_result(self, worker_ctx, result=None, exc_info=None):
"""Log exception info, if it is present."""
worker_info = _worker_ctx_to_dict(worker_ctx)
if exc_info is None:
self.logger.info("worker successfully finished", extra=worker_info)
return
exception_info = {'exception_info': _exception_info_to_dict(exc_info)}
worker_info = {'worker': _worker_ctx_to_dict(worker_ctx)}
exception_info = _exception_info_to_dict(exc_info)
self.logger.error(exception_info, extra=worker_info)

0 comments on commit 8141f8b

Please sign in to comment.