예제 #1
0
def get_logger():
    logging.setLoggerClass(PyLogrus)

    logger = logging.getLogger(__name__)  # type: PyLogrus
    logger.setLevel(logging.DEBUG)

    enabled_fields = [('name', 'logger_name'),
                      ('asctime', 'service_timestamp'), ('levelname', 'level'),
                      ('threadName', 'thread_name'), 'message',
                      ('exception', 'exception_class'),
                      ('stacktrace', 'stack_trace'), 'module',
                      ('funcName', 'function')]

    formatter = JsonFormatter(datefmt='Z',
                              enabled_fields=enabled_fields,
                              indent=2,
                              sort_keys=True)
    formatter.override_level_names({'CRITICAL': 'FATAL', 'WARNING': 'WARN'})

    ch = logging.StreamHandler()
    ch.setLevel(logging.DEBUG)
    ch.setFormatter(formatter)
    logger.addHandler(ch)

    return logger
예제 #2
0
    def test_level_names_overriding(self):
        formatter = JsonFormatter()
        formatter.override_level_names({'CRITICAL': 'FATAL'})
        log = self.get_logger(formatter)

        log.critical("test message")
        with open(self.filename) as f:
            content = json.loads(f.readlines()[-1])
            self.assertEqual(content['levelname'], 'FATAL')