From 79219cadcc8aca81264fa6f7c554a45325d58ebb Mon Sep 17 00:00:00 2001 From: Hendry Ratnam Date: Thu, 6 Jul 2023 23:51:19 -0400 Subject: [PATCH 1/8] added logging section --- docs/index.md | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/docs/index.md b/docs/index.md index e2ec3d866..3babcfd72 100644 --- a/docs/index.md +++ b/docs/index.md @@ -291,6 +291,47 @@ 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). + +Below I have provided 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 + ## The ASGI interface Uvicorn uses the [ASGI specification][asgi] for interacting with an application. From c90379285d2ef987b9f2390ada5945124917648e Mon Sep 17 00:00:00 2001 From: Hendry Ratnam Date: Thu, 6 Jul 2023 23:52:07 -0400 Subject: [PATCH 2/8] added examples of how to implement logging files --- docs/index.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/docs/index.md b/docs/index.md index 3babcfd72..2d970375e 100644 --- a/docs/index.md +++ b/docs/index.md @@ -332,6 +332,28 @@ loggers: ``` > **_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 progromatically 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. From 3e28bc44492909b58db6a3beea78e5183252dd19 Mon Sep 17 00:00:00 2001 From: Ruckshan H Ratnam Date: Thu, 20 Jul 2023 20:17:34 -0400 Subject: [PATCH 3/8] Update docs/index.md Co-authored-by: Guilherme Bordignon --- docs/index.md | 1 - 1 file changed, 1 deletion(-) diff --git a/docs/index.md b/docs/index.md index 2d970375e..b3d3f09da 100644 --- a/docs/index.md +++ b/docs/index.md @@ -348,7 +348,6 @@ if __name__ == "__main__": Or you can use via command line like below - ``` $ uvicorn main:app --log-config ./logging.yaml ``` From aa4b42e7f3b36f747bd67b1d8e2e7670a9ab2536 Mon Sep 17 00:00:00 2001 From: Ruckshan H Ratnam Date: Thu, 20 Jul 2023 20:20:05 -0400 Subject: [PATCH 4/8] Update docs/index.md Co-authored-by: Guilherme Bordignon --- docs/index.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/index.md b/docs/index.md index b3d3f09da..7c15fba8e 100644 --- a/docs/index.md +++ b/docs/index.md @@ -293,7 +293,9 @@ $ 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). +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). Below I have provided an example of a log file written in YAML that will get you started. From 7ddda187ffda3ab09cecd6d98630db4287eec8ce Mon Sep 17 00:00:00 2001 From: Ruckshan H Ratnam Date: Thu, 20 Jul 2023 20:20:19 -0400 Subject: [PATCH 5/8] Update docs/index.md Co-authored-by: Guilherme Bordignon --- docs/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/index.md b/docs/index.md index 7c15fba8e..6c5372b57 100644 --- a/docs/index.md +++ b/docs/index.md @@ -297,7 +297,7 @@ You can also customize your logs using a configuration file written in YAML or J Find more information on creating these files in the [official Python documentation](https://docs.python.org/3/howto/logging.html#configuring-logging). -Below I have provided an example of a log file written in YAML that will get you started. +Here's an example of a log file written in YAML that will get you started. **logging.yaml**: ```yaml From 648ce485827c1e203a8b15c65b195a4af10bb9c7 Mon Sep 17 00:00:00 2001 From: Ruckshan H Ratnam Date: Thu, 20 Jul 2023 20:20:25 -0400 Subject: [PATCH 6/8] Update docs/index.md Co-authored-by: Guilherme Bordignon --- docs/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/index.md b/docs/index.md index 6c5372b57..5511cbbd4 100644 --- a/docs/index.md +++ b/docs/index.md @@ -335,7 +335,7 @@ loggers: > **_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 progromatically as seen below +After you have configured the logging file above, you can use it programmatically as seen below: ```python # main.py From 11351de643dbaa834dde26cd03e4740efbdd68d8 Mon Sep 17 00:00:00 2001 From: Ruckshan H Ratnam Date: Thu, 20 Jul 2023 20:20:33 -0400 Subject: [PATCH 7/8] Update docs/index.md Co-authored-by: Guilherme Bordignon --- docs/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/index.md b/docs/index.md index 5511cbbd4..7e1a3970c 100644 --- a/docs/index.md +++ b/docs/index.md @@ -332,7 +332,7 @@ loggers: handlers: - access ``` -> **_NOTE:_** Using Uvicorn's formatter gives us access to the terminal colors and proper formatting +> **_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: From a165db3d28e52a0d9d11bbfb99678cc5c5dcfcc5 Mon Sep 17 00:00:00 2001 From: Ruckshan H Ratnam Date: Thu, 20 Jul 2023 20:20:38 -0400 Subject: [PATCH 8/8] Update docs/index.md Co-authored-by: Guilherme Bordignon --- docs/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/index.md b/docs/index.md index 7e1a3970c..01b334113 100644 --- a/docs/index.md +++ b/docs/index.md @@ -348,7 +348,7 @@ 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 +Or you can use via command line like below: ``` $ uvicorn main:app --log-config ./logging.yaml