lib-x-xlogging 1.6.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- lib_x_xlogging-1.6.0/LICENSE +21 -0
- lib_x_xlogging-1.6.0/PKG-INFO +91 -0
- lib_x_xlogging-1.6.0/README.md +71 -0
- lib_x_xlogging-1.6.0/lib_x_xlogging.egg-info/PKG-INFO +91 -0
- lib_x_xlogging-1.6.0/lib_x_xlogging.egg-info/SOURCES.txt +34 -0
- lib_x_xlogging-1.6.0/lib_x_xlogging.egg-info/dependency_links.txt +1 -0
- lib_x_xlogging-1.6.0/lib_x_xlogging.egg-info/requires.txt +10 -0
- lib_x_xlogging-1.6.0/lib_x_xlogging.egg-info/top_level.txt +1 -0
- lib_x_xlogging-1.6.0/pyproject.toml +59 -0
- lib_x_xlogging-1.6.0/setup.cfg +4 -0
- lib_x_xlogging-1.6.0/xlogging/__init__.py +22 -0
- lib_x_xlogging-1.6.0/xlogging/format/__init__.py +11 -0
- lib_x_xlogging-1.6.0/xlogging/format/_common.py +219 -0
- lib_x_xlogging-1.6.0/xlogging/format/color.py +59 -0
- lib_x_xlogging-1.6.0/xlogging/format/colortree.py +85 -0
- lib_x_xlogging-1.6.0/xlogging/format/json.py +87 -0
- lib_x_xlogging-1.6.0/xlogging/format/tests/__init__.py +1 -0
- lib_x_xlogging-1.6.0/xlogging/format/tests/test_color_unit.py +169 -0
- lib_x_xlogging-1.6.0/xlogging/format/tests/test_colortree_unit.py +279 -0
- lib_x_xlogging-1.6.0/xlogging/format/tests/test_json_unit.py +149 -0
- lib_x_xlogging-1.6.0/xlogging/format/tests/test_tree_unit.py +233 -0
- lib_x_xlogging-1.6.0/xlogging/format/tree.py +143 -0
- lib_x_xlogging-1.6.0/xlogging/handler/__init__.py +7 -0
- lib_x_xlogging-1.6.0/xlogging/handler/console.py +40 -0
- lib_x_xlogging-1.6.0/xlogging/handler/file.py +89 -0
- lib_x_xlogging-1.6.0/xlogging/handler/tests/__init__.py +1 -0
- lib_x_xlogging-1.6.0/xlogging/handler/tests/test_console_unit.py +179 -0
- lib_x_xlogging-1.6.0/xlogging/handler/tests/test_file_unit.py +176 -0
- lib_x_xlogging-1.6.0/xlogging/record/__init__.py +5 -0
- lib_x_xlogging-1.6.0/xlogging/record/event.py +54 -0
- lib_x_xlogging-1.6.0/xlogging/record/tests/__init__.py +1 -0
- lib_x_xlogging-1.6.0/xlogging/record/tests/test_event_unit.py +470 -0
- lib_x_xlogging-1.6.0/xlogging/stream/__init__.py +5 -0
- lib_x_xlogging-1.6.0/xlogging/stream/event.py +263 -0
- lib_x_xlogging-1.6.0/xlogging/stream/tests/__init__.py +1 -0
- lib_x_xlogging-1.6.0/xlogging/stream/tests/test_event_unit.py +569 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 XING GNIX
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: lib-x-xlogging
|
|
3
|
+
Version: 1.6.0
|
|
4
|
+
Summary: Lightweight python logging extension.
|
|
5
|
+
Author-email: Xing Xing <x.xing.work@gmail.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Requires-Python: >=3.11
|
|
8
|
+
Description-Content-Type: text/markdown
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Requires-Dist: colorlog
|
|
11
|
+
Provides-Extra: dev
|
|
12
|
+
Requires-Dist: pytest>=8.0; extra == "dev"
|
|
13
|
+
Requires-Dist: pytest-mock; extra == "dev"
|
|
14
|
+
Requires-Dist: ruff; extra == "dev"
|
|
15
|
+
Requires-Dist: setuptools; extra == "dev"
|
|
16
|
+
Requires-Dist: wheel; extra == "dev"
|
|
17
|
+
Requires-Dist: twine; extra == "dev"
|
|
18
|
+
Requires-Dist: build; extra == "dev"
|
|
19
|
+
Dynamic: license-file
|
|
20
|
+
|
|
21
|
+
# xlogging
|
|
22
|
+
|
|
23
|
+
A lightweight extension on top of Python's built-in `logging` module. Structured, readable output for local dev and production services.
|
|
24
|
+
|
|
25
|
+
## What it solves
|
|
26
|
+
|
|
27
|
+
stdlib `logging` emits flat, hard-to-read text. When a log record carries nested context — request metadata, exception details, tags — there's no clean way to display or store it. `xlogging` adds structured formatters and a first-class `LogEvent` record so that context travels with the message.
|
|
28
|
+
|
|
29
|
+
## Key features
|
|
30
|
+
|
|
31
|
+
- **TreeFormat / ColorTreeFormat** — renders nested dicts and lists as a Unicode tree, readable in terminal
|
|
32
|
+
- **JsonFormat** — emits newline-delimited JSON for file storage and log aggregators
|
|
33
|
+
- **EventStream** — a `Logger` subclass that accepts `metadata` and `tags` per call, and supports per-logger timezone
|
|
34
|
+
- **LogEvent** — a `LogRecord` subclass with `created_at`, `metadata`, and `tags` fields
|
|
35
|
+
- **FileGroup** — rotating file handler that defaults to JSON format
|
|
36
|
+
- **ConsoleGroup** — stream handler with pluggable formatter
|
|
37
|
+
|
|
38
|
+
## Usage
|
|
39
|
+
|
|
40
|
+
```python
|
|
41
|
+
from xlogging import EventStream, ColorTreeFormat, ConsoleGroup
|
|
42
|
+
|
|
43
|
+
stream = EventStream.from_options(
|
|
44
|
+
name="app",
|
|
45
|
+
handlers=[ConsoleGroup(format=ColorTreeFormat())],
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
stream.info(
|
|
49
|
+
"order filled",
|
|
50
|
+
metadata={"symbol": "AAPL", "qty": 100, "price": 189.5},
|
|
51
|
+
tags={"strategy": "momentum"},
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
stream.error(
|
|
55
|
+
"execution failed",
|
|
56
|
+
metadata={"order_id": "x-001", "reason": "insufficient margin"},
|
|
57
|
+
)
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
Terminal output:
|
|
61
|
+
|
|
62
|
+
```
|
|
63
|
+
[2026-08-31 09:00:00][INFO]: order filled
|
|
64
|
+
├── metadata:
|
|
65
|
+
│ ├── symbol: AAPL
|
|
66
|
+
│ ├── qty: 100
|
|
67
|
+
│ └── price: 189.5
|
|
68
|
+
└── tags:
|
|
69
|
+
└── strategy: momentum
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### File handler
|
|
73
|
+
|
|
74
|
+
```python
|
|
75
|
+
from xlogging import EventStream, FileGroup
|
|
76
|
+
|
|
77
|
+
stream = EventStream.from_options(
|
|
78
|
+
name="app",
|
|
79
|
+
handlers=[FileGroup(path="logs/", name="app")],
|
|
80
|
+
)
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
Writes rotating JSON logs to `logs/app.log` (10 MB per file, 1 backup by default).
|
|
84
|
+
|
|
85
|
+
## Install
|
|
86
|
+
|
|
87
|
+
```bash
|
|
88
|
+
pip install lib-x-xlogging
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
Requires Python 3.11+. Runtime dependency: `colorlog`.
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# xlogging
|
|
2
|
+
|
|
3
|
+
A lightweight extension on top of Python's built-in `logging` module. Structured, readable output for local dev and production services.
|
|
4
|
+
|
|
5
|
+
## What it solves
|
|
6
|
+
|
|
7
|
+
stdlib `logging` emits flat, hard-to-read text. When a log record carries nested context — request metadata, exception details, tags — there's no clean way to display or store it. `xlogging` adds structured formatters and a first-class `LogEvent` record so that context travels with the message.
|
|
8
|
+
|
|
9
|
+
## Key features
|
|
10
|
+
|
|
11
|
+
- **TreeFormat / ColorTreeFormat** — renders nested dicts and lists as a Unicode tree, readable in terminal
|
|
12
|
+
- **JsonFormat** — emits newline-delimited JSON for file storage and log aggregators
|
|
13
|
+
- **EventStream** — a `Logger` subclass that accepts `metadata` and `tags` per call, and supports per-logger timezone
|
|
14
|
+
- **LogEvent** — a `LogRecord` subclass with `created_at`, `metadata`, and `tags` fields
|
|
15
|
+
- **FileGroup** — rotating file handler that defaults to JSON format
|
|
16
|
+
- **ConsoleGroup** — stream handler with pluggable formatter
|
|
17
|
+
|
|
18
|
+
## Usage
|
|
19
|
+
|
|
20
|
+
```python
|
|
21
|
+
from xlogging import EventStream, ColorTreeFormat, ConsoleGroup
|
|
22
|
+
|
|
23
|
+
stream = EventStream.from_options(
|
|
24
|
+
name="app",
|
|
25
|
+
handlers=[ConsoleGroup(format=ColorTreeFormat())],
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
stream.info(
|
|
29
|
+
"order filled",
|
|
30
|
+
metadata={"symbol": "AAPL", "qty": 100, "price": 189.5},
|
|
31
|
+
tags={"strategy": "momentum"},
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
stream.error(
|
|
35
|
+
"execution failed",
|
|
36
|
+
metadata={"order_id": "x-001", "reason": "insufficient margin"},
|
|
37
|
+
)
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Terminal output:
|
|
41
|
+
|
|
42
|
+
```
|
|
43
|
+
[2026-08-31 09:00:00][INFO]: order filled
|
|
44
|
+
├── metadata:
|
|
45
|
+
│ ├── symbol: AAPL
|
|
46
|
+
│ ├── qty: 100
|
|
47
|
+
│ └── price: 189.5
|
|
48
|
+
└── tags:
|
|
49
|
+
└── strategy: momentum
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### File handler
|
|
53
|
+
|
|
54
|
+
```python
|
|
55
|
+
from xlogging import EventStream, FileGroup
|
|
56
|
+
|
|
57
|
+
stream = EventStream.from_options(
|
|
58
|
+
name="app",
|
|
59
|
+
handlers=[FileGroup(path="logs/", name="app")],
|
|
60
|
+
)
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
Writes rotating JSON logs to `logs/app.log` (10 MB per file, 1 backup by default).
|
|
64
|
+
|
|
65
|
+
## Install
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
pip install lib-x-xlogging
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
Requires Python 3.11+. Runtime dependency: `colorlog`.
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: lib-x-xlogging
|
|
3
|
+
Version: 1.6.0
|
|
4
|
+
Summary: Lightweight python logging extension.
|
|
5
|
+
Author-email: Xing Xing <x.xing.work@gmail.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Requires-Python: >=3.11
|
|
8
|
+
Description-Content-Type: text/markdown
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Requires-Dist: colorlog
|
|
11
|
+
Provides-Extra: dev
|
|
12
|
+
Requires-Dist: pytest>=8.0; extra == "dev"
|
|
13
|
+
Requires-Dist: pytest-mock; extra == "dev"
|
|
14
|
+
Requires-Dist: ruff; extra == "dev"
|
|
15
|
+
Requires-Dist: setuptools; extra == "dev"
|
|
16
|
+
Requires-Dist: wheel; extra == "dev"
|
|
17
|
+
Requires-Dist: twine; extra == "dev"
|
|
18
|
+
Requires-Dist: build; extra == "dev"
|
|
19
|
+
Dynamic: license-file
|
|
20
|
+
|
|
21
|
+
# xlogging
|
|
22
|
+
|
|
23
|
+
A lightweight extension on top of Python's built-in `logging` module. Structured, readable output for local dev and production services.
|
|
24
|
+
|
|
25
|
+
## What it solves
|
|
26
|
+
|
|
27
|
+
stdlib `logging` emits flat, hard-to-read text. When a log record carries nested context — request metadata, exception details, tags — there's no clean way to display or store it. `xlogging` adds structured formatters and a first-class `LogEvent` record so that context travels with the message.
|
|
28
|
+
|
|
29
|
+
## Key features
|
|
30
|
+
|
|
31
|
+
- **TreeFormat / ColorTreeFormat** — renders nested dicts and lists as a Unicode tree, readable in terminal
|
|
32
|
+
- **JsonFormat** — emits newline-delimited JSON for file storage and log aggregators
|
|
33
|
+
- **EventStream** — a `Logger` subclass that accepts `metadata` and `tags` per call, and supports per-logger timezone
|
|
34
|
+
- **LogEvent** — a `LogRecord` subclass with `created_at`, `metadata`, and `tags` fields
|
|
35
|
+
- **FileGroup** — rotating file handler that defaults to JSON format
|
|
36
|
+
- **ConsoleGroup** — stream handler with pluggable formatter
|
|
37
|
+
|
|
38
|
+
## Usage
|
|
39
|
+
|
|
40
|
+
```python
|
|
41
|
+
from xlogging import EventStream, ColorTreeFormat, ConsoleGroup
|
|
42
|
+
|
|
43
|
+
stream = EventStream.from_options(
|
|
44
|
+
name="app",
|
|
45
|
+
handlers=[ConsoleGroup(format=ColorTreeFormat())],
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
stream.info(
|
|
49
|
+
"order filled",
|
|
50
|
+
metadata={"symbol": "AAPL", "qty": 100, "price": 189.5},
|
|
51
|
+
tags={"strategy": "momentum"},
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
stream.error(
|
|
55
|
+
"execution failed",
|
|
56
|
+
metadata={"order_id": "x-001", "reason": "insufficient margin"},
|
|
57
|
+
)
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
Terminal output:
|
|
61
|
+
|
|
62
|
+
```
|
|
63
|
+
[2026-08-31 09:00:00][INFO]: order filled
|
|
64
|
+
├── metadata:
|
|
65
|
+
│ ├── symbol: AAPL
|
|
66
|
+
│ ├── qty: 100
|
|
67
|
+
│ └── price: 189.5
|
|
68
|
+
└── tags:
|
|
69
|
+
└── strategy: momentum
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### File handler
|
|
73
|
+
|
|
74
|
+
```python
|
|
75
|
+
from xlogging import EventStream, FileGroup
|
|
76
|
+
|
|
77
|
+
stream = EventStream.from_options(
|
|
78
|
+
name="app",
|
|
79
|
+
handlers=[FileGroup(path="logs/", name="app")],
|
|
80
|
+
)
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
Writes rotating JSON logs to `logs/app.log` (10 MB per file, 1 backup by default).
|
|
84
|
+
|
|
85
|
+
## Install
|
|
86
|
+
|
|
87
|
+
```bash
|
|
88
|
+
pip install lib-x-xlogging
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
Requires Python 3.11+. Runtime dependency: `colorlog`.
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
lib_x_xlogging.egg-info/PKG-INFO
|
|
5
|
+
lib_x_xlogging.egg-info/SOURCES.txt
|
|
6
|
+
lib_x_xlogging.egg-info/dependency_links.txt
|
|
7
|
+
lib_x_xlogging.egg-info/requires.txt
|
|
8
|
+
lib_x_xlogging.egg-info/top_level.txt
|
|
9
|
+
xlogging/__init__.py
|
|
10
|
+
xlogging/format/__init__.py
|
|
11
|
+
xlogging/format/_common.py
|
|
12
|
+
xlogging/format/color.py
|
|
13
|
+
xlogging/format/colortree.py
|
|
14
|
+
xlogging/format/json.py
|
|
15
|
+
xlogging/format/tree.py
|
|
16
|
+
xlogging/format/tests/__init__.py
|
|
17
|
+
xlogging/format/tests/test_color_unit.py
|
|
18
|
+
xlogging/format/tests/test_colortree_unit.py
|
|
19
|
+
xlogging/format/tests/test_json_unit.py
|
|
20
|
+
xlogging/format/tests/test_tree_unit.py
|
|
21
|
+
xlogging/handler/__init__.py
|
|
22
|
+
xlogging/handler/console.py
|
|
23
|
+
xlogging/handler/file.py
|
|
24
|
+
xlogging/handler/tests/__init__.py
|
|
25
|
+
xlogging/handler/tests/test_console_unit.py
|
|
26
|
+
xlogging/handler/tests/test_file_unit.py
|
|
27
|
+
xlogging/record/__init__.py
|
|
28
|
+
xlogging/record/event.py
|
|
29
|
+
xlogging/record/tests/__init__.py
|
|
30
|
+
xlogging/record/tests/test_event_unit.py
|
|
31
|
+
xlogging/stream/__init__.py
|
|
32
|
+
xlogging/stream/event.py
|
|
33
|
+
xlogging/stream/tests/__init__.py
|
|
34
|
+
xlogging/stream/tests/test_event_unit.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
xlogging
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "lib-x-xlogging"
|
|
3
|
+
version = "1.6.0"
|
|
4
|
+
description = "Lightweight python logging extension."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
authors = [
|
|
7
|
+
{ name = "Xing Xing", email = "x.xing.work@gmail.com" }
|
|
8
|
+
]
|
|
9
|
+
license = "MIT"
|
|
10
|
+
license-files = ["LICENSE"]
|
|
11
|
+
requires-python = ">=3.11"
|
|
12
|
+
|
|
13
|
+
dependencies = [
|
|
14
|
+
"colorlog"
|
|
15
|
+
]
|
|
16
|
+
|
|
17
|
+
[project.optional-dependencies]
|
|
18
|
+
dev = [
|
|
19
|
+
"pytest>=8.0",
|
|
20
|
+
"pytest-mock",
|
|
21
|
+
"ruff",
|
|
22
|
+
"setuptools",
|
|
23
|
+
"wheel",
|
|
24
|
+
"twine",
|
|
25
|
+
"build",
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
[tool.pytest.ini_options]
|
|
29
|
+
pythonpath = ["."]
|
|
30
|
+
testpaths = ["tests"]
|
|
31
|
+
|
|
32
|
+
[build-system]
|
|
33
|
+
requires = [
|
|
34
|
+
"setuptools",
|
|
35
|
+
"wheel",
|
|
36
|
+
"twine",
|
|
37
|
+
"build"
|
|
38
|
+
]
|
|
39
|
+
build-backend = "setuptools.build_meta"
|
|
40
|
+
|
|
41
|
+
[tool.setuptools]
|
|
42
|
+
include-package-data = true
|
|
43
|
+
|
|
44
|
+
[tool.setuptools.packages.find]
|
|
45
|
+
where = ["."]
|
|
46
|
+
include = ["xlogging*"]
|
|
47
|
+
exclude = ["tests*"]
|
|
48
|
+
|
|
49
|
+
[tool.ruff]
|
|
50
|
+
line-length = 100
|
|
51
|
+
target-version = "py311"
|
|
52
|
+
fix = true
|
|
53
|
+
|
|
54
|
+
[tool.ruff.lint]
|
|
55
|
+
select = ["E", "F", "I"]
|
|
56
|
+
ignore = ["E501"]
|
|
57
|
+
|
|
58
|
+
[tool.ruff.lint.isort]
|
|
59
|
+
force-single-line = false
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from xlogging.format import (
|
|
4
|
+
ColorFormat,
|
|
5
|
+
ColorTreeFormat,
|
|
6
|
+
JsonFormat,
|
|
7
|
+
TreeFormat,
|
|
8
|
+
)
|
|
9
|
+
from xlogging.handler import ConsoleGroup, FileGroup
|
|
10
|
+
from xlogging.record import LogEvent
|
|
11
|
+
from xlogging.stream import EventStream
|
|
12
|
+
|
|
13
|
+
__all__ = [
|
|
14
|
+
"ColorFormat",
|
|
15
|
+
"ColorTreeFormat",
|
|
16
|
+
"JsonFormat",
|
|
17
|
+
"TreeFormat",
|
|
18
|
+
"ConsoleGroup",
|
|
19
|
+
"FileGroup",
|
|
20
|
+
"LogEvent",
|
|
21
|
+
"EventStream",
|
|
22
|
+
]
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
from xlogging.format.color import ColorFormat
|
|
2
|
+
from xlogging.format.colortree import ColorTreeFormat
|
|
3
|
+
from xlogging.format.json import JsonFormat
|
|
4
|
+
from xlogging.format.tree import TreeFormat
|
|
5
|
+
|
|
6
|
+
__all__ = [
|
|
7
|
+
"ColorFormat",
|
|
8
|
+
"JsonFormat",
|
|
9
|
+
"TreeFormat",
|
|
10
|
+
"ColorTreeFormat",
|
|
11
|
+
]
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import logging
|
|
4
|
+
import traceback
|
|
5
|
+
from datetime import datetime
|
|
6
|
+
from typing import Any
|
|
7
|
+
from zoneinfo import ZoneInfo
|
|
8
|
+
|
|
9
|
+
from colorlog.escape_codes import escape_codes as _esc
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class IColorConfig:
|
|
13
|
+
"""
|
|
14
|
+
Color configuration for logging.
|
|
15
|
+
|
|
16
|
+
Attributes:
|
|
17
|
+
color_config (dict[str, str]):
|
|
18
|
+
A dictionary mapping log levels to color codes.
|
|
19
|
+
color_reset (str):
|
|
20
|
+
The escape code to reset color formatting.
|
|
21
|
+
|
|
22
|
+
Methods:
|
|
23
|
+
resolve_colors_config(colors: dict[str, str] | None = None) -> dict[str, str]:
|
|
24
|
+
Resolves the color configuration based on the provided colors or defaults.
|
|
25
|
+
get_color_reset() -> str:
|
|
26
|
+
Returns the escape code to reset color formatting.
|
|
27
|
+
get_default_colors() -> dict[str, str]:
|
|
28
|
+
Returns the default color configuration.
|
|
29
|
+
|
|
30
|
+
"""
|
|
31
|
+
|
|
32
|
+
color_config: dict[str, str] = {
|
|
33
|
+
"DEBUG": "cyan",
|
|
34
|
+
"INFO": "38;5;244",
|
|
35
|
+
"WARNING": "38;5;208",
|
|
36
|
+
"ERROR": "bold_red",
|
|
37
|
+
"CRITICAL": "purple",
|
|
38
|
+
}
|
|
39
|
+
color_reset: str = _esc.get("reset", "\033[0m")
|
|
40
|
+
|
|
41
|
+
@staticmethod
|
|
42
|
+
def resolve_colors_config(
|
|
43
|
+
colors: dict[str, str] | None = None,
|
|
44
|
+
) -> dict[str, str]:
|
|
45
|
+
if colors is not None:
|
|
46
|
+
source = colors
|
|
47
|
+
else:
|
|
48
|
+
source = IColorConfig.color_config
|
|
49
|
+
|
|
50
|
+
resolved = {}
|
|
51
|
+
for lvl, color in source.items():
|
|
52
|
+
if color in _esc:
|
|
53
|
+
resolved[lvl] = _esc[color]
|
|
54
|
+
else:
|
|
55
|
+
resolved[lvl] = f"\033[{color}m"
|
|
56
|
+
return resolved
|
|
57
|
+
|
|
58
|
+
@staticmethod
|
|
59
|
+
def get_color_reset() -> str:
|
|
60
|
+
return IColorConfig.color_reset
|
|
61
|
+
|
|
62
|
+
@staticmethod
|
|
63
|
+
def get_default_colors() -> dict[str, str]:
|
|
64
|
+
return IColorConfig.color_config
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
class IFormatConfig:
|
|
68
|
+
"""
|
|
69
|
+
Format configuration for logging.
|
|
70
|
+
|
|
71
|
+
Attributes:
|
|
72
|
+
format (str):
|
|
73
|
+
The default log message format.
|
|
74
|
+
colored_format (str):
|
|
75
|
+
The default colored log message format.
|
|
76
|
+
date_format (str):
|
|
77
|
+
The default date format for log messages.
|
|
78
|
+
|
|
79
|
+
Methods:
|
|
80
|
+
resolve_date_format(datefmt: str | None = None) -> str:
|
|
81
|
+
Resolves the date format based on the provided datefmt or defaults.
|
|
82
|
+
resolve_format(format: str | None = None) -> str:
|
|
83
|
+
Resolves the log message format based on the provided format or defaults.
|
|
84
|
+
resolve_colored_format(format: str | None = None) -> str:
|
|
85
|
+
Resolves the colored log message format based on the provided format or defaults.
|
|
86
|
+
|
|
87
|
+
"""
|
|
88
|
+
|
|
89
|
+
format: str = "[%(asctime)s][%(levelname)s] %(message)s"
|
|
90
|
+
colored_format: str = "%(log_color)s[%(asctime)s][%(levelname)s] %(message)s%(reset)s"
|
|
91
|
+
date_format: str = "%Y-%m-%d %H:%M:%S"
|
|
92
|
+
|
|
93
|
+
@staticmethod
|
|
94
|
+
def resolve_date_format(
|
|
95
|
+
datefmt: str | None = None,
|
|
96
|
+
) -> str:
|
|
97
|
+
return datefmt or IFormatConfig.date_format
|
|
98
|
+
|
|
99
|
+
@staticmethod
|
|
100
|
+
def resolve_format(
|
|
101
|
+
format: str | None = None,
|
|
102
|
+
) -> str:
|
|
103
|
+
return format or IFormatConfig.format
|
|
104
|
+
|
|
105
|
+
@staticmethod
|
|
106
|
+
def resolve_colored_format(
|
|
107
|
+
format: str | None = None,
|
|
108
|
+
) -> str:
|
|
109
|
+
return format or IFormatConfig.colored_format
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
class ITreeConfig:
|
|
113
|
+
"""
|
|
114
|
+
Internal Tree configuration
|
|
115
|
+
|
|
116
|
+
Attributes:
|
|
117
|
+
tree_branch (str):
|
|
118
|
+
The string representation of a tree branch.
|
|
119
|
+
tree_last (str):
|
|
120
|
+
The string representation of the last branch in a tree.
|
|
121
|
+
tree_pipe (str):
|
|
122
|
+
The string representation of a tree pipe.
|
|
123
|
+
tree_space (str):
|
|
124
|
+
The string representation of a tree space.
|
|
125
|
+
|
|
126
|
+
"""
|
|
127
|
+
|
|
128
|
+
tree_branch: str = "├── "
|
|
129
|
+
tree_last: str = "└── "
|
|
130
|
+
tree_pipe: str = "│ "
|
|
131
|
+
tree_space: str = " "
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
class ILogRecord:
|
|
135
|
+
"""
|
|
136
|
+
Internal LogRecord utility class for logging.
|
|
137
|
+
|
|
138
|
+
"""
|
|
139
|
+
|
|
140
|
+
@staticmethod
|
|
141
|
+
def serialise_exc(
|
|
142
|
+
record: logging.LogRecord,
|
|
143
|
+
tb_as_str: bool = False,
|
|
144
|
+
) -> dict[str, Any] | None:
|
|
145
|
+
_exc_info = getattr(record, "exc_info", None)
|
|
146
|
+
if not _exc_info or _exc_info[0] is None:
|
|
147
|
+
return None
|
|
148
|
+
_type, _value, _tb = _exc_info
|
|
149
|
+
_result = {}
|
|
150
|
+
_result["type"] = _type.__qualname__
|
|
151
|
+
_result["message"] = str(_value)
|
|
152
|
+
_tb_lines: list[str] = []
|
|
153
|
+
for chunk in traceback.format_tb(_tb):
|
|
154
|
+
for line in chunk.splitlines():
|
|
155
|
+
if isinstance(line, str):
|
|
156
|
+
_tb_lines.append(line.strip())
|
|
157
|
+
if tb_as_str:
|
|
158
|
+
_result["traceback"] = "\n".join(_tb_lines)
|
|
159
|
+
else:
|
|
160
|
+
_result["traceback"] = _tb_lines
|
|
161
|
+
return _result
|
|
162
|
+
|
|
163
|
+
@staticmethod
|
|
164
|
+
def serialise_cdt(
|
|
165
|
+
record: logging.LogRecord,
|
|
166
|
+
date_format: str | None = None,
|
|
167
|
+
) -> str:
|
|
168
|
+
_date_format = IFormatConfig.resolve_date_format(date_format)
|
|
169
|
+
_o_ts: float = record.created
|
|
170
|
+
_o_cdt: datetime = datetime.fromtimestamp(_o_ts, tz=ZoneInfo("UTC"))
|
|
171
|
+
_e_cdt: datetime | None = getattr(record, "created_at", None)
|
|
172
|
+
_cdt: str = (_e_cdt or _o_cdt).strftime(_date_format)
|
|
173
|
+
return _cdt
|
|
174
|
+
|
|
175
|
+
@staticmethod
|
|
176
|
+
def serialise_all(
|
|
177
|
+
record: logging.LogRecord,
|
|
178
|
+
date_format: str | None = None,
|
|
179
|
+
tb_as_str: bool = False,
|
|
180
|
+
ignore_unset: bool = False,
|
|
181
|
+
reduced_attrs: bool = False,
|
|
182
|
+
) -> dict[str, Any]:
|
|
183
|
+
_cdt: str = ILogRecord.serialise_cdt(
|
|
184
|
+
record=record,
|
|
185
|
+
date_format=date_format,
|
|
186
|
+
)
|
|
187
|
+
_exc: dict[str, Any] | None = ILogRecord.serialise_exc(
|
|
188
|
+
record=record,
|
|
189
|
+
tb_as_str=tb_as_str,
|
|
190
|
+
)
|
|
191
|
+
_msg: str = record.getMessage()
|
|
192
|
+
if reduced_attrs:
|
|
193
|
+
_result = {
|
|
194
|
+
"message": _msg,
|
|
195
|
+
"created_at": _cdt,
|
|
196
|
+
"level": record.levelname,
|
|
197
|
+
"exception": _exc,
|
|
198
|
+
"metadata": getattr(record, "metadata", None),
|
|
199
|
+
"tags": getattr(record, "tags", None),
|
|
200
|
+
}
|
|
201
|
+
else:
|
|
202
|
+
_result = {
|
|
203
|
+
"message": _msg,
|
|
204
|
+
"created_at": _cdt,
|
|
205
|
+
"level": record.levelname,
|
|
206
|
+
"line_no": record.lineno,
|
|
207
|
+
"func_name": record.funcName,
|
|
208
|
+
"module": getattr(record, "module", None),
|
|
209
|
+
"stack_info": getattr(record, "stack_info", None),
|
|
210
|
+
"exception": _exc,
|
|
211
|
+
"metadata": getattr(record, "metadata", None),
|
|
212
|
+
"tags": getattr(record, "tags", None),
|
|
213
|
+
# "thread": record.thread,
|
|
214
|
+
# "thread_name": record.threadName,
|
|
215
|
+
# "process": record.process,
|
|
216
|
+
}
|
|
217
|
+
if ignore_unset:
|
|
218
|
+
_result = {k: v for k, v in _result.items() if v is not None}
|
|
219
|
+
return _result
|