Skip to content

Commit 7a97337

Browse files
committed
docs: Update README.md to include customizable log levels, dependency injection example, and FastAPI integration
1 parent 89e6b62 commit 7a97337

File tree

1 file changed

+129
-2
lines changed

1 file changed

+129
-2
lines changed

README.md

Lines changed: 129 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Chromatrace is a Python package designed for advanced logging capabilities, incl
55
## Features
66

77
- Configurable logging settings using Pydantic.
8+
- Customizable log levels and loggers for different services.
89
- Support for trace IDs and request IDs.
910
- Customizable log formats and handlers.
1011
- Asynchronous and synchronous function tracing.
@@ -42,9 +43,135 @@ Use the `tracer` decorator to trace your functions:
4243
```python
4344
@tracer
4445
async def my_async_function():
45-
# Your code here
46+
logger.debug("Check something")
47+
logger.info("Doing something")
48+
logger.warning("Doing something")
49+
logger.error("Something went wrong")
4650
```
4751

52+
### Dependency Injection using Lagom
53+
54+
```python
55+
from lagom import Container
56+
57+
container = Container()
58+
59+
from chromatrace import LoggingConfig, LoggingSettings
60+
61+
container[LoggingSettings] = LoggingSettings()
62+
container[LoggingConfig] = LoggingConfig(
63+
container[LoggingSettings],
64+
application_level='Development',
65+
enable_tracing=True,
66+
ignore_nan_trace=True
67+
)
68+
```
69+
70+
Then, add the `LoggingConfig` to your service:
71+
72+
```python
73+
import logging
74+
75+
from chromatrace import LoggingConfig
76+
77+
78+
class SomeService:
79+
def __init__(self, logging_config: LoggingConfig):
80+
self.logger = logging_config.get_logger(self.__class__.__name__)
81+
self.logger.setLevel(logging.ERROR)
82+
83+
async def do_something(self):
84+
self.logger.debug("Check something in second service")
85+
self.logger.info("Doing something in second service")
86+
self.logger.error("Something went wrong in second service")
87+
```
88+
89+
Results:
90+
```log
91+
[Development]-(2024-11-21 23:43:26)-[INFO]-[APIService]-FILENAME:api_app.py-FUNC:do_something-THREAD:MainThread-LINE:27 ::
92+
Doing something in API service
93+
94+
[Development]-(2024-11-21 23:43:26)-[ERROR]-[APIService]-FILENAME:api_app.py-FUNC:do_something-THREAD:MainThread-LINE:28 ::
95+
Something went wrong in API service
96+
97+
[T-dc1be4de]-[Development]-(2024-11-21 23:43:26)-[INFO]-[Main]-FILENAME:main.py-FUNC:main-THREAD:MainThread-LINE:21 ::
98+
Starting main
99+
100+
[T-dc1be4de]-[Development]-(2024-11-21 23:43:26)-[ERROR]-[ExampleService]-FILENAME:example_service.py-FUNC:do_something-THREAD:MainThread-LINE:26 ::
101+
Something went wrong
102+
103+
[T-dc1be4de]-[Development]-(2024-11-21 23:43:26)-[INFO]-[InnerService]-FILENAME:example_service.py-FUNC:do_something-THREAD:MainThread-LINE:13 ::
104+
Doing something in second service
105+
106+
[T-dc1be4de]-[Development]-(2024-11-21 23:43:26)-[ERROR]-[InnerService]-FILENAME:example_service.py-FUNC:do_something-THREAD:MainThread-LINE:14 ::
107+
Something went wrong in second service
108+
109+
[T-dc1be4de]-[Development]-(2024-11-21 23:43:26)-[DEBUG]-[AnotherSample]-FILENAME:sample.py-FUNC:do_something-THREAD:MainThread-LINE:12 ::
110+
Check something
111+
112+
[T-dc1be4de]-[Development]-(2024-11-21 23:43:26)-[INFO]-[AnotherSample]-FILENAME:sample.py-FUNC:do_something-THREAD:MainThread-LINE:13 ::
113+
Doing something
114+
115+
[T-dc1be4de]-[Development]-(2024-11-21 23:43:26)-[WARNING]-[AnotherSample]-FILENAME:sample.py-FUNC:do_something-THREAD:MainThread-LINE:14 ::
116+
Doing something
117+
118+
[T-dc1be4de]-[Development]-(2024-11-21 23:43:26)-[ERROR]-[AnotherSample]-FILENAME:sample.py-FUNC:do_something-THREAD:MainThread-LINE:15 ::
119+
Something went wrong
120+
```
121+
122+
The two first log was out of trace and the trace ID was not added to the log message. The rest of the logs were within the trace and the trace ID - `T-dc1be4de`, was added to the log message.
123+
124+
**NOTE**: The important thing is that each Class or Service can have its own log level. This is useful when you want to have different log levels for different services.
125+
126+
### FastAPI Integration
127+
128+
```python
129+
from chromatrace import RequestIdMiddleware
130+
131+
app = FastAPI()
132+
app.add_middleware(RequestIdMiddleware)
133+
```
134+
135+
Result:
136+
```log
137+
[R-ffe0a9a2]-[Development]-(2024-11-22 00:13:53)-[INFO]-[APIService]-FILENAME:api_app.py-FUNC:read_root-THREAD:MainThread-LINE:38 ::
138+
Hello World
139+
140+
[R-ffe0a9a2]-[Development]-(2024-11-22 00:13:53)-[ERROR]-[ExampleService]-FILENAME:example_service.py-FUNC:do_something-THREAD:MainThread-LINE:26 ::
141+
Something went wrong
142+
143+
[R-ffe0a9a2]-[Development]-(2024-11-22 00:13:53)-[INFO]-[InnerService]-FILENAME:example_service.py-FUNC:do_something-THREAD:MainThread-LINE:13 ::
144+
Doing something in second service
145+
146+
[R-ffe0a9a2]-[Development]-(2024-11-22 00:13:53)-[ERROR]-[InnerService]-FILENAME:example_service.py-FUNC:do_something-THREAD:MainThread-LINE:14 ::
147+
Something went wrong in second service
148+
```
149+
150+
As you can see, the request ID - `R-ffe0a9a2` is automatically added to the log messages from the thread that handles the request.
151+
152+
153+
## Examples
154+
155+
You can find examples of how to use Chromatrace in the [examples](src/exmples/) directory. Run the examples using the following command:
156+
157+
```bash
158+
python main.py
159+
```
160+
161+
Then, run:
162+
163+
```bash
164+
curl 0.0.0.0:8000
165+
```
166+
167+
Now, check the logs in the terminal. :)
168+
48169
## License
49170

50-
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
171+
This project is licensed under the GNU AFFERO GENERAL PUBLIC LICENSE - see the [LICENSE](LICENSE) file for details.
172+
173+
## Ideas and Sources
174+
175+
- [Python Logging Best Practices Tips](https://coralogix.com/blog/python-logging-best-practices-tips/)
176+
- [12 Python Logging Best Practices To Debug Apps Faster](https://middleware.io/blog/python-logging-best-practices/)
177+
- [10 Best Practices for Logging in Python](https://betterstack.com/community/guides/logging/python/python-logging-best-practices/)

0 commit comments

Comments
 (0)