Skip to content

Commit 488b4ec

Browse files
authored
Merge pull request #9 from Msameim181/develop
V0.1.9 to v0.2.11
2 parents 15a8552 + 911d2e7 commit 488b4ec

File tree

14 files changed

+568
-137
lines changed

14 files changed

+568
-137
lines changed

.github/workflows/build.yml

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
name: Build and Test
1+
name: Test and Tag and Publish
22

33
on:
44
push:
5-
branches: [ main ]
5+
branches: [ main, develop ]
66
pull_request:
7-
branches: [ main ]
7+
branches: [ main, develop ]
88
release:
99
types: [created]
1010

@@ -26,7 +26,7 @@ jobs:
2626
- name: Install dependencies
2727
run: |
2828
python -m pip install --upgrade pip
29-
pip install build pytest pytest-cov ruff
29+
pip install build pytest pytest-cov ruff toml
3030
pip install -r requirements.txt
3131
pip install -e .
3232
@@ -41,11 +41,50 @@ jobs:
4141
- name: Build package
4242
run: python -m build
4343

44+
create-tag:
45+
needs: test
46+
runs-on: ubuntu-latest
47+
permissions:
48+
contents: write
49+
if: github.event_name == 'push' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/develop')
50+
51+
steps:
52+
- uses: actions/checkout@v3
53+
with:
54+
token: ${{ secrets.GITHUB_TOKEN }}
55+
fetch-depth: 0
56+
57+
- name: Install dependencies
58+
run: |
59+
python -m pip install --upgrade pip
60+
pip install build toml
61+
62+
63+
- name: Install dependencies
64+
run: |
65+
python -m pip install --upgrade pip
66+
pip install build toml
67+
68+
- name: Get version from pyproject.toml
69+
id: get_version
70+
run: |
71+
version=$(python -c "import toml; print(toml.load('pyproject.toml')['project']['version'])")
72+
echo "version=$version" >> $GITHUB_OUTPUT
73+
74+
- name: Create and push tag
75+
env:
76+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
77+
run: |
78+
git config --local user.email "[email protected]"
79+
git config --local user.name "GitHub Action"
80+
git tag -a "v${{ steps.get_version.outputs.version }}" -m "Release v${{ steps.get_version.outputs.version }}"
81+
git push -f https://${GITHUB_ACTOR}:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git "v${{ steps.get_version.outputs.version }}"
82+
4483
publish:
4584
needs: test
4685
runs-on: ubuntu-latest
4786
if: github.event_name == 'release' && github.event.action == 'created'
48-
87+
4988
steps:
5089
- uses: actions/checkout@v3
5190

@@ -57,8 +96,14 @@ jobs:
5796
- name: Install dependencies
5897
run: |
5998
python -m pip install --upgrade pip
60-
pip install build twine
99+
pip install build pytest pytest-cov ruff twine
100+
pip install -r requirements.txt
101+
pip install -e .
61102
103+
- name: Run tests
104+
run: |
105+
pytest --cov
106+
62107
- name: Build package
63108
run: python -m build
64109

