def process_request(self, request): request.uuid = str(uuid.uuid4()) request._logger_ctx = log.name_and_fields('django.http', uuid=request.uuid, method=request.method, path=request.path) request._logger_ctx.__enter__() log.info('Request accepted')
def execute(self, *args, **options): self.command_name = self.__class__.__module__.rsplit('.')[-1] fields = dict(args=', '.join(args), uuid=str(uuid.uuid4())) fields.update((key, value) for key, value in options.items() if value is not None) # For convenience to not make # from twiggy_goodies.threading import log # in each command self.logger = log with log.name_and_fields('command.' + self.command_name, **fields): log.info('Running command') try: return super(LogMixin, self).execute(*args, **options) except Exception: log.trace().error('Unhandled exception') raise
def some_function(): log.info('inner function does not accept logger') log.info('but uses same field as caller')
#!/usr/bin/env python from twiggy_goodies.setup import setup_logging from twiggy_goodies.threading import log def some_function(): log.info('inner function does not accept logger') log.info('but uses same field as caller') setup_logging(None) log.info('before request') with log.fields(request_id='foo'): log.info('bar has happened') some_function() log.info('after request, id gone')