Skip to content

How can I customize a JSON request log formatter? #66

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

Closed
buildpeak opened this issue Apr 5, 2021 · 4 comments
Closed

How can I customize a JSON request log formatter? #66

buildpeak opened this issue Apr 5, 2021 · 4 comments

Comments

@buildpeak
Copy link

buildpeak commented Apr 5, 2021

From this example (https://github.com/bobbui/json-logging-python/blob/master/example/custom_log_format.py), I can easily customize a JSON non-web formatter.
However, it seems not easy to customize a JSON log fomatter dealing with request information. For example, like this:

request = record.request_info.request
request_adapter = request_util.request_adapter
json_log_object = {
    ...
    "request_id": request_util.get_correlation_id(request),
    "path": request_adapter.get_path(request),
    ...
}

Because, request_util is not passed to logging.Formatter.format method. Any idea?

Thanks

@bobbui
Copy link
Owner

bobbui commented Apr 5, 2021

u should use init_request_instrument to pass a customized custom formatter for request logging, see the method signature here . I've just updated readme to include new example for web logging https://github.com/bobbui/json-logging-python#26-custom-log-formatter

@bobbui bobbui closed this as completed Apr 5, 2021
@buildpeak
Copy link
Author

u should use init_request_instrument to pass a customized custom formatter for request logging, see the method signature here . I've just updated readme to include new example for web logging https://github.com/bobbui/json-logging-python#26-custom-log-formatter

Thanks, but the problem is that I need to get an instance of request_util in my custom formatter so that I can get an instance of a request adapter.

@bobbui
Copy link
Owner

bobbui commented Apr 10, 2021

@buildpeak After tackle this #68 , I think the better way to customize JSON logging request is something like that

@hanzhenbjm
Copy link

main.zip
Note: the log file cannot be written using filter

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

No branches or pull requests

3 participants