README.md

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
[![Python Versions](https://img.shields.io/pypi/pyversions/chromatrace.svg)]()
1414

1515

16-
Chromatrace is a Python package designed for advanced logging capabilities, including trace and request ID management. It provides a flexible logging configuration and supports colored logging for better visibility.
16+
Chromatrace is a Python package designed for advanced logging capabilities, including trace and request ID management along with process ID. It provides a flexible logging configuration and supports colored logging for better visibility.
1717

1818
I believe that logging is an essential part of any application, and it is crucial to have a well-organized and structured logging system. Chromatrace aims to provide a simple and easy-to-use logging system that can be integrated into any Python application.
1919
In simple terms, Chromatrace is a Best Practice of Logging in Python.
@@ -23,8 +23,13 @@ In simple terms, Chromatrace is a Best Practice of Logging in Python.
2323
- Configurable logging settings using Pydantic.
2424
- Customizable log levels and loggers for different services.
2525
- Support for trace IDs and request IDs.
26+
- Support for process IDs.
2627
- Customizable log formats and handlers.
2728
- Asynchronous and synchronous function tracing.
29+
- Uvicorn integration for logging configuration to customize log settings.
30+
- FastAPI integration for request ID management.
31+
- SocketIO integration for request ID management.
32+
- Practical examples for different frameworks and use cases.
2833

2934
## Installation
3035

@@ -76,13 +81,13 @@ container = Container()
7681

7782
from chromatrace import LoggingConfig, LoggingSettings
7883

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-
)
84+
container[LoggingSettings] = LoggingSettings(
85+
application_level="Development",
86+
enable_tracing=True,
87+
ignore_nan_trace=False,
88+
enable_file_logging=True,
89+
)
90+
container[LoggingConfig] = LoggingConfig(container[LoggingSettings])
8691
```
8792

8893
Then, add the `LoggingConfig` to your service:
@@ -195,6 +200,48 @@ Received message on main namespace. SID: FI3E_S_A-KsTi4RLAAAD, Message: Hello fr
195200
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.
196201

197202

203+
### Uvicorn Integration
204+
205+
```python
206+
from chromatrace.uvicorn import GetLoggingConfig, UvicornLoggingSettings
207+
208+
rest_application = FastAPI()
209+
210+
uvicorn.run(
211+
rest_application,
212+
host="0.0.0.0",
213+
port=8000,
214+
log_level="debug",
215+
log_config=GetLoggingConfig(
216+
UvicornLoggingSettings(
217+
enable_file_logging=True,
218+
show_process_id=True,
219+
)
220+
),
221+
)
222+
```
223+
224+
Result:
225+
```log
226+
(2024-12-12 20:54:54)-[PID:3710345]-[INFO]: Started server process [3710345]
227+
(2024-12-12 20:54:54)-[PID:3710345]-[INFO]: Waiting for application startup.
228+
(2024-12-12 20:54:54)-[PID:3710345]-[INFO]: Application startup complete.
229+
(2024-12-12 20:54:54)-[PID:3710345]-[INFO]: Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
230+
(2024-12-12 20:54:57)-[PID:3710345]-[INFO]: ADDRESS:(127.0.0.1:46166) - REQUEST:"GET /consume HTTP/1.1" - STATUS:200 OK
231+
(2024-12-12 20:55:45)-[PID:3710345]-[INFO]: ADDRESS:(127.0.0.1:54018) - REQUEST:"GET /consume HTTP/1.1" - STATUS:200 OK
232+
(2024-12-12 20:56:51)-[PID:3710345]-[INFO]: ADDRESS:(127.0.0.1:58240) - REQUEST:"GET / HTTP/1.1" - STATUS:200 OK
233+
(2024-12-12 20:56:51)-[PID:3710345]-[INFO]: ADDRESS:(127.0.0.1:58254) - REQUEST:"GET / HTTP/1.1" - STATUS:200 OK
234+
(2024-12-12 20:56:52)-[PID:3710345]-[INFO]: ADDRESS:(127.0.0.1:58260) - REQUEST:"GET / HTTP/1.1" - STATUS:200 OK
235+
(2024-12-12 20:56:52)-[PID:3710345]-[INFO]: ADDRESS:(127.0.0.1:58270) - REQUEST:"GET / HTTP/1.1" - STATUS:200 OK
236+
(2024-12-12 21:16:45)-[PID:3710345]-[INFO]: Shutting down
237+
(2024-12-12 21:16:45)-[PID:3710345]-[INFO]: Waiting for application shutdown.
238+
(2024-12-12 21:16:45)-[PID:3710345]-[INFO]: Application shutdown complete.
239+
(2024-12-12 21:16:45)-[PID:3710345]-[INFO]: Finished server process [3710345]
240+
```
241+
242+
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.
243+
244+
198245
## Examples
199246

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

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
44

55
[project]
66
name = "chromatrace"
7-
version = "0.1.9"
7+
version = "0.2.11"
88
description = "Advanced Python logging with tracing, coloring and FastAPI, Django, and SocketIO integrations"
99
readme = "README.md"
1010
authors = [

src/chromatrace/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@
99
trace_id_ctx,
1010
tracer,
1111
)
12+
from .uvicorn import GetLoggingConfig, UvicornLoggingSettings # noqa: F401

src/chromatrace/logging_config.py

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,8 @@
55

66
from .logging_settings import (
77
ApplicationLevelFilter,
8-
ColoredFormatter,
9-
IgnoreNANTraceFormatter,
8+
BasicFormatter,
109
LoggingSettings,
11-
PlainFormatter,
12-
PlainSysLogFormatter,
1310
SysLogFormatter,
1411
)
1512
from .tracer import RequestIdFilter
@@ -89,18 +86,12 @@ def _setup_syslog_handler(self, logger: logging.Logger):
8986
return
9087

9188
try:
92-
if self.settings.use_syslog_colored_formatter:
93-
syslog_formatter = SysLogFormatter(
94-
fmt=self.settings.log_format,
95-
datefmt=self.settings.date_format,
96-
style=self.settings.style,
97-
)
98-
else:
99-
syslog_formatter = PlainSysLogFormatter(
100-
fmt=self.settings.log_format,
101-
datefmt=self.settings.date_format,
102-
style=self.settings.style,
103-
)
89+
syslog_formatter = SysLogFormatter(
90+
fmt=self.settings.log_format,
91+
datefmt=self.settings.date_format,
92+
style=self.settings.style,
93+
colored=self.settings.use_syslog_colored_formatter,
94+
)
10495

10596
# Create handler with socket handling
10697
syslog_handler = logging.handlers.SysLogHandler(
@@ -127,23 +118,16 @@ def _setup_syslog_handler(self, logger: logging.Logger):
127118
logger.warning(f"Failed to setup syslog handler: {str(e)}")
128119
print(f"Failed to setup syslog handler: {str(e)}")
129120

130-
def _get_formatter(self, colored: bool = False):
131-
if colored and self.settings.enable_tracing and self.settings.ignore_nan_trace:
132-
return IgnoreNANTraceFormatter(
133-
fmt=self.settings.log_format,
134-
datefmt=self.settings.date_format,
135-
style=self.settings.style,
136-
)
137-
elif colored:
138-
return ColoredFormatter(
139-
fmt=self.settings.log_format,
140-
datefmt=self.settings.date_format,
141-
style=self.settings.style,
142-
)
143-
return PlainFormatter(
121+
def _get_formatter(
122+
self,
123+
colored: bool = False,
124+
):
125+
return BasicFormatter(
144126
fmt=self.settings.log_format,
145127
datefmt=self.settings.date_format,
146128
style=self.settings.style,
129+
colored=colored,
130+
remove_nan_trace=self.settings.ignore_nan_trace,
147131
)
148132

149133
def _setup_handlers(self, logger: logging.Logger):

0 commit comments

Comments
 (0)