# Path: djenga/logging/formatters.py
# class JsonFormatter(logging.Formatter):
#     """
#     This formatter is useful if you want to ship logs to a
#     json-based centralized log aggregation platform like ELK.
#     n.b., this formatter is very opinionated.
#     """
#     def format_message(self, record: logging.LogRecord):  # noqa: C901
#         s = record.getMessage()
#         if record.exc_info:
#             # Cache the traceback text to avoid converting it multiple times
#             # (it's constant anyway)
#             if not record.exc_text:
#                 record.exc_text = self.formatException(record.exc_info)
#         if record.exc_text:
#             if s[-1:] != '\n':
#                 s = s + '\n'
#             s = s + record.exc_text
#         if record.stack_info:
#             if s[-1:] != '\n':
#                 s = s + '\n'
#             s = s + self.formatStack(record.stack_info)
#         return s
# 
#     def iso_time(self, record: logging.LogRecord):
#         dt = datetime.utcfromtimestamp(record.created)
#         return dt.isoformat('T')
# 
#     DEFAULT_KEYS = {
#         'name', 'msg', 'args', 'levelname', 'levelno', 'pathname', 'filename',
#         'module', 'exc_info', 'exc_text', 'stack_info', 'lineno', 'funcName',
#         'created', 'msecs', 'relativeCreated', 'thread', 'threadName',
#         'processName', 'process',
#     }
# 
#     def to_dict(self, record: logging.LogRecord):
#         data = {
#             'timestamp': self.iso_time(record),
#             'message': self.format_message(record),
#             'function': record.funcName,
#             'path': record.pathname,
#             'module': record.module,
#             'level': record.levelname,
#             'line_number': record.lineno,
#             'logger': record.name,
#         }
#         exception_info = record.exc_info
#         if exception_info:
#             exception_type = record.exc_info[0]
#             data['exception_type'] = (
#                 f'{exception_type.__module__}.'
#                 f'{exception_type.__name__}'
#             )
#             data['exception_args'] = list(record.exc_info[1].args)
#         for key, value in record.__dict__.items():
#             if key not in JsonFormatter.DEFAULT_KEYS:
#                 data[key] = value
#         return data
# 
#     def format(self, record: logging.LogRecord):
#         return json.dumps(self.to_dict(record), cls=DjangoJSONEncoder)
# 
# class JsonTaskFormatter(JsonFormatter):
#     """
#     This formatter is useful if you want to ship celery logs to a
#     json-based centralized log aggregation platform like ELK.
#     n.b., this formatter is very opinionated.
#     """
#     def to_dict(self, record: logging.LogRecord):
#         data = super(JsonTaskFormatter, self).to_dict(record)
#         try:
#             from celery._state import get_current_task
#             task = get_current_task()
#             if task and task.request:
#                 data['task_id'] = task.request.id
#                 data['task_name'] = task.name
#         except ImportError:
#             pass
#         return data


import json
import logging
from unittest import mock
from django.test import TestCase
from djenga.logging.formatters import JsonFormatter, JsonTaskFormatter


__all__ = [ 'JsonFormatterTest', ]
log = logging.getLogger(__name__)


class JsonFormatterTest(TestCase):
    def test_json_formatter(self):

# Path: djenga/logging/formatters.py
# class JsonFormatter(logging.Formatter):
#     """
#     This formatter is useful if you want to ship logs to a
#     json-based centralized log aggregation platform like ELK.
#     n.b., this formatter is very opinionated.
#     """
#     def format_message(self, record: logging.LogRecord):  # noqa: C901
#         s = record.getMessage()
#         if record.exc_info:
#             # Cache the traceback text to avoid converting it multiple times
#             # (it's constant anyway)
#             if not record.exc_text:
#                 record.exc_text = self.formatException(record.exc_info)
#         if record.exc_text:
#             if s[-1:] != '\n':
#                 s = s + '\n'
#             s = s + record.exc_text
#         if record.stack_info:
#             if s[-1:] != '\n':
#                 s = s + '\n'
#             s = s + self.formatStack(record.stack_info)
#         return s
# 
#     def iso_time(self, record: logging.LogRecord):
#         dt = datetime.utcfromtimestamp(record.created)
#         return dt.isoformat('T')
# 
#     DEFAULT_KEYS = {
#         'name', 'msg', 'args', 'levelname', 'levelno', 'pathname', 'filename',
#         'module', 'exc_info', 'exc_text', 'stack_info', 'lineno', 'funcName',
#         'created', 'msecs', 'relativeCreated', 'thread', 'threadName',
#         'processName', 'process',
#     }
# 
#     def to_dict(self, record: logging.LogRecord):
#         data = {
#             'timestamp': self.iso_time(record),
#             'message': self.format_message(record),
#             'function': record.funcName,
#             'path': record.pathname,
#             'module': record.module,
#             'level': record.levelname,
#             'line_number': record.lineno,
#             'logger': record.name,
#         }
#         exception_info = record.exc_info
#         if exception_info:
#             exception_type = record.exc_info[0]
#             data['exception_type'] = (
#                 f'{exception_type.__module__}.'
#                 f'{exception_type.__name__}'
#             )
#             data['exception_args'] = list(record.exc_info[1].args)
#         for key, value in record.__dict__.items():
#             if key not in JsonFormatter.DEFAULT_KEYS:
#                 data[key] = value
#         return data
# 
#     def format(self, record: logging.LogRecord):
#         return json.dumps(self.to_dict(record), cls=DjangoJSONEncoder)
# 
# class JsonTaskFormatter(JsonFormatter):
#     """
#     This formatter is useful if you want to ship celery logs to a
#     json-based centralized log aggregation platform like ELK.
#     n.b., this formatter is very opinionated.
#     """
#     def to_dict(self, record: logging.LogRecord):
#         data = super(JsonTaskFormatter, self).to_dict(record)
#         try:
#             from celery._state import get_current_task
#             task = get_current_task()
#             if task and task.request:
#                 data['task_id'] = task.request.id
#                 data['task_name'] = task.name
#         except ImportError:
#             pass
#         return data


# Path: djenga_tests/tests/json_formatters.py
import json
import logging
from unittest import mock
from django.test import TestCase
from djenga.logging.formatters import JsonFormatter, JsonTaskFormatter



__all__ = [ 'JsonFormatterTest', ]
log = logging.getLogger(__name__)


class JsonFormatterTest(TestCase):
    def test_json_formatter(self):

        formatter = JsonFormatter()