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

After asgi-correlation-id is used, the log cannot be printed when creating fastapi app #23

Closed
slowchen opened this issue Mar 12, 2022 · 7 comments

Comments

@slowchen
Copy link

I configured loguru in fastapi according to the example, and the code is as follows.

import logging
import sys

import uvicorn
from asgi_correlation_id import CorrelationIdMiddleware
from asgi_correlation_id.context import correlation_id
from fastapi import FastAPI
from loguru import logger

logger.remove()


def correlation_id_filter(record):
    record['correlation_id'] = correlation_id.get()
    return record['correlation_id']


fmt = "<green>{time:YYYY-MM-DD HH:mm:ss.SSS}</green> | <level>{level: <8}</level> | <red> {correlation_id} </red> | <cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> - <level>{message}</level>"

logger.add(sys.stderr, format=fmt, level=logging.DEBUG, filter=correlation_id_filter)


def start_app():
    logger.info('start app....')
    print('start app....')


app = FastAPI()

app.add_middleware(CorrelationIdMiddleware)

app.add_event_handler('startup', start_app)


@app.get('/')
def index():
    logger.info(f"Request with id ")
    return 'OK'


if __name__ == '__main__':
    uvicorn.run(app='mainw:app', host='0.0.0.0', port=8000, reload=False, debug=True)

After the project is started, logger did not print the log in start_app(), just print()

/Users/slowchen/.virtualenvs/fastapi/bin/python /Users/slowchen/workspace/FastApi/tianya-fastapi/app/mainw.py
INFO:     Will watch for changes in these directories: ['/Users/slowchen/workspace/FastApi/tianya-fastapi/app']
INFO:     Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
INFO:     Started reloader process [31627] using statreload
INFO:     Started server process [31629]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
start app....
INFO:     127.0.0.1:52057 - "GET / HTTP/1.1" 200 OK
2022-03-12 13:43:30.186 | INFO     |  048a508fb369425fb25a63631899b99c  | mainw:index:37 - Request with id 

How can I configure it to print the log correctly when fastapi, startup or adding middleware

@JonasKs
Copy link
Member

JonasKs commented Mar 12, 2022

Hi!

This is probably a question you should ask over at loguru, since it is not really related to this package. I am not a loguru user, and I'm not sure why you have to write logger.remove(), but this is the source of your problem. Remove that line and the log is printed. Uvicorn also takes log arguments, so maybe you should look into that?

image

I'll close this issue, but we'll still get notified if you reply in it. Have a good weekend 😊

@JonasKs JonasKs closed this as completed Mar 12, 2022
@slowchen
Copy link
Author

Thank you for your answer. And according to your picture, why is there no correlation id in the start app output log

@JonasKs
Copy link
Member

JonasKs commented Mar 12, 2022

The correlation ID is generated when a request is done to FastAPI, and then that ID attached to all other log messages for that request. In other words, if there is just a normal log that is not triggered through a request, it won’t have a correlation ID. Using loguru it seems to be completely gone, normally it would be None.

@slowchen
Copy link
Author

slowchen commented Mar 12, 2022

OK, I probably understand what you mean. This may be the problem of loguru. If you use python logging, even normal log can be printed success. Is that right?

@JonasKs
Copy link
Member

JonasKs commented Mar 12, 2022

I mean, the ID is generated on an incoming request. So any log generated on startup/shutdown is not triggered by an incoming request, and won’t have an ID.

@slowchen
Copy link
Author

I see. Thank you very much for your reply. Have a nice weekend

@JonasKs
Copy link
Member

JonasKs commented Mar 12, 2022

You’re welcome. Thank you, you too!😊

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

2 participants