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

add section on logging in documentation #2031

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,69 @@ 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. You can find more info on creating these files from the official Python documentation found [here](https://docs.python.org/3/howto/logging.html#configuring-logging).
ruckshan-ratnam marked this conversation as resolved.
Show resolved Hide resolved

Below I have provided an example of a log file written in YAML that will get you started.
ruckshan-ratnam marked this conversation as resolved.
Show resolved Hide resolved

**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
ruckshan-ratnam marked this conversation as resolved.
Show resolved Hide resolved


After you have configured the logging file above you can use it progromatically as seen below
ruckshan-ratnam marked this conversation as resolved.
Show resolved Hide resolved

```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
ruckshan-ratnam marked this conversation as resolved.
Show resolved Hide resolved

<!-- :cli_usage: -->
ruckshan-ratnam marked this conversation as resolved.
Show resolved Hide resolved
```
$ uvicorn main:app --log-config ./logging.yaml
```


## The ASGI interface

Uvicorn uses the [ASGI specification][asgi] for interacting with an application.
Expand Down