diff --git a/docs/index.md b/docs/index.md index e2ec3d866..01b334113 100644 --- a/docs/index.md +++ b/docs/index.md @@ -291,6 +291,70 @@ def create_app(): $ uvicorn --factory example:create_app ``` +### Logging + +You can also customize your logs using a configuration file written in YAML or JSON format. + +Find more information on creating these files in the [official Python documentation](https://docs.python.org/3/howto/logging.html#configuring-logging). + +Here's an example of a log file written in YAML that will get you started. + +**logging.yaml**: +```yaml +version: 1 +disable_existing_loggers: False +formatters: + default: + (): 'uvicorn.logging.DefaultFormatter' + fmt: '%(asctime)s -- %(levelprefix)s %(message)s' + access: + (): 'uvicorn.logging.AccessFormatter' + fmt: '%(asctime)s -- %(levelprefix)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 +``` +> **_NOTE:_** Using Uvicorn's formatter gives us access to the terminal colors and proper formatting. + + +After you have configured the logging file above, you can use it programmatically as seen below: + +```python +# main.py +import uvicorn + +async def app(scope, receive, send): + ... + +if __name__ == "__main__": + uvicorn.run("main:app", port=5000, log_level="info", log_config="./logging.yaml") +``` + +Or you can use via command line like below: + +``` +$ uvicorn main:app --log-config ./logging.yaml +``` + + ## The ASGI interface Uvicorn uses the [ASGI specification][asgi] for interacting with an application.