prismalog 0.1.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.
- prismalog-0.1.0/LICENSE +21 -0
- prismalog-0.1.0/PKG-INFO +184 -0
- prismalog-0.1.0/README.md +113 -0
- prismalog-0.1.0/prismalog/__init__.py +35 -0
- prismalog-0.1.0/prismalog/argparser.py +216 -0
- prismalog-0.1.0/prismalog/config.py +783 -0
- prismalog-0.1.0/prismalog/log.py +927 -0
- prismalog-0.1.0/prismalog/py.typed +0 -0
- prismalog-0.1.0/prismalog.egg-info/PKG-INFO +184 -0
- prismalog-0.1.0/prismalog.egg-info/SOURCES.txt +30 -0
- prismalog-0.1.0/prismalog.egg-info/dependency_links.txt +1 -0
- prismalog-0.1.0/prismalog.egg-info/requires.txt +54 -0
- prismalog-0.1.0/prismalog.egg-info/top_level.txt +1 -0
- prismalog-0.1.0/pyproject.toml +146 -0
- prismalog-0.1.0/setup.cfg +4 -0
- prismalog-0.1.0/setup.py +10 -0
- prismalog-0.1.0/tests/test_config.py +269 -0
- prismalog-0.1.0/tests/test_config_checking_priority.py +238 -0
- prismalog-0.1.0/tests/test_config_priority.py +128 -0
- prismalog-0.1.0/tests/test_config_processing.py +176 -0
- prismalog-0.1.0/tests/test_configurations.py +911 -0
- prismalog-0.1.0/tests/test_edge_cases.py +129 -0
- prismalog-0.1.0/tests/test_edge_cases_pytest.py +120 -0
- prismalog-0.1.0/tests/test_fixtures.py +118 -0
- prismalog-0.1.0/tests/test_integration.py +95 -0
- prismalog-0.1.0/tests/test_log.py +529 -0
- prismalog-0.1.0/tests/test_log_format.py +126 -0
- prismalog-0.1.0/tests/test_log_format_isolated.py +421 -0
- prismalog-0.1.0/tests/test_mixed_concurrency.py +182 -0
- prismalog-0.1.0/tests/test_multithreading.py +83 -0
- prismalog-0.1.0/tests/test_prio.py +156 -0
- prismalog-0.1.0/tests/test_stress.py +123 -0
prismalog-0.1.0/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2025 Alexey Obukhov
|
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.
|
prismalog-0.1.0/PKG-INFO
ADDED
@@ -0,0 +1,184 @@
|
|
1
|
+
Metadata-Version: 2.4
|
2
|
+
Name: prismalog
|
3
|
+
Version: 0.1.0
|
4
|
+
Summary: High-performance colored, multi-process logging library for Python
|
5
|
+
Author-email: Alexey Obukhov <alexey.obukhov@hotmail.com>
|
6
|
+
License: MIT
|
7
|
+
Project-URL: Homepage, https://github.com/vertok/prismalog
|
8
|
+
Project-URL: Documentation, https://prismalog.readthedocs.io/
|
9
|
+
Project-URL: Issues, https://github.com/vertok/prismalog/issues
|
10
|
+
Project-URL: Changelog, https://github.com/vertok/prismalog/blob/main/CHANGELOG.md
|
11
|
+
Keywords: logging,colored,high-performance,multiprocessing
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
13
|
+
Classifier: Programming Language :: Python :: 3.8
|
14
|
+
Classifier: Programming Language :: Python :: 3.10
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
16
|
+
Classifier: License :: OSI Approved :: MIT License
|
17
|
+
Classifier: Operating System :: OS Independent
|
18
|
+
Classifier: Topic :: System :: Logging
|
19
|
+
Requires-Python: >=3.8
|
20
|
+
Description-Content-Type: text/markdown
|
21
|
+
License-File: LICENSE
|
22
|
+
Provides-Extra: yaml
|
23
|
+
Requires-Dist: PyYAML>=6.0.1; extra == "yaml"
|
24
|
+
Provides-Extra: dev
|
25
|
+
Requires-Dist: black>=22.6.0; extra == "dev"
|
26
|
+
Requires-Dist: pylint>=2.14.0; extra == "dev"
|
27
|
+
Requires-Dist: mypy>=1.0.0; extra == "dev"
|
28
|
+
Requires-Dist: isort>=5.10.0; extra == "dev"
|
29
|
+
Requires-Dist: build>=0.8.0; extra == "dev"
|
30
|
+
Requires-Dist: twine>=4.0.0; extra == "dev"
|
31
|
+
Requires-Dist: flake8>=5.0.0; extra == "dev"
|
32
|
+
Requires-Dist: pre-commit>=3.5.0; extra == "dev"
|
33
|
+
Requires-Dist: PyYAML>=6.0.2; extra == "dev"
|
34
|
+
Provides-Extra: test
|
35
|
+
Requires-Dist: pytest>=8.3.5; extra == "test"
|
36
|
+
Requires-Dist: pytest-cov>=3.0.0; extra == "test"
|
37
|
+
Requires-Dist: pytest-html>=3.2.0; extra == "test"
|
38
|
+
Requires-Dist: pytest-timeout>=2.1.0; extra == "test"
|
39
|
+
Requires-Dist: coverage>=7.6.1; extra == "test"
|
40
|
+
Requires-Dist: nltk>=3.9.1; extra == "test"
|
41
|
+
Requires-Dist: PyYAML>=6.0.2; extra == "test"
|
42
|
+
Provides-Extra: doc
|
43
|
+
Requires-Dist: sphinx>=4.5.0; extra == "doc"
|
44
|
+
Requires-Dist: sphinx-rtd-theme>=1.0.0; extra == "doc"
|
45
|
+
Requires-Dist: sphinx-autodoc-typehints>=1.18.0; extra == "doc"
|
46
|
+
Requires-Dist: sphinx-design>=0.5.0; extra == "doc"
|
47
|
+
Provides-Extra: ci
|
48
|
+
Requires-Dist: anybadge>=1.10.0; extra == "ci"
|
49
|
+
Provides-Extra: all
|
50
|
+
Requires-Dist: black>=22.6.0; extra == "all"
|
51
|
+
Requires-Dist: pylint>=2.14.0; extra == "all"
|
52
|
+
Requires-Dist: mypy>=1.0.0; extra == "all"
|
53
|
+
Requires-Dist: isort>=5.10.0; extra == "all"
|
54
|
+
Requires-Dist: build>=0.8.0; extra == "all"
|
55
|
+
Requires-Dist: twine>=4.0.0; extra == "all"
|
56
|
+
Requires-Dist: flake8>=5.0.0; extra == "all"
|
57
|
+
Requires-Dist: pre-commit>=3.5.0; extra == "all"
|
58
|
+
Requires-Dist: PyYAML>=6.0.2; extra == "all"
|
59
|
+
Requires-Dist: pytest>=8.3.5; extra == "all"
|
60
|
+
Requires-Dist: pytest-cov>=3.0.0; extra == "all"
|
61
|
+
Requires-Dist: pytest-html>=3.2.0; extra == "all"
|
62
|
+
Requires-Dist: pytest-timeout>=2.1.0; extra == "all"
|
63
|
+
Requires-Dist: coverage>=7.6.1; extra == "all"
|
64
|
+
Requires-Dist: nltk>=3.9.1; extra == "all"
|
65
|
+
Requires-Dist: sphinx>=4.5.0; extra == "all"
|
66
|
+
Requires-Dist: sphinx-rtd-theme>=1.0.0; extra == "all"
|
67
|
+
Requires-Dist: sphinx-autodoc-typehints>=1.18.0; extra == "all"
|
68
|
+
Requires-Dist: sphinx-design>=0.5.0; extra == "all"
|
69
|
+
Requires-Dist: anybadge>=1.10.0; extra == "all"
|
70
|
+
Dynamic: license-file
|
71
|
+
|
72
|
+
# prismalog
|
73
|
+
|
74
|
+
A robust, multi-process safe logging system for Python applications that integrates perfectly with the psy-supabase package.
|
75
|
+
|
76
|
+
[](https://pypi.org/project/prismalog/)
|
77
|
+
[](https://pypi.org/project/prismalog/)
|
78
|
+
[](https://vertok.github.io/prismalog/htmlcov/index.html)
|
79
|
+
[](https://vertok.github.io/prismalog/reports/html/3.8/reports/pylint/pylint-py3.8.html)
|
80
|
+
[](https://vertok.github.io/prismalog/htmlcov/index.html)
|
81
|
+
[](https://vertok.github.io/prismalog/reports/html/3.10/reports/pylint/pylint-py3.10.html)
|
82
|
+
[](https://vertok.github.io/prismalog/htmlcov/index.html)
|
83
|
+
[](https://vertok.github.io/prismalog/reports/html/3.11/reports/pylint/pylint-py3.11.html)
|
84
|
+
|
85
|
+
## Features
|
86
|
+
|
87
|
+
- ⚛️ Zero-dependency core
|
88
|
+
- 🚀 High performance
|
89
|
+
- 🎨 Colored console output
|
90
|
+
- 📁 Automatic log file rotation
|
91
|
+
- 🔄 Multi-process safe logging
|
92
|
+
- 🧵 Multithreading-safe logging
|
93
|
+
- ⚙️ YAML-based configuration
|
94
|
+
- 🔇 Control for verbose third-party libraries
|
95
|
+
- 🧪 Testing support
|
96
|
+
|
97
|
+
## Performance Characteristics
|
98
|
+
|
99
|
+
prismalog was designed for high-performance applications. My testing shows:
|
100
|
+
|
101
|
+
- **Overhead per log call**: ~0.15ms (typical)
|
102
|
+
- **Memory impact**: Minimal (~0.3MB for 10,000 messages)
|
103
|
+
- **Throughput**: Capable of handling 20,000+ messages per second
|
104
|
+
- **Multi-process/Multi-threading safety**: No measurable performance penalty compared to single-process
|
105
|
+
|
106
|
+
## Performance Notes
|
107
|
+
|
108
|
+
While prismalog achieves excellent performance characteristics, it's important to note that the primary bottleneck is filesystem I/O when writing to log files. This limitation is inherent to disk-based logging systems:
|
109
|
+
|
110
|
+
- File locking mechanisms required for multi-process safety introduce some overhead
|
111
|
+
- Synchronous writes to ensure log integrity can impact throughput during high-volume logging events
|
112
|
+
- Storage device speed directly impacts maximum sustainable throughput
|
113
|
+
|
114
|
+
For applications with extreme logging requirements, consider:
|
115
|
+
- Using an asynchronous logging configuration
|
116
|
+
- Implementing log batching for high-volume events
|
117
|
+
- Configuring separate log files for different components to distribute I/O load
|
118
|
+
|
119
|
+
The current performance metrics were achieved with standard SSD hardware. With specialized I/O optimization or enterprise-grade storage systems, significantly higher throughput is achievable.
|
120
|
+
|
121
|
+
## Quick Start
|
122
|
+
|
123
|
+
```python
|
124
|
+
from prismalog import get_logger, LoggingConfig
|
125
|
+
|
126
|
+
# Initialize with configuration file
|
127
|
+
LoggingConfig.initialize(config_file="config.yaml")
|
128
|
+
|
129
|
+
# Get a logger
|
130
|
+
logger = get_logger("my_module")
|
131
|
+
logger.info("Application started")
|
132
|
+
```
|
133
|
+
|
134
|
+
## Command-Line Integration
|
135
|
+
|
136
|
+
Any application using prismalog automatically supports these command-line arguments:
|
137
|
+
|
138
|
+
```bash
|
139
|
+
--log-config PATH # Path to logging configuration file
|
140
|
+
--log-level {DEBUG,INFO,WARNING,ERROR,CRITICAL}
|
141
|
+
# Set the default logging level
|
142
|
+
--log-dir PATH # Directory where log files will be stored
|
143
|
+
```
|
144
|
+
|
145
|
+
## Usage
|
146
|
+
|
147
|
+
```python
|
148
|
+
from prismalog.log import get_logger, LoggingConfig
|
149
|
+
|
150
|
+
# Initialize logging (with command-line support)
|
151
|
+
LoggingConfig.initialize(use_cli_args=True)
|
152
|
+
|
153
|
+
# Get logger and use it
|
154
|
+
logger = get_logger("my_app")
|
155
|
+
logger.info("Application started")
|
156
|
+
```
|
157
|
+
|
158
|
+
## Dependencies
|
159
|
+
|
160
|
+
prismalog is designed to work with **zero external dependencies** for core functionality. It relies solely on the Python standard library, making it lightweight and easy to integrate into any project.
|
161
|
+
|
162
|
+
### Optional Dependencies
|
163
|
+
|
164
|
+
- **YAML Configuration**: If YAML config files needed, install with `pip install prismalog[yaml]`
|
165
|
+
- **Development**: For running tests and examples, install with `pip install prismalog[dev]`
|
166
|
+
|
167
|
+
### Installation Options
|
168
|
+
|
169
|
+
```bash
|
170
|
+
# Prepare before installation (recommended)
|
171
|
+
python -m venv .venv
|
172
|
+
source source .venv/bin/activate
|
173
|
+
|
174
|
+
# Basic installation - no external dependencies
|
175
|
+
pip install -e .
|
176
|
+
|
177
|
+
# With documentation support
|
178
|
+
pip install prismalog[doc]
|
179
|
+
|
180
|
+
# For development and testing
|
181
|
+
pip install prismalog[dev]
|
182
|
+
|
183
|
+
# With all optional features
|
184
|
+
pip install prismalog[all]
|
@@ -0,0 +1,113 @@
|
|
1
|
+
# prismalog
|
2
|
+
|
3
|
+
A robust, multi-process safe logging system for Python applications that integrates perfectly with the psy-supabase package.
|
4
|
+
|
5
|
+
[](https://pypi.org/project/prismalog/)
|
6
|
+
[](https://pypi.org/project/prismalog/)
|
7
|
+
[](https://vertok.github.io/prismalog/htmlcov/index.html)
|
8
|
+
[](https://vertok.github.io/prismalog/reports/html/3.8/reports/pylint/pylint-py3.8.html)
|
9
|
+
[](https://vertok.github.io/prismalog/htmlcov/index.html)
|
10
|
+
[](https://vertok.github.io/prismalog/reports/html/3.10/reports/pylint/pylint-py3.10.html)
|
11
|
+
[](https://vertok.github.io/prismalog/htmlcov/index.html)
|
12
|
+
[](https://vertok.github.io/prismalog/reports/html/3.11/reports/pylint/pylint-py3.11.html)
|
13
|
+
|
14
|
+
## Features
|
15
|
+
|
16
|
+
- ⚛️ Zero-dependency core
|
17
|
+
- 🚀 High performance
|
18
|
+
- 🎨 Colored console output
|
19
|
+
- 📁 Automatic log file rotation
|
20
|
+
- 🔄 Multi-process safe logging
|
21
|
+
- 🧵 Multithreading-safe logging
|
22
|
+
- ⚙️ YAML-based configuration
|
23
|
+
- 🔇 Control for verbose third-party libraries
|
24
|
+
- 🧪 Testing support
|
25
|
+
|
26
|
+
## Performance Characteristics
|
27
|
+
|
28
|
+
prismalog was designed for high-performance applications. My testing shows:
|
29
|
+
|
30
|
+
- **Overhead per log call**: ~0.15ms (typical)
|
31
|
+
- **Memory impact**: Minimal (~0.3MB for 10,000 messages)
|
32
|
+
- **Throughput**: Capable of handling 20,000+ messages per second
|
33
|
+
- **Multi-process/Multi-threading safety**: No measurable performance penalty compared to single-process
|
34
|
+
|
35
|
+
## Performance Notes
|
36
|
+
|
37
|
+
While prismalog achieves excellent performance characteristics, it's important to note that the primary bottleneck is filesystem I/O when writing to log files. This limitation is inherent to disk-based logging systems:
|
38
|
+
|
39
|
+
- File locking mechanisms required for multi-process safety introduce some overhead
|
40
|
+
- Synchronous writes to ensure log integrity can impact throughput during high-volume logging events
|
41
|
+
- Storage device speed directly impacts maximum sustainable throughput
|
42
|
+
|
43
|
+
For applications with extreme logging requirements, consider:
|
44
|
+
- Using an asynchronous logging configuration
|
45
|
+
- Implementing log batching for high-volume events
|
46
|
+
- Configuring separate log files for different components to distribute I/O load
|
47
|
+
|
48
|
+
The current performance metrics were achieved with standard SSD hardware. With specialized I/O optimization or enterprise-grade storage systems, significantly higher throughput is achievable.
|
49
|
+
|
50
|
+
## Quick Start
|
51
|
+
|
52
|
+
```python
|
53
|
+
from prismalog import get_logger, LoggingConfig
|
54
|
+
|
55
|
+
# Initialize with configuration file
|
56
|
+
LoggingConfig.initialize(config_file="config.yaml")
|
57
|
+
|
58
|
+
# Get a logger
|
59
|
+
logger = get_logger("my_module")
|
60
|
+
logger.info("Application started")
|
61
|
+
```
|
62
|
+
|
63
|
+
## Command-Line Integration
|
64
|
+
|
65
|
+
Any application using prismalog automatically supports these command-line arguments:
|
66
|
+
|
67
|
+
```bash
|
68
|
+
--log-config PATH # Path to logging configuration file
|
69
|
+
--log-level {DEBUG,INFO,WARNING,ERROR,CRITICAL}
|
70
|
+
# Set the default logging level
|
71
|
+
--log-dir PATH # Directory where log files will be stored
|
72
|
+
```
|
73
|
+
|
74
|
+
## Usage
|
75
|
+
|
76
|
+
```python
|
77
|
+
from prismalog.log import get_logger, LoggingConfig
|
78
|
+
|
79
|
+
# Initialize logging (with command-line support)
|
80
|
+
LoggingConfig.initialize(use_cli_args=True)
|
81
|
+
|
82
|
+
# Get logger and use it
|
83
|
+
logger = get_logger("my_app")
|
84
|
+
logger.info("Application started")
|
85
|
+
```
|
86
|
+
|
87
|
+
## Dependencies
|
88
|
+
|
89
|
+
prismalog is designed to work with **zero external dependencies** for core functionality. It relies solely on the Python standard library, making it lightweight and easy to integrate into any project.
|
90
|
+
|
91
|
+
### Optional Dependencies
|
92
|
+
|
93
|
+
- **YAML Configuration**: If YAML config files needed, install with `pip install prismalog[yaml]`
|
94
|
+
- **Development**: For running tests and examples, install with `pip install prismalog[dev]`
|
95
|
+
|
96
|
+
### Installation Options
|
97
|
+
|
98
|
+
```bash
|
99
|
+
# Prepare before installation (recommended)
|
100
|
+
python -m venv .venv
|
101
|
+
source source .venv/bin/activate
|
102
|
+
|
103
|
+
# Basic installation - no external dependencies
|
104
|
+
pip install -e .
|
105
|
+
|
106
|
+
# With documentation support
|
107
|
+
pip install prismalog[doc]
|
108
|
+
|
109
|
+
# For development and testing
|
110
|
+
pip install prismalog[dev]
|
111
|
+
|
112
|
+
# With all optional features
|
113
|
+
pip install prismalog[all]
|
@@ -0,0 +1,35 @@
|
|
1
|
+
"""
|
2
|
+
For more information, see the documentation at:
|
3
|
+
https://github.com/vertok/prismalog
|
4
|
+
"""
|
5
|
+
|
6
|
+
from typing import Optional
|
7
|
+
|
8
|
+
from .config import LoggingConfig
|
9
|
+
from .log import ColoredFormatter, ColoredLogger, CriticalExitHandler, MultiProcessingLog, get_logger
|
10
|
+
|
11
|
+
|
12
|
+
def setup_logging(config_file: Optional[str] = None, use_cli_args: bool = True) -> dict:
|
13
|
+
"""
|
14
|
+
Initialize logging with potential command-line arguments.
|
15
|
+
|
16
|
+
Simple helper function that initializes LoggingConfig with both
|
17
|
+
a config file and command-line arguments. This is the most common
|
18
|
+
use case for applications.
|
19
|
+
|
20
|
+
Args:
|
21
|
+
config_file: Optional path to config file
|
22
|
+
use_cli_args: Whether to parse command-line arguments (default: True)
|
23
|
+
"""
|
24
|
+
return LoggingConfig.initialize(config_file=config_file, use_cli_args=use_cli_args)
|
25
|
+
|
26
|
+
|
27
|
+
__all__ = [
|
28
|
+
"get_logger",
|
29
|
+
"ColoredLogger",
|
30
|
+
"ColoredFormatter",
|
31
|
+
"MultiProcessingLog",
|
32
|
+
"CriticalExitHandler",
|
33
|
+
"LoggingConfig",
|
34
|
+
"setup_logging",
|
35
|
+
]
|
@@ -0,0 +1,216 @@
|
|
1
|
+
"""
|
2
|
+
Standard command-line argument parser for prismalog logging system.
|
3
|
+
|
4
|
+
This module provides a standardized way to add prismalog-related
|
5
|
+
command line arguments to any application using the package. It enables
|
6
|
+
consistent handling of logging configuration options across different
|
7
|
+
applications and scripts.
|
8
|
+
|
9
|
+
Features:
|
10
|
+
---------
|
11
|
+
* Standardized logging arguments for all prismalog applications
|
12
|
+
* Support for configuration via command line or config file
|
13
|
+
* Automatic mapping between CLI args and LoggingConfig settings
|
14
|
+
* Integration with Python's argparse module
|
15
|
+
* Consistent argument naming conventions
|
16
|
+
|
17
|
+
Available Arguments:
|
18
|
+
--------------------
|
19
|
+
--log-config Path to a YAML configuration file
|
20
|
+
--log-level Set the default logging level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
|
21
|
+
--log-dir Directory where log files will be stored
|
22
|
+
--log-format Format string for log messages
|
23
|
+
--no-color Disable colored console output
|
24
|
+
--disable-rotation Disable log file rotation
|
25
|
+
--exit-on-critical Exit program on critical errors
|
26
|
+
--rotation-size Log file rotation size in MB
|
27
|
+
--backup-count Number of backup log files to keep
|
28
|
+
|
29
|
+
Usage Examples:
|
30
|
+
---------------
|
31
|
+
1. Basic usage::
|
32
|
+
|
33
|
+
from prismalog.argparser import get_argument_parser, extract_logging_args
|
34
|
+
from prismalog.log import LoggingConfig
|
35
|
+
|
36
|
+
# Create parser with standard logging arguments
|
37
|
+
parser = get_argument_parser(description="My Application")
|
38
|
+
|
39
|
+
# Add your own application-specific arguments
|
40
|
+
parser.add_argument("--my-option", help="Application-specific option")
|
41
|
+
|
42
|
+
# Parse arguments
|
43
|
+
args = parser.parse_args()
|
44
|
+
|
45
|
+
# Extract and apply logging configuration
|
46
|
+
logging_args = extract_logging_args(args)
|
47
|
+
LoggingConfig.from_dict(logging_args)
|
48
|
+
|
49
|
+
2. Adding to an existing parser::
|
50
|
+
|
51
|
+
import argparse
|
52
|
+
from prismalog.argparser import add_logging_arguments, extract_logging_args
|
53
|
+
|
54
|
+
# Create your own parser
|
55
|
+
parser = argparse.ArgumentParser(description="My Application")
|
56
|
+
parser.add_argument("--my-option", help="Application-specific option")
|
57
|
+
|
58
|
+
# Add standard logging arguments
|
59
|
+
add_logging_arguments(parser)
|
60
|
+
|
61
|
+
# Parse and extract
|
62
|
+
args = parser.parse_args()
|
63
|
+
logging_args = extract_logging_args(args)
|
64
|
+
"""
|
65
|
+
|
66
|
+
import argparse
|
67
|
+
from typing import Any, Dict, Optional
|
68
|
+
|
69
|
+
|
70
|
+
class LoggingArgumentParser:
|
71
|
+
"""Helper class for adding standard logging arguments to argparse."""
|
72
|
+
|
73
|
+
@staticmethod
|
74
|
+
def add_arguments(parser: Optional[argparse.ArgumentParser] = None) -> argparse.ArgumentParser:
|
75
|
+
"""
|
76
|
+
Add standard prismalog arguments to an existing parser.
|
77
|
+
|
78
|
+
Args:
|
79
|
+
parser: An existing ArgumentParser instance. If None, a new one is created.
|
80
|
+
|
81
|
+
Returns:
|
82
|
+
The ArgumentParser with prismalog arguments added
|
83
|
+
"""
|
84
|
+
if parser is None:
|
85
|
+
parser = argparse.ArgumentParser()
|
86
|
+
|
87
|
+
# Config file option
|
88
|
+
parser.add_argument("--log-config", help="Path to a YAML configuration file")
|
89
|
+
|
90
|
+
# Standard logging arguments with case-insensitive level
|
91
|
+
parser.add_argument(
|
92
|
+
"--log-level",
|
93
|
+
choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"],
|
94
|
+
type=str.upper, # Simply convert to uppercase
|
95
|
+
help="Set the default logging level (case-insensitive)",
|
96
|
+
)
|
97
|
+
|
98
|
+
parser.add_argument("--log-dir", help="Directory where log files will be stored")
|
99
|
+
|
100
|
+
parser.add_argument("--log-format", help="Format string for log messages")
|
101
|
+
|
102
|
+
# Boolean flags
|
103
|
+
parser.add_argument(
|
104
|
+
"--no-color",
|
105
|
+
"--no-colors",
|
106
|
+
dest="colored_console",
|
107
|
+
action="store_false",
|
108
|
+
help="Disable colored console output",
|
109
|
+
)
|
110
|
+
|
111
|
+
parser.add_argument(
|
112
|
+
"--disable-rotation", dest="disable_rotation", action="store_true", help="Disable log file rotation"
|
113
|
+
)
|
114
|
+
|
115
|
+
parser.add_argument(
|
116
|
+
"--exit-on-critical",
|
117
|
+
dest="exit_on_critical",
|
118
|
+
action="store_true",
|
119
|
+
help="Exit the program on critical errors",
|
120
|
+
)
|
121
|
+
|
122
|
+
# Numeric options
|
123
|
+
parser.add_argument("--rotation-size", type=int, dest="rotation_size_mb", help="Log file rotation size in MB")
|
124
|
+
|
125
|
+
parser.add_argument("--backup-count", type=int, help="Number of backup log files to keep")
|
126
|
+
|
127
|
+
return parser
|
128
|
+
|
129
|
+
@staticmethod
|
130
|
+
def create_parser(description: Optional[str] = None) -> argparse.ArgumentParser:
|
131
|
+
"""
|
132
|
+
Create a new ArgumentParser with prismalog arguments.
|
133
|
+
|
134
|
+
Args:
|
135
|
+
description: Description for the ArgumentParser
|
136
|
+
|
137
|
+
Returns:
|
138
|
+
A new ArgumentParser with prismalog arguments
|
139
|
+
"""
|
140
|
+
parser = argparse.ArgumentParser(description=description)
|
141
|
+
return LoggingArgumentParser.add_arguments(parser)
|
142
|
+
|
143
|
+
@staticmethod
|
144
|
+
def extract_logging_args(args: argparse.Namespace) -> Dict[str, Any]:
|
145
|
+
"""
|
146
|
+
Extract logging-related arguments from parsed args.
|
147
|
+
|
148
|
+
Args:
|
149
|
+
args: The parsed args from ArgumentParser.parse_args()
|
150
|
+
|
151
|
+
Returns:
|
152
|
+
Dictionary with only the logging-related arguments
|
153
|
+
"""
|
154
|
+
# Define mappings from CLI arg names to config keys
|
155
|
+
key_mappings = {
|
156
|
+
"log_level": "default_level",
|
157
|
+
"log_config": "config_file",
|
158
|
+
"log_dir": "log_dir",
|
159
|
+
"log_format": "log_format",
|
160
|
+
"colored_console": "colored_console",
|
161
|
+
"disable_rotation": "disable_rotation",
|
162
|
+
"exit_on_critical": "exit_on_critical",
|
163
|
+
"rotation_size_mb": "rotation_size_mb",
|
164
|
+
"backup_count": "backup_count",
|
165
|
+
}
|
166
|
+
|
167
|
+
# Convert args to dictionary
|
168
|
+
args_dict = vars(args)
|
169
|
+
|
170
|
+
# Extract and map arguments
|
171
|
+
result = {}
|
172
|
+
for arg_name, value in args_dict.items():
|
173
|
+
if value is not None and arg_name in key_mappings:
|
174
|
+
config_key = key_mappings[arg_name]
|
175
|
+
result[config_key] = value
|
176
|
+
|
177
|
+
return result
|
178
|
+
|
179
|
+
|
180
|
+
def get_argument_parser(description: Optional[str] = None) -> argparse.ArgumentParser:
|
181
|
+
"""
|
182
|
+
Create a new ArgumentParser with prismalog arguments.
|
183
|
+
|
184
|
+
Args:
|
185
|
+
description: Description for the ArgumentParser
|
186
|
+
|
187
|
+
Returns:
|
188
|
+
A new ArgumentParser with prismalog arguments
|
189
|
+
"""
|
190
|
+
return LoggingArgumentParser.create_parser(description)
|
191
|
+
|
192
|
+
|
193
|
+
def add_logging_arguments(parser: argparse.ArgumentParser) -> argparse.ArgumentParser:
|
194
|
+
"""
|
195
|
+
Add standard prismalog arguments to an existing parser.
|
196
|
+
|
197
|
+
Args:
|
198
|
+
parser: An existing ArgumentParser instance
|
199
|
+
|
200
|
+
Returns:
|
201
|
+
The ArgumentParser with prismalog arguments added
|
202
|
+
"""
|
203
|
+
return LoggingArgumentParser.add_arguments(parser)
|
204
|
+
|
205
|
+
|
206
|
+
def extract_logging_args(args: argparse.Namespace) -> Dict[str, Any]:
|
207
|
+
"""
|
208
|
+
Extract logging-related arguments from parsed args.
|
209
|
+
|
210
|
+
Args:
|
211
|
+
args: The parsed args from ArgumentParser.parse_args()
|
212
|
+
|
213
|
+
Returns:
|
214
|
+
Dictionary with only the logging-related arguments
|
215
|
+
"""
|
216
|
+
return LoggingArgumentParser.extract_logging_args(args)
|