format.py 901 Bytes
Newer Older
Weizhi Cui's avatar
Weizhi Cui committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
# @Version     : Python 3.11.4
# @Software    : Sublime Text 4
# @Author      : StudentCWZ
# @Email       : StudentCWZ@outlook.com
# @Date        : 2023/10/28 18:21
# @File        : format.py
# @Description :
"""

import json


def serialize(record: dict) -> str:
崔为之's avatar
崔为之 committed
17
    """Customize logger format"""
Weizhi Cui's avatar
Weizhi Cui committed
18 19 20 21 22 23 24 25 26 27 28 29 30
    time_stamp = record["time"]
    time_stamp = time_stamp.strftime("%Y-%m-%d %H:%M:%S")
    subset = {
        "time": time_stamp,
        "message": record["message"],
        "level": record["level"].name.lower(),
        "tag": "{}:{}".format(record["file"].path, record["line"]),
        "field": {"data": record["extra"].get("data", {})},
    }
    return json.dumps(subset, ensure_ascii=False)


def patching(record: dict) -> str:
崔为之's avatar
崔为之 committed
31
    """Set the patch"""
Weizhi Cui's avatar
Weizhi Cui committed
32 33
    record["extra"]["serialized"] = serialize(record)
    return "{extra[serialized]}\n"