def process_request(self, request): context.clear() extractor = settings.PYLOGCTX_REQUEST_EXTRACTOR try: context.update(**extractor(request)) except Exception: logger.exception()
def test_update_clear_remove(context): log_context.update(myField='toto', myOtherField='titi') fields = log_context.as_dict() assert 'myField' in fields assert 'myOtherField' in fields log_context.remove('myOtherField') fields = log_context.as_dict() assert 'myField' in fields assert 'myOtherField' not in fields log_context.clear() fields = log_context.as_dict() assert 'myField' not in fields
def test_adapter(): from pylogctx import context, log_adapter # To fill save context context.update(toto="tata") fields = context.as_dict() assert 'toto' in fields app = Celery(task_cls='pylogctx.celery.LoggingTask') @log_adapter(app.Task) def adapter(task): return { 'celeryTaskId': task.request.id, 'celeryTask': task.name } @app.task def my_task(): return context.as_dict() result = my_task.apply() if VERSION.major < 4: result.maybe_reraise() else: result.maybe_throw() fields = result.result assert 'celeryTask' in fields assert 'celeryTaskId' in fields # Check context is the same before task was started fields = context.as_dict() assert 'toto' in fields context.clear() # Clear context
def process_exception(self, request, exception): context.clear() return None
def process_response(self, request, response): context.clear() return response
def process_request(self, request): context.clear() try: context.update(request) except AdapterNotFound: logger.info("Can't adapt %s for log.", request.__class__.__name__)