question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

JsonLogging format

See original GitHub issue

Hi folks, could be possible a Json format logger? i’m using elasticsearch to log data, and json is very intuitive there’s a logger but i don’t know how to use it with uvicorn:

from pythonjsonlogger.jsonlogger import JsonFormatter
JsonFormatter("(asctime) (levelname) (name) (module) (funcName) (lineno) (message)")

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

13reactions
erewokcommented, May 20, 2020

This is something that I do in all of my projects. Currently, I am running gunicorn with the uvicorn worker and passing a custom logfile like so:

$ gunicorn example:app -w 2 -k uvicorn.workers.UvicornWorker --log-config logging_config.conf

The logging_config.conf looks like this:

[loggers]
keys=root, uvicorn, gunicorn

[handlers]
keys=access_handler

[formatters]
keys=json

[logger_root]
level=INFO
handlers=access_handler
propagate=1

[logger_gunicorn]
level=INFO
handlers=access_handler
propagate=0
qualname=gunicorn

[logger_uvicorn]
level=INFO
handlers=access_handler
propagate=0
qualname=uvicorn

[handler_access_handler]
class=logging.StreamHandler
formatter=json
args=()

[formatter_json]
class=pythonjsonlogger.jsonlogger.JsonFormatter

Note that for high throughput, the uvicorn docs mention turning off access logging, which I have not done here.

0reactions
florimondmancacommented, Sep 28, 2020

Hello all, since it looks like this is mostly a logging configuration question and that some solutions were provided above, I’m going to close this off for housekeeping purposes. Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

JSON Logging Best Practices - Loggly
JSON logging is a kind of structured logging, Meaning you'll have your log data parsed and stored in a structured JSON format. Why...
Read more >
JSON Logging: What, Why, How, & Tips - Coralogix
There is no standard format for JSON logs – just the JSON syntax – so you can decide on a structure to serve...
Read more >
8 Essential Tips for JSON logging - Papertrail
The JSON format is ubiquitous and used everywhere from logging in web apps to message passing for microcontrollers. Thanks to its compact nature...
Read more >
Format for writing a JSON log file? - Stack Overflow
That log format is called JSON-lines by various logging apps. Logstash for example: elastic.co/guide/en/logstash/current/… – Mnebuerquo. Mar 28, ...
Read more >
JSON Well Log Format
The log header contains metadata that describes the overall logging operation and consists of any JSON objects and arrays that the producing entity...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found