parent
248c430ffa
commit
713e878a25
3 changed files with 77 additions and 7 deletions
@ -0,0 +1,17 @@ |
||||
from django.core.management.base import BaseCommand |
||||
|
||||
from finance.loggers import FinanceLogger |
||||
|
||||
|
||||
class Command(BaseCommand): |
||||
help = 'Команда для тестов' |
||||
|
||||
def handle(self, *args, **options): |
||||
logger = FinanceLogger() |
||||
try: |
||||
10 / 0 |
||||
except Exception as exc: |
||||
logger.exception('logger_yandex test log record', extra={'asdf': 3246523}, |
||||
invoice_id=42, exc={'a': 1, 'b': 3, 'привет': 'медвед'} |
||||
) |
||||
|
||||
@ -0,0 +1,48 @@ |
||||
# -*- coding: utf-8 -*- |
||||
|
||||
import logging |
||||
|
||||
_logger = logging.getLogger('finance_data') |
||||
|
||||
|
||||
class FinanceLogger: |
||||
""" |
||||
Все kwargs попадют в %(finance_data)s и логируются |
||||
'format': '%(asctime)s - %(levelname)s - %(message)s - %(finance_data)s' |
||||
""" |
||||
|
||||
def log(self, level, msg, *args, **kwargs): |
||||
_logger.log(level=level, msg=msg, *args, **self._make_kwargs(kwargs)) |
||||
|
||||
def _make_kwargs(self, kwargs): |
||||
new_kwargs = {} |
||||
for inspected_kwarg in ('exc_info', 'stack_info', 'extra'): |
||||
try: |
||||
new_kwargs[inspected_kwarg] = kwargs.pop(inspected_kwarg) |
||||
except KeyError: |
||||
pass |
||||
if 'extra' in new_kwargs: |
||||
new_kwargs['extra']['finance_data'] = kwargs |
||||
else: |
||||
new_kwargs['extra'] = dict(finance_data=kwargs) |
||||
return new_kwargs |
||||
|
||||
def debug(self, msg, *args, **kwargs): |
||||
self.log(level=logging.DEBUG, msg=msg, *args, **kwargs) |
||||
|
||||
def info(self, msg, *args, **kwargs): |
||||
self.log(level=logging.INFO, msg=msg, *args, **kwargs) |
||||
|
||||
def warning(self, msg, *args, **kwargs): |
||||
self.log(level=logging.WARNING, msg=msg, *args, **kwargs) |
||||
|
||||
# TODO отделить логирование ошибок в другой лог |
||||
def error(self, msg, *args, **kwargs): |
||||
self.log(level=logging.ERROR, msg=msg, *args, **kwargs) |
||||
|
||||
def critical(self, msg, *args, **kwargs): |
||||
self.log(level=logging.CRITICAL, msg=msg, *args, **kwargs) |
||||
|
||||
def exception(self, msg, *args, **kwargs): |
||||
kwargs['stack_info'] = True |
||||
_logger.exception(msg, *args, **self._make_kwargs(kwargs)) |
||||
Loading…
Reference in new issue