Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to use custom json lib for encoding #12

Open
mahenzon opened this issue Jul 30, 2021 · 9 comments · May be fixed by #13
Open

Option to use custom json lib for encoding #12

mahenzon opened this issue Jul 30, 2021 · 9 comments · May be fixed by #13
Assignees
Labels
enhancement New feature or request

Comments

@mahenzon
Copy link

Right now builtin json lib is used:

return json.loads(fmt, object_pairs_hook=dictionary)

return json.dumps(

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

@MyColorfulDays MyColorfulDays self-assigned this Jul 31, 2021
@MyColorfulDays MyColorfulDays added the enhancement New feature or request label Jul 31, 2021
@MyColorfulDays
Copy link
Owner

@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)

@mahenzon
Copy link
Author

mahenzon commented Jul 31, 2021

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.

@MyColorfulDays
Copy link
Owner

@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?

@mahenzon
Copy link
Author

mahenzon commented Aug 5, 2021

Sure! Will work on it

@mahenzon mahenzon linked a pull request Aug 5, 2021 that will close this issue
@toplove
Copy link

toplove commented Aug 17, 2021

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

@MyColorfulDays
Copy link
Owner

@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
        },
    }
}

@toplove
Copy link

toplove commented Aug 17, 2021

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"}

@MyColorfulDays
Copy link
Owner

@toplove
Oh, the above answer is for version v0.4.X(not release yet), If you're using version v0.3.X, reference this
#5 (comment)

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
        },
    }
}

@toplove
Copy link

toplove commented Aug 18, 2021

thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants