-
-
Notifications
You must be signed in to change notification settings - Fork 748
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
Add example of logging config file #491
Comments
Agreed yup. As a starting point we could just link to this: https://docs.python.org/3/library/logging.config.html#logging-config-fileformat Tho ideally a proper example that's relevant to uvicorn would be ace. Any pull requests here would be most welcome (even if it's just linkig to the Python docs to start with.) |
It would also be worth mentioning that by enabling the custom log, other uvicorn loggers are likely to become disabled. See #511 |
I found this helpful:
|
I will appreciate good example for the uvicorn logging with other libs.
|
Do you use any config file by default when nothing is passed to the --log-config? |
I would like to specify the log configuration in a separate file and use it with the option --log-config.
but I don't know how to specify it in a config file. |
assuming you're passing a yaml file, something like the below should print the timestamp, this is well explained in python docs here version: 1
formatters:
simple:
format: '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
handlers:
console:
class: logging.StreamHandler
level: DEBUG
formatter: simple
stream: ext://sys.stdout
loggers:
simpleExample:
level: DEBUG
handlers: [console]
propagate: no
root:
level: DEBUG
handlers: [console] |
Thank you @euri10. However, I got the following error when I try to run uvicorn with --log-config option: Sorry, but this is my very first time with this problem and I'm a bit confused... |
dunno what you're doing, seems like you're using a ini logging format ? you can use
Lines 240 to 247 in bdab488
for reference with the above simplest log.yaml and running
|
I also had @mattiacampana 's issue --- fixed by updating to the latest uvicorn with |
How add custom header to access log? User-Agent or Authorization, etc.? Thanks. |
Not sure if this is helpful but I had the same issue as @mattiacampana and I solved by defining a new formatter, a new handler and configuring the root logger to use them. Then setting version: 1
disable_existing_loggers: False
formatters:
timestamped:
format: '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
handlers:
console:
class: logging.StreamHandler
level: INFO
formatter: timestamped
stream: ext://sys.stdout
root:
level: INFO
handlers: [console] |
Another solution I found, in order to use uvicorn formatter classes (and not lose colors, for instance), is to re-define loggers like this: version: 1
disable_existing_loggers: False
formatters:
default:
(): 'uvicorn.logging.DefaultFormatter'
fmt: '%(asctime)s %(levelprefix)-9s %(name)s -: %(message)s'
access:
(): 'uvicorn.logging.AccessFormatter'
fmt: '%(asctime)s %(levelprefix)-9s %(name)s -: %(client_addr)s - "%(request_line)s" %(status_code)s'
handlers:
default:
class: logging.StreamHandler
formatter: default
stream: ext://sys.stderr
access:
class: logging.StreamHandler
formatter: access
stream: ext://sys.stdout
loggers:
uvicorn:
level: INFO
handlers:
- default
uvicorn.error:
level: INFO
uvicorn.access:
level: INFO
propagate: False
handlers:
- access |
I'm experiecing the same problem as @mattiacampana and @nicoinn. I already upgraded uvicorn to the latest version: Error:
Service:
log_conf.yml
|
@houmie your error is in gunicron which tries to load a ini file, this is unrelated to uvicorn |
That's strange. This only happens when I try to pass in
If I don't do that it works fine.
I even tried to load everything without gunicorn config.
It keeps complaining about the yml file:
What could I be missing, please? |
again, this has nothing to do with uvicorn, you are passing a yaml log file to gunicorn while it expects a ini file: |
Sorry my bad. I'm trying to get my head around this. So how would I pass in Based on the docs: https://www.uvicorn.org/#running-with-gunicorn I'm not seeing how to achieve that. Thanks |
it's in the docs here https://www.uvicorn.org/deployment/#gunicorn
|
Ah Thanks. I see. So it should look like this? /etc/app/config.py
And then the gunicorn service loads it like this, correct?
Is there a way to load the uvicorn.yml file into the sub class without the pain of translating every line into CONFIG_KWARGS by hand? |
/trying to keep calm and not throw a RTFM... what about that ?
please try not to hijack unrelated issues and read the docs, thanks |
Please keep calm. Why are you so on the edge? I'm having a respectful and friendly conversation with you. Nobody is holding a gun against your head here. The documentation is not very comprehensive.
It seems pointing to the subclass in the same |
just run |
¯_(ツ)_/¯ I have followed what you have suggested: /etc/systemd/system/app.service:
A friendly reminder the subclass of MyUvicornWorker is in /etc/app/config.py.
|
adapt your path @houmie |
Any example for this? I want to log the request_id from nginx |
@grillazz Thanks for the reference, but I just ended up using |
You are absolutely right. I will show a complete case that how Gunicorn can use Uvicorn log:
The FastAPI app dir: Deploy dir: # deploy/__init__.py
from uvicorn_worker import UvicornWorker
class MyUvicornWorker(UvicornWorker):
CONFIG_KWARGS = {
"loop": "asyncio",
"http": "auto",
"lifespan": "off",
"log_config": "mylogconfig.yaml"
} # deploy/gunicorn.conf.py
wsgi_app = "app.main:app"
bind = "127.0.0.1:3011"
timeout = 5
worker_class = "deploy.MyUvicornWorker" # this is important
accesslog = "access.log"
errorlog = "debug.log"
... The log config file: version: 1
disable_existing_loggers: False
formatters:
default:
(): 'uvicorn.logging.DefaultFormatter'
fmt: '%(levelprefix)s [%(asctime)s] %(message)s'
access:
(): 'uvicorn.logging.AccessFormatter'
fmt: '%(levelprefix)s [%(asctime)s] %(client_addr)s - "%(request_line)s" %(status_code)s'
handlers:
default:
class: logging.StreamHandler
formatter: default
stream: ext://sys.stderr
access:
class: logging.StreamHandler
formatter: access
stream: ext://sys.stdout
loggers:
uvicorn:
level: INFO
handlers:
- default
uvicorn.error:
level: INFO
uvicorn.access:
level: INFO
propagate: False
handlers:
- access Run: Hope this can help you guys, |
In the doc, there is the description of
--log-config <path>
for the configuration of a config file. I think an example of config file could be really awesome for the new comers.Important
The text was updated successfully, but these errors were encountered: