jsonformatter icon indicating copy to clipboard operation
jsonformatter copied to clipboard

Option to use custom json lib for encoding

Open mahenzon opened this issue 4 years ago • 9 comments

Right now builtin json lib is used:

https://github.com/MyColorfulDays/jsonformatter/blob/a5561e58de0857766d8cd10729ebe261f9fe8296/jsonformatter/jsonformatter.py#L12

https://github.com/MyColorfulDays/jsonformatter/blob/a5561e58de0857766d8cd10729ebe261f9fe8296/jsonformatter/jsonformatter.py#L165

https://github.com/MyColorfulDays/jsonformatter/blob/a5561e58de0857766d8cd10729ebe261f9fe8296/jsonformatter/jsonformatter.py#L427

But there're other libs that are faster, for example orjson, ujson, rapidjson, simplejson.. and more. It'll be cool to have an ability to change encoder (for speed and compatibility purposes).

Possible solution to make it flexible: https://github.com/aiogram/aiogram/blob/dev-2.x/aiogram/utils/json.py

mahenzon avatar Jul 30 '21 18:07 mahenzon

@mahenzon Good suggestion. I think I can add a keyword argument to support other libs encoder, e.g.dumps=json.dumps , like this

formatter = JsonFormatter(dumps=json.dumps, *args, **kwargs)

MyColorfulDays avatar Jul 31 '21 15:07 MyColorfulDays

Hi! Yeah, it'll be a good option But will it be compatible with dictConfig?
🤔

I can do a PR, but we need to agree on a solution.

mahenzon avatar Jul 31 '21 15:07 mahenzon

@mahenzon Thank you and welcome to commit PR. I think it will be compatible with dictConfig but not verified yet. Can you do this work?

MyColorfulDays avatar Aug 05 '21 07:08 MyColorfulDays

Sure! Will work on it

mahenzon avatar Aug 05 '21 13:08 mahenzon

how to modify the default parameters in Django configuaration,like set ensure_ascii=False

toplove avatar Aug 17 '21 08:08 toplove

@toplove https://github.com/MyColorfulDays/jsonformatter/tree/v0.4.x#case-2-in-django In your project settings.py

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'standard': {
            'class': 'jsonformatter.JsonFormatter',
            'format': OrderedDict([
                ("Name", "name"),
                ("Levelno", "levelno"),
                ("Levelname", "levelname"),
                ("Pathname", "pathname"),
                ("Filename", "filename"),
                ("Module", "module"),
                ("Lineno", "lineno"),
                ("FuncName", "funcName"),
                ("Created", "created"),
                ("Asctime", "asctime"),
                ("Msecs", "msecs"),
                ("RelativeCreated", "relativeCreated"),
                ("Thread", "thread"),
                ("ThreadName", "threadName"),
                ("Process", "process"),
                ("Message", "message")
            ]),
            "mix_extra": True,
            "ensure_ascii": False,
            # the other optional arguments
        },
    },
    'handlers': {
        'console': {
            'level': 'INFO',
            'formatter': 'standard',
            'class': 'logging.StreamHandler',
        },
    },
    'loggers': {
        'django': {
            'handlers': ['console'],
            'level': 'INFO',
            'propagate': False
        },
    }
}

MyColorfulDays avatar Aug 17 '21 09:08 MyColorfulDays

thanks for your answer,tried configuration like yours,but don't work it

    'standard': {
        'class': 'jsonformatter.JsonFormatter',
        'format': {
            "name": "name",
            "asctime": "asctime",
            "levelname": "levelname",
            "process": "process",
            "processName": "processName",
            "thread": "thread",
            "threadName": "threadName",
            "module": "module",
            "pathname": "pathname",
            "funcName": "funcName",
            "lineno": "lineno",
            "message": "message"
        },
        "ensure_ascii": False
    }

{"name": "django", "asctime": "2021-08-17 17:56:20,374", "levelname": "INFO", "process": 6372, "processName": "MainProcess", "thread": 24544, "threadName": "ThreadPoolExecutor-0_0", "module": "cmdb_consump", "pathname": "common\cmdb_consump.py", "funcName": "cmdb_consump", "lineno": 57, "message": "cmdb\u67e5\u8be2system_apps,view_ida86de242caad4489bcd8f0375b7eb130\uff0c\u63a5\u53e3\u54cd\u5e94\u65f6\u95f40.5826475620269775\u79d2"}

toplove avatar Aug 17 '21 10:08 toplove

@toplove Oh, the above answer is for version v0.4.X(not release yet), If you're using version v0.3.X, reference this https://github.com/MyColorfulDays/jsonformatter/issues/5#issuecomment-801268674

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'standard': {
            'class': 'jsonformatter.JsonFormatter',
            'fmt': OrderedDict([
                ("Name", "name"),
                ("Levelno", "levelno"),
                ("Levelname", "levelname"),
                ("Pathname", "pathname"),
                ("Filename", "filename"),
                ("Module", "module"),
                ("Lineno", "lineno"),
                ("FuncName", "funcName"),
                ("Created", "created"),
                ("Asctime", "asctime"),
                ("Msecs", "msecs"),
                ("RelativeCreated", "relativeCreated"),
                ("Thread", "thread"),
                ("ThreadName", "threadName"),
                ("Process", "process"),
                ("Message", "message")
            ]),
            "mix_extra": True,
            "ensure_ascii": False,
            # the other optional arguments
        },
    },
    'handlers': {
        'console': {
            'level': 'INFO',
            'formatter': 'standard',
            'class': 'logging.StreamHandler',
        },
    },
    'loggers': {
        'django': {
            'handlers': ['console'],
            'level': 'INFO',
            'propagate': False
        },
    }
}

MyColorfulDays avatar Aug 17 '21 10:08 MyColorfulDays

thank you very much!

toplove avatar Aug 18 '21 01:08 toplove