logging-ext2 0.2.4__tar.gz → 0.3.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.
- {logging_ext2-0.2.4 → logging_ext2-0.3.0}/PKG-INFO +1 -1
- {logging_ext2-0.2.4 → logging_ext2-0.3.0}/src/logging_ext2/__about__.py +1 -1
- {logging_ext2-0.2.4 → logging_ext2-0.3.0}/src/logging_ext2/handlers.py +15 -4
- {logging_ext2-0.2.4 → logging_ext2-0.3.0}/.gitignore +0 -0
- {logging_ext2-0.2.4 → logging_ext2-0.3.0}/LICENSE +0 -0
- {logging_ext2-0.2.4 → logging_ext2-0.3.0}/README.md +0 -0
- {logging_ext2-0.2.4 → logging_ext2-0.3.0}/pyproject.toml +0 -0
- {logging_ext2-0.2.4 → logging_ext2-0.3.0}/src/logging_ext2/__init__.py +0 -0
- {logging_ext2-0.2.4 → logging_ext2-0.3.0}/src/logging_ext2/types.py +0 -0
- {logging_ext2-0.2.4 → logging_ext2-0.3.0}/tests/__init__.py +0 -0
@@ -6,6 +6,7 @@
|
|
6
6
|
import datetime
|
7
7
|
import subprocess
|
8
8
|
import time
|
9
|
+
from typing import List
|
9
10
|
|
10
11
|
from filelock import FileLock
|
11
12
|
|
@@ -21,18 +22,19 @@ class TimedRotatingFileHandler(Handler):
|
|
21
22
|
"""
|
22
23
|
|
23
24
|
def __init__(self, level=NOTSET, filename="log.log",
|
24
|
-
datetime_formatter="%Y-%m-%d", max_keep=10, flat_keep=2):
|
25
|
+
datetime_formatter="%Y-%m-%d", max_keep=10, flat_keep=2) -> None:
|
25
26
|
"""
|
26
27
|
params:
|
27
28
|
max_keep: how many files will the rotation keep
|
28
29
|
flat_keep: how many files will stay in text mod, the other (max_keep-flat_keep) will be compressed using gzip
|
29
30
|
"""
|
30
|
-
self.filepath: Path = None
|
31
|
+
self.filepath: Path = None # type: ignore[assignment]
|
31
32
|
self.filename = Path(filename).name
|
32
33
|
self.base_dir = Path(filename).parent
|
33
34
|
self.datetime_formatter = datetime_formatter
|
34
35
|
self.current_time_str = self.get_time_str()
|
35
36
|
self.stream = self.init_stream()
|
37
|
+
assert self.filepath
|
36
38
|
self.max_keep = max_keep
|
37
39
|
self.flat_keep = flat_keep
|
38
40
|
super().__init__(level)
|
@@ -55,9 +57,10 @@ class TimedRotatingFileHandler(Handler):
|
|
55
57
|
new_time_str = self.get_time_str()
|
56
58
|
return new_time_str != self.current_time_str
|
57
59
|
|
58
|
-
def do_rollover(self):
|
60
|
+
def do_rollover(self) -> None:
|
59
61
|
if self.filepath.exists():
|
60
62
|
self.stream.close()
|
63
|
+
self.stream = self.init_stream()
|
61
64
|
paths: List[Path] = [
|
62
65
|
i
|
63
66
|
for i in self.base_dir.iterdir()
|
@@ -66,10 +69,19 @@ class TimedRotatingFileHandler(Handler):
|
|
66
69
|
paths.sort(key=lambda x: x.name, reverse=True)
|
67
70
|
to_delete: Path
|
68
71
|
for to_delete in paths[self.max_keep:]:
|
72
|
+
if to_delete == self.filepath:
|
73
|
+
continue
|
69
74
|
to_delete.unlink(missing_ok=True)
|
75
|
+
|
76
|
+
def gzip_file(self, paths) -> None:
|
77
|
+
"""
|
78
|
+
TODO use async function to gzip file
|
79
|
+
"""
|
70
80
|
for to_gzip_path in paths[self.flat_keep:self.max_keep]:
|
71
81
|
# here use the system gzip command to ignore exceptions like
|
72
82
|
# permission denied or file not found
|
83
|
+
if to_gzip_path == self.filepath:
|
84
|
+
continue
|
73
85
|
try:
|
74
86
|
if not to_gzip_path.stat().st_size:
|
75
87
|
to_gzip_path.unlink(missing_ok=True)
|
@@ -90,4 +102,3 @@ class TimedRotatingFileHandler(Handler):
|
|
90
102
|
).with_suffix(".gz"))
|
91
103
|
cmds = ["gzip", to_gzip_path]
|
92
104
|
subprocess.run(cmds)
|
93
|
-
self.stream = self.init_stream()
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|