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

Logging custom docs #515

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 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
17 changes: 17 additions & 0 deletions docs/examples.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Examples

## Custom logging

The below `logging_example.yaml` is a yaml representation of the default logging configuration.
You can pass it loading a dict, using the `--log-config` flag.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just checking... Does this definately work?
I haven't checked out if Python's log file format also supports using the yaml style?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is definitely an ambiguous formulation, python logging supports dict / file (ini style). A lot of time people use yaml and load it to dict with pyyaml for instance, that's why I said "representation"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, but it wouldn't directly work with our --log-config flag right?

(Tho we could potentially change things here, so that we expect a YAML style logging config file.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correct, one would have to do something like

    with open('logging_example.yaml', 'r') as stream:
        config = yaml.load(stream, yaml.FullLoader)
    uvicorn.run("main:app", reload=True, log_config=config, log_level="trace")

It sets 2 loggers:
1. `uvicorn.error` whose formatter is the `uvicorn.logging.DefaultFormatter`.
2. `uvicorn.access` whose formatter is the `uvicorn.logging.AccessFormatter`.

Both formatters will output a colorized automatically if a tty is detected.

If you used the `--use-colors / --no-use-colors` then the output will / won't be colorized.

```yaml hl_lines="7 11 38 39 40 41"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather than any example is just included directly, than using this style.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok so you prefer to remove markdown-include and just copy-paste examples right ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

{!./src/logging_example/logging_example.yaml!}
```
2 changes: 2 additions & 0 deletions docs/settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ equivalent keyword arguments, eg. `uvicorn.run("example:app", port=5000, reload=
* `--no-access-log` - Disable access log only, without changing log level.
* `--use-colors / --no-use-colors` - Enable / disable colorized formatting of the log records, in case this is not set it will be auto-detected.

See a custom logging configuration [example](examples.md)

## Implementation

* `--loop <str>` - Set the event loop implementation. The uvloop implementation provides greater performance, but is not compatible with Windows or PyPy. But you can use IOCP in windows. **Options:** *'auto', 'asyncio', 'uvloop', 'iocp'.* **Default:** *'auto'*.
Expand Down
29 changes: 29 additions & 0 deletions docs/src/logging_example/logging_example.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
version: 1
disable_existing_loggers: False
formatters:
default:
"()": uvicorn.logging.DefaultFormatter
format: "%(levelprefix)s %(message)s"
access:
"()": uvicorn.logging.AccessFormatter
format: '%(levelprefix)s %(client_addr)s - "%(request_line)s" %(status_code)s'
handlers:
default:
formatter: default
class: logging.StreamHandler
stream: ext://sys.stdout
access:
formatter: access
class: logging.StreamHandler
stream: ext://sys.stdout
loggers:
uvicorn.error:
level: INFO
handlers:
- default
propagate: no
uvicorn.access:
level: INFO
handlers:
- access
propagate: no
2 changes: 2 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ pages:
markdown_extensions:
- markdown.extensions.codehilite:
guess_lang: false
- markdown_include.include:
base_path: docs
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ requests
# Documentation
mkdocs
mkdocs-material
markdown-include