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 all 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
64 changes: 64 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down