lcdp-logging-utils 1.5.8__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.
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: lcdp-logging-utils
|
|
3
|
+
Version: 1.5.8
|
|
4
|
+
Summary: Postgres Utils to configure logging...
|
|
5
|
+
Author: Le Comptoir Des Pharmacies
|
|
6
|
+
Author-email: webmaster@lecomptoirdespharmacies.fr
|
|
7
|
+
Requires-Python: >=3.8
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
16
|
+
Requires-Dist: click (==8.1.7)
|
|
File without changes
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
import click
|
|
3
|
+
import sys
|
|
4
|
+
from copy import copy
|
|
5
|
+
from typing_extensions import Literal
|
|
6
|
+
TRACE_LOG_LEVEL = 5
|
|
7
|
+
|
|
8
|
+
class DdStub:
|
|
9
|
+
trace_id = 0
|
|
10
|
+
span_id = 0
|
|
11
|
+
|
|
12
|
+
class ColourizedFormatter(logging.Formatter):
|
|
13
|
+
"""
|
|
14
|
+
A custom log formatter class that:
|
|
15
|
+
|
|
16
|
+
* Outputs the LOG_LEVEL with an appropriate color.
|
|
17
|
+
* If a log call includes an `extra={"color_message": ...}` it will be used
|
|
18
|
+
for formatting the output, instead of the plain text message.
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
level_name_colors = {
|
|
22
|
+
TRACE_LOG_LEVEL: lambda level_name: click.style(str(level_name), fg="blue"),
|
|
23
|
+
logging.DEBUG: lambda level_name: click.style(str(level_name), fg="cyan"),
|
|
24
|
+
logging.INFO: lambda level_name: click.style(str(level_name), fg="green"),
|
|
25
|
+
logging.WARNING: lambda level_name: click.style(str(level_name), fg="yellow"),
|
|
26
|
+
logging.ERROR: lambda level_name: click.style(str(level_name), fg="red"),
|
|
27
|
+
logging.CRITICAL: lambda level_name: click.style(str(level_name), fg="bright_red"),
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
def __init__(
|
|
31
|
+
self,
|
|
32
|
+
fmt: str | None = None,
|
|
33
|
+
datefmt: str | None = None,
|
|
34
|
+
style: Literal["%", "{", "$"] = "%",
|
|
35
|
+
use_colors: bool | None = None,
|
|
36
|
+
*args,
|
|
37
|
+
**kwargs
|
|
38
|
+
):
|
|
39
|
+
if use_colors in (True, False):
|
|
40
|
+
self.use_colors = use_colors
|
|
41
|
+
else:
|
|
42
|
+
self.use_colors = sys.stdout.isatty()
|
|
43
|
+
super().__init__(fmt=fmt, datefmt=datefmt, style=style, *args, **kwargs)
|
|
44
|
+
|
|
45
|
+
def color_level_name(self, level_name: str, level_no: int) -> str:
|
|
46
|
+
def default(level_name: str) -> str:
|
|
47
|
+
return str(level_name) # pragma: no cover
|
|
48
|
+
|
|
49
|
+
func = self.level_name_colors.get(level_no, default)
|
|
50
|
+
return func(level_name)
|
|
51
|
+
|
|
52
|
+
def should_use_colors(self) -> bool:
|
|
53
|
+
return True # pragma: no cover
|
|
54
|
+
|
|
55
|
+
def formatMessage(self, record: logging.LogRecord) -> str:
|
|
56
|
+
recordcopy = copy(record)
|
|
57
|
+
levelname = recordcopy.levelname
|
|
58
|
+
if self.use_colors:
|
|
59
|
+
levelname = self.color_level_name(levelname, recordcopy.levelno)
|
|
60
|
+
if "color_message" in recordcopy.__dict__:
|
|
61
|
+
recordcopy.msg = recordcopy.__dict__["color_message"]
|
|
62
|
+
recordcopy.__dict__["message"] = recordcopy.getMessage()
|
|
63
|
+
recordcopy.__dict__["levelprefix"] = levelname
|
|
64
|
+
return super().formatMessage(recordcopy)
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
# See : https://github.com/encode/starlette/issues/864#issuecomment-653076434
|
|
68
|
+
def create_periodic_endpoint_filter(custom_endpoints=[]):
|
|
69
|
+
class PeriodicEndpointFilter(logging.Filter):
|
|
70
|
+
def filter(self, record: logging.LogRecord) -> bool:
|
|
71
|
+
matchers = ["/actuator/health"]
|
|
72
|
+
matchers.extend(custom_endpoints)
|
|
73
|
+
return not any(xs in record.getMessage() for xs in matchers)
|
|
74
|
+
|
|
75
|
+
return PeriodicEndpointFilter
|
|
76
|
+
|
|
77
|
+
def console_formatter():
|
|
78
|
+
return ColourizedFormatter(
|
|
79
|
+
"{threadName}s - {dd.trace_id} {dd.span_id} - {levelprefix} - {name} - {message}",
|
|
80
|
+
style="{",
|
|
81
|
+
use_colors=True,
|
|
82
|
+
defaults={"dd": DdStub()}
|
|
83
|
+
)
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
[tool.poetry]
|
|
2
|
+
name = "lcdp-logging-utils"
|
|
3
|
+
# https://github.com/python-poetry/poetry/issues/1208
|
|
4
|
+
version = "1.5.8"
|
|
5
|
+
description = "Postgres Utils to configure logging..."
|
|
6
|
+
authors = ["Le Comptoir Des Pharmacies <webmaster@lecomptoirdespharmacies.fr>"]
|
|
7
|
+
|
|
8
|
+
[tool.poetry-dynamic-versioning]
|
|
9
|
+
enable = false
|
|
10
|
+
vcs = "git"
|
|
11
|
+
|
|
12
|
+
[tool.poetry.requires-plugins]
|
|
13
|
+
poetry-dynamic-versioning = { version = ">=1.0.0,<2.0.0", extras = ["plugin"] }
|
|
14
|
+
|
|
15
|
+
[tool.poetry.dependencies]
|
|
16
|
+
python = ">=3.8"
|
|
17
|
+
click = "8.1.7"
|
|
18
|
+
|
|
19
|
+
[tool.poetry.dev-dependencies]
|
|
20
|
+
|
|
21
|
+
[build-system]
|
|
22
|
+
requires = ["poetry-core>=1.0.0", "poetry-dynamic-versioning>=1.0.0,<2.0.0"]
|
|
23
|
+
build-backend = "poetry_dynamic_versioning.backend"
|