There are too many default fields for loguru serialize, most of which are not needed at all, which greatly wastes log storage space:

 from loguru import logger
import sys

logger.remove()

logger.add(sys.stdout, serialize='json')


logger.debug(f'哈哈')

output:

 {"text": "2022-08-28 01:11:45.258 | DEBUG    | __main__:<module>:9 - 哈哈\n", "record": {"elapsed": {"repr": "0:00:00.004474", "seconds": 0.004474}, "exception": null, "extra": {}, "file": {"name": "003.py", "path": "/home/bot/Desktop/ideaboom/test_logger/003.py"}, "function": "<module>", "level": {"icon": "🐞", "name": "DEBUG", "no": 10}, "line": 9, "message": "哈哈", "module": "003", "name": "__main__", "process": {"id": 282787, "name": "MainProcess"}, "thread": {"id": 139949730423424, "name": "MainThread"}, "time": {"repr": "2022-08-28 01:11:45.258317+08:00", "timestamp": 1661620305.258317}}}

We need to reduce these useless fields

Use something like "monkey patching" to achieve:

 from loguru._recattrs import RecordException
from loguru._handler import Handler
from loguru import logger
import sys
import json


def _serialize_record(text: str, record: dict):
    exception: RecordException = record["exception"]

    if exception is not None:
        exception = {
            "type": None if exception.type is None else exception.type.__name__,
            "value": exception.value,
            "traceback": bool(exception.traceback),
        }

    serializable = {
        "text": text,
        "record": {
            "extra": record["extra"],
        },
    }

    return json.dumps(serializable, default=str, ensure_ascii=False) + "\n"


Handler._serialize_record = staticmethod(_serialize_record)

logger.remove()

logger.add(sys.stdout, serialize='json')


logger.debug(f'哈哈')

output:

 {"text": "2022-08-28 01:10:55.326 | DEBUG    | __main__:<module>:35 - 哈哈\n", "record": {"extra": {}}}

Principle: We replace the loguru/_handler.py _serialize_record function in ---03b7ebaecd2dc511730aec946f7857bd--- with our own _serialize_record function.

The loguru/_handler.py _serialize_record function in ---271056700873763593578a098db12768--- looks like this:

 @staticmethod
def _serialize_record(text, record):
    exception = record["exception"]

    if exception is not None:
        exception = {
            "type": None if exception.type is None else exception.type.__name__,
            "value": exception.value,
            "traceback": bool(exception.traceback),
        }

    serializable = {
        "text": text,
        "record": {
            "elapsed": {
                "repr": record["elapsed"],
                "seconds": record["elapsed"].total_seconds(),
            },
            "exception": exception,
            "extra": record["extra"],
            "file": {"name": record["file"].name, "path": record["file"].path},
            "function": record["function"],
            "level": {
                "icon": record["level"].icon,
                "name": record["level"].name,
                "no": record["level"].no,
            },
            "line": record["line"],
            "message": record["message"],
            "module": record["module"],
            "name": record["name"],
            "process": {"id": record["process"].id, "name": record["process"].name},
            "thread": {"id": record["thread"].id, "name": record["thread"].name},
            "time": {"repr": record["time"], "timestamp": record["time"].timestamp()},
        },
    }

    return json.dumps(serializable, default=str, ensure_ascii=False) + "\n"

You can select fields according to actual needs


universe_king
3.4k 声望680 粉丝