Commit d77ea0e9 authored by Markus Scheidgen's avatar Markus Scheidgen
Browse files

Use better logformatter for pytest caplog output.

parent 29fbf2fd
......@@ -256,15 +256,19 @@ class LogstashFormatter(logstash.formatter.LogstashFormatterBase):
class ConsoleFormatter(LogstashFormatter):
short_format = False
@classmethod
def serialize(cls, message_dict):
from io import StringIO
logger = message_dict.pop('logger_name', None)
logger = message_dict.pop('logger_name', 'unknown logger')
event = message_dict.pop('event', None)
level = message_dict.pop('level', None)
level = message_dict.pop('level', 'UNKNOWN')
exception = message_dict.pop('exception', None)
time = message_dict.pop('@timestamp', None)
time = message_dict.pop('@timestamp', '1970-01-01 12:00:00')
for key in ['type', 'tags', 'stack_info', 'path', 'message', 'host', '@version', 'digest']:
message_dict.pop(key, None)
keys = list(message_dict.keys())
......@@ -277,7 +281,12 @@ class ConsoleFormatter(LogstashFormatter):
out.write('\n - exception: %s' % str(exception).replace('\n', '\n '))
for key in keys:
out.write('\n - %s: %s' % (key, str(message_dict.get(key, None))))
if cls.short_format and key.startswith('nomad.'):
print_key = key[6:]
else:
print_key = key
if not cls.short_format or print_key not in ['release', 'service']:
out.write('\n - %s: %s' % (print_key, str(message_dict.get(key, None))))
return out.getvalue()
......
......@@ -28,11 +28,13 @@ from flask import request, g
import elasticsearch.exceptions
from typing import List
import numpy as np
import json
import logging
from nomadcore.local_meta_info import loadJsonFile
import nomad_meta_info
from nomad import config, infrastructure, parsing, processing, app, search
from nomad import config, infrastructure, parsing, processing, app, search, utils
from nomad.datamodel import User, CalcWithMetadata
from nomad.parsing import LocalBackend
......@@ -46,6 +48,10 @@ test_log_level = logging.CRITICAL
example_files = [empty_file, example_file]
utils.ConsoleFormatter.short_format = True
logging.Formatter = utils.ConsoleFormatter
@pytest.fixture(scope="session")
def monkeysession(request):
from _pytest.monkeypatch import MonkeyPatch
......@@ -346,10 +352,12 @@ def test_user_bravado_client(client, test_user_auth, monkeypatch):
@pytest.fixture(scope='function')
def no_warn(caplog):
caplog.handler.formatter = utils.ConsoleFormatter()
yield caplog
for record in caplog.get_records(when='call'):
if record.levelname in ['WARNING', 'ERROR', 'CRITICAL']:
assert False, record.msg
msg = utils.ConsoleFormatter.serialize(json.loads(record.msg))
assert False, msg
@pytest.fixture(scope='function')
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment