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.

How can I log a request with his params and headers ?. Something like this:

INFO 2020-08-13 13:36:33.494 uvicorn.protocols.http.h11_impl:send - 127.0.0.1:52660 - "GET /url1/url2/ HTTP/1.1" 400 params= {"some": value, "some1":value} headers={"header1": value}

I’m using loguru with fastapi and uvicorn.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
Delgancommented, Aug 18, 2020

That happens because of colorize=True. Loguru will add ansi codes to colorize your logs. However, these codes are only transformed to colors while inside a terminal.

By default, if the colorize argument is not provided, Loguru tries to guess whether or not the sink (like sys.stderr or a file) supports ansi codes and strip them out if it doesn’t. However as you specified colorize=True, Loguru will add them anyway. That’s why you’re seeing such symbols in your file.

You can set colorize=False or just plainly omit this parameter and it should work fine. 😉

2reactions
Delgancommented, Aug 14, 2020

Indeed, but look at where the format_record is used in the snippet you shared:

logger.configure(
    handlers=[{"sink": sys.stdout, "level": logging.DEBUG, "format": format_record}]
)

This custom format is only used to configure the sys.stdout handler. If you want the same format for the logs emitted to your file, you need to specify it while adding the file handler too. For example:

logger.add("log/access.log", format=format_record, enqueue=True , backtrace=True, diagnose=True)
Read more comments on GitHub >

github_iconTop Results From Across the Web

FastAPI logging - Phil Girard
FastAPI logging. Here is how I setup my python logging for a fastAPI project. I have the following fastAPI file architecture: main.py logging.conf...
Read more >
Logging & Tracing in Python, FastApi, OpenCensus and Azure
Learn how to correctly log & trace in Python, FastApi and then collect & visualize them. Tagged with python, fastapi, logging.
Read more >
Adding python logging to FastApi endpoints, hosted on docker ...
I have a fastapi app on which I want to add python logging. I followed the basic tutorial and added this, however this...
Read more >
Debugging - FastAPI
You can connect the debugger in your editor, for example with Visual Studio Code or PyCharm. Call uvicorn ¶. In your FastAPI application,...
Read more >
fastapi-log - PyPI
Fastapi Route Log. License. A FastAPI router for logging every request. Installation. $ pip install fastapi_log. Dashboard testing.
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