Skip to content

Commit c6d56e2

Browse files
committed
docs: update README to enhance logging configuration and add Uvicorn integration example
1 parent 4f4a3cc commit c6d56e2

File tree

1 file changed

+49
-7
lines changed

1 file changed

+49
-7
lines changed

README.md

+49-7
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,13 @@ container = Container()
7676

7777
from chromatrace import LoggingConfig, LoggingSettings
7878

79-
container[LoggingSettings] = LoggingSettings()
80-
container[LoggingConfig] = LoggingConfig(
81-
container[LoggingSettings],
82-
application_level='Development',
83-
enable_tracing=True,
84-
ignore_nan_trace=True
85-
)
79+
container[LoggingSettings] = LoggingSettings(
80+
application_level="Development",
81+
enable_tracing=True,
82+
ignore_nan_trace=False,
83+
enable_file_logging=True,
84+
)
85+
container[LoggingConfig] = LoggingConfig(container[LoggingSettings])
8686
```
8787

8888
Then, add the `LoggingConfig` to your service:
@@ -195,6 +195,48 @@ Received message on main namespace. SID: FI3E_S_A-KsTi4RLAAAD, Message: Hello fr
195195
Yes, the socket logs are also within the trace. The trace ID - `S-4e2b7c5e` and `S-aaf46528` was added to the log messages. For better experience, the prefix `S` was added to the trace ID to differentiate it from the request ID.
196196

197197

198+
### Uvicorn Integration
199+
200+
```python
201+
from chromatrace.uvicorn import GetLoggingConfig, UvicornLoggingSettings
202+
203+
rest_application = FastAPI()
204+
205+
uvicorn.run(
206+
rest_application,
207+
host="0.0.0.0",
208+
port=8000,
209+
log_level="debug",
210+
log_config=GetLoggingConfig(
211+
UvicornLoggingSettings(
212+
enable_file_logging=True,
213+
show_process_id=True,
214+
)
215+
),
216+
)
217+
```
218+
219+
Result:
220+
```log
221+
(2024-12-12 20:54:54)-[PID:3710345]-[INFO]: Started server process [3710345]
222+
(2024-12-12 20:54:54)-[PID:3710345]-[INFO]: Waiting for application startup.
223+
(2024-12-12 20:54:54)-[PID:3710345]-[INFO]: Application startup complete.
224+
(2024-12-12 20:54:54)-[PID:3710345]-[INFO]: Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
225+
(2024-12-12 20:54:57)-[PID:3710345]-[INFO]: ADDRESS:(127.0.0.1:46166) - REQUEST:"GET /consume HTTP/1.1" - STATUS:200 OK
226+
(2024-12-12 20:55:45)-[PID:3710345]-[INFO]: ADDRESS:(127.0.0.1:54018) - REQUEST:"GET /consume HTTP/1.1" - STATUS:200 OK
227+
(2024-12-12 20:56:51)-[PID:3710345]-[INFO]: ADDRESS:(127.0.0.1:58240) - REQUEST:"GET / HTTP/1.1" - STATUS:200 OK
228+
(2024-12-12 20:56:51)-[PID:3710345]-[INFO]: ADDRESS:(127.0.0.1:58254) - REQUEST:"GET / HTTP/1.1" - STATUS:200 OK
229+
(2024-12-12 20:56:52)-[PID:3710345]-[INFO]: ADDRESS:(127.0.0.1:58260) - REQUEST:"GET / HTTP/1.1" - STATUS:200 OK
230+
(2024-12-12 20:56:52)-[PID:3710345]-[INFO]: ADDRESS:(127.0.0.1:58270) - REQUEST:"GET / HTTP/1.1" - STATUS:200 OK
231+
(2024-12-12 21:16:45)-[PID:3710345]-[INFO]: Shutting down
232+
(2024-12-12 21:16:45)-[PID:3710345]-[INFO]: Waiting for application shutdown.
233+
(2024-12-12 21:16:45)-[PID:3710345]-[INFO]: Application shutdown complete.
234+
(2024-12-12 21:16:45)-[PID:3710345]-[INFO]: Finished server process [3710345]
235+
```
236+
237+
The logs are within the process ID - `PID:3710345` was added to the log messages. The log messages are also colored for better visibility. The log messages are also written to the file if the `enable_file_logging` is set to `True`. For more information, check the [config.py](src/chromatrace/uvicorn/config.py) file, `UvicornLoggingSettings` class.
238+
239+
198240
## Examples
199241

200242
> You don't trust me, do you? I understand. You wanna see it in action, right? I got you covered. :)

0 commit comments

Comments
 (0)