Esempio n. 1
0
    def __init__(self,
                 name: str = 'aiologger-json',
                 level: int = logging.DEBUG,
                 serializer: Callable[..., str] = json.dumps,
                 flatten: bool = False,
                 serializer_kwargs: Dict = None,
                 extra: Dict = None,
                 exclude_fields: Iterable[str] = None,
                 loop: AbstractEventLoop = None,
                 tz: timezone = None,
                 formatter: Optional[logging.Formatter] = None,
                 handler_factory: Optional[Callable[
                     [], Awaitable[Iterable[logging.Handler]]]] = None):
        formatter = formatter or ExtendedJsonFormatter(
            serializer=serializer, exclude_fields=exclude_fields, tz=tz)
        super().__init__(name=name,
                         level=level,
                         loop=loop,
                         formatter=formatter,
                         handler_factory=handler_factory)

        self.flatten = flatten

        if serializer_kwargs is None:
            serializer_kwargs = {}
        self.serializer_kwargs = serializer_kwargs

        if extra is None:
            extra = {}
        self.extra = extra
Esempio n. 2
0
 def with_default_handlers(  # type: ignore
     cls,
     *,
     name: str = "aiologger-json",
     level: int = LogLevel.NOTSET,
     serializer: Callable[..., str] = json.dumps,
     flatten: bool = False,
     serializer_kwargs: Dict = None,
     extra: Dict = None,
     exclude_fields: Iterable[str] = None,
     loop: Optional[AbstractEventLoop] = None,
     tz: timezone = None,
     formatter: Optional[Formatter] = None,
 ):
     if formatter is None:
         formatter = ExtendedJsonFormatter(
             serializer=serializer, exclude_fields=exclude_fields, tz=tz
         )
     return super(JsonLogger, cls).with_default_handlers(
         name=name,
         level=level,
         loop=loop,
         flatten=flatten,
         serializer_kwargs=serializer_kwargs,
         extra=extra,
         formatter=formatter,
     )
Esempio n. 3
0
 def get_logger(nats_handler, service_type):
     """
     Creates and returns a nats logging handler
     Args:
         nats_handler: a nats_handler object
         service_type: type of service 
     """
     handler = NatsLoggingHandler(nats_handler, service_type)
     formatter = ExtendedJsonFormatter(exclude_fields=["file_path"])
     handler.formatter = formatter
     logger = JsonLogger(name=nats_handler.sender_id)
     logger.add_handler(handler)
     return logger
Esempio n. 4
0
 def test_formatter_with_tz_info_utc(self):
     """
     Check that, if tz is not None, log messages must have timezone info
     """
     formatter = ExtendedJsonFormatter(tz=timezone.utc)
     result = dict(formatter.formatter_fields_for_record(self.record))
     self.assertEqual(
         result, {
             'logged_at': '2018-06-16T13:16:00+00:00',
             'line_number': 42,
             'function': 'xablaufunc',
             'level': 'WARNING',
             'file_path':
             '/aiologger/tests/formatters/test_json_formatter.py'
         })
Esempio n. 5
0
    def test_json_properly_serialized_when_bytes_object(self):
        # orjson
        custom_formatter = ExtendedJsonFormatter(serializer=orjson.dumps)
        # Note: json.dumps by default uses this separator (', ', ': ')
        # adding a whitespace whereas with orjson it is not
        # so to have a perfect match is it necessary to specify it
        custom_orjson_serializer_msg = custom_formatter.format(self.record)

        # json
        serialized_result = self.formatter.format(self.record)
        content = json.loads(serialized_result)
        default_json_serializer_msg = self.formatter.serializer(
            content, separators=(",", ":"))

        self.assertEqual(custom_orjson_serializer_msg,
                         default_json_serializer_msg)
Esempio n. 6
0
 def test_formatter_with_tz_info_other_zone(self):
     """
     Current time is NOT UTC (is -7) and we want logs to be generate in America/Sao_Paulo (-3)
     """
     tz_america = timezone(timedelta(hours=-3))
     formatter = ExtendedJsonFormatter(tz=tz_america)
     result = dict(formatter.formatter_fields_for_record(self.record))
     self.assertEqual(
         result, {
             'logged_at': '2018-06-16T19:20:00-03:00',
             'line_number': 42,
             'function': 'xablaufunc',
             'level': 'WARNING',
             'file_path':
             '/aiologger/tests/formatters/test_json_formatter.py'
         })
Esempio n. 7
0
 def test_formatter_with_tz_info_utc(self):
     """
     Check that, if tz is not None, log messages must have timezone info
     """
     formatter = ExtendedJsonFormatter(tz=timezone.utc)
     result = dict(formatter.formatter_fields_for_record(self.record))
     self.assertEqual(
         result,
         {
             "logged_at": "2018-06-16T13:16:00+00:00",
             "line_number": 42,
             "function": "xablaufunc",
             "level": "WARNING",
             "file_path":
             "/aiologger/tests/formatters/test_json_formatter.py",
         },
     )
Esempio n. 8
0
 def setUp(self):
     self.formatter = ExtendedJsonFormatter()
     self.record = LogRecord(
         level=30,
         name='aiologger',
         pathname="/aiologger/tests/formatters/test_json_formatter.py",
         func='xablaufunc',
         lineno=42,
         msg={
             "dog": "Xablau",
             "action": "bark"
         },
         exc_info=None,
         args=None,
         extra=None,
         flatten=False,
         serializer_kwargs={})
Esempio n. 9
0
 def test_formatter_with_tz_info_america_sao_paulo_from_utc(self):
     """
     Current time is UTC and we want logs to be generated on America/Sao_Paulo (-3)
     """
     tz_america = timezone(timedelta(hours=-3))
     formatter = ExtendedJsonFormatter(tz=tz_america)
     result = dict(formatter.formatter_fields_for_record(self.record))
     self.assertEqual(
         result,
         {
             "logged_at": "2018-06-16T13:20:00-03:00",
             "line_number": 42,
             "function": "xablaufunc",
             "level": "WARNING",
             "file_path":
             "/aiologger/tests/formatters/test_json_formatter.py",
         },
     )
Esempio n. 10
0
 def test_default_log_fields_can_be_excluded_with_exclude_fields_initialization_argument(
         self):
     formatter = ExtendedJsonFormatter(
         exclude_fields=(LOG_LEVEL_FIELDNAME, ))
     self.assertNotIn(LOG_LEVEL_FIELDNAME, formatter.log_fields)
Esempio n. 11
0
 def test_it_uses_default_log_fields_if_none_are_excluded_on_initialization(
         self):
     formatter = ExtendedJsonFormatter()
     self.assertEqual(ExtendedJsonFormatter.default_fields,
                      formatter.log_fields)