prismalog 0.1.0__py3-none-any.whl
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/__init__.py +35 -0
- prismalog/argparser.py +216 -0
- prismalog/config.py +783 -0
- prismalog/log.py +927 -0
- prismalog/py.typed +0 -0
- prismalog-0.1.0.dist-info/METADATA +184 -0
- prismalog-0.1.0.dist-info/RECORD +10 -0
- prismalog-0.1.0.dist-info/WHEEL +5 -0
- prismalog-0.1.0.dist-info/licenses/LICENSE +21 -0
- prismalog-0.1.0.dist-info/top_level.txt +1 -0
prismalog/__init__.py
ADDED
@@ -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
|
+
]
|
prismalog/argparser.py
ADDED
@@ -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)
|