logging-ext2 0.0.1__py3-none-any.whl → 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.
logging_ext2/__about__.py CHANGED
@@ -1,4 +1,4 @@
1
1
  # SPDX-FileCopyrightText: 2025-present Xiang Wang <ramwin@qq.com>
2
2
  #
3
3
  # SPDX-License-Identifier: MIT
4
- __version__ = "0.0.1"
4
+ __version__ = "0.1.0"
@@ -0,0 +1,71 @@
1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+ # Xiang Wang <ramwin@qq.com>
4
+
5
+
6
+ import datetime
7
+ import subprocess
8
+
9
+ from logging import Handler, NOTSET
10
+ from pathlib import Path
11
+ from .types import PathWithDatetime
12
+
13
+
14
+ class TimedRotatingFileHandler(Handler):
15
+ """
16
+ compare to logging.TimedRotatingFileHandler, this handler will write
17
+ log directly to file like 'info.log.2025-04-19'.
18
+ """
19
+
20
+ def __init__(self, level=NOTSET, filename="log.log",
21
+ datetime_formatter="%Y-%m-%d", max_keep=10, flat_keep=2):
22
+ """
23
+ params:
24
+ max_keep: how many files will the rotation keep
25
+ flat_keep: how many files will stay in text mod, the other (max_keep-flat_keep) will be compressed using gzip
26
+ """
27
+ self.filename = Path(filename).name
28
+ self.base_dir = Path(filename).parent
29
+ self.datetime_formatter = datetime_formatter
30
+ self.current_time_str = self.get_time_str()
31
+ self.stream = self.init_stream()
32
+ self.max_keep = max_keep
33
+ self.flat_keep = flat_keep
34
+ super().__init__(level)
35
+
36
+ def init_stream(self):
37
+ filepath = self.base_dir.joinpath(
38
+ f"{self.filename}.{self.get_time_str()}")
39
+ return open(filepath, "a")
40
+
41
+ def get_time_str(self) -> str:
42
+ return datetime.datetime.now().strftime(self.datetime_formatter)
43
+
44
+ def emit(self, record):
45
+ if self.should_rollover():
46
+ self.do_rollover()
47
+ self.stream.write(self.format(record) + "\n")
48
+ self.stream.flush()
49
+
50
+ def should_rollover(self) -> bool:
51
+ new_time_str = self.get_time_str()
52
+ return new_time_str != self.current_time_str
53
+
54
+ def do_rollover(self):
55
+ self.stream.close()
56
+ paths: List[Path] = [
57
+ i
58
+ for i in self.base_dir.iterdir()
59
+ if i.is_file() and i.name.startswith(self.filename)
60
+ ]
61
+ paths.sort(key=lambda x: x.name, reverse=True)
62
+ to_delete: Path
63
+ for to_delete in paths[self.max_keep:]:
64
+ to_delete.unlink(missing_ok=True)
65
+ for to_gzip_path in paths[self.flat_keep:self.max_keep]:
66
+ # here use the system gzip command to ignore exceptions like
67
+ # permission denied or file not found
68
+ if to_gzip_path.suffix == ".gz":
69
+ continue
70
+ subprocess.run(["gzip", to_gzip_path])
71
+ self.stream = self.init_stream()
logging_ext2/types.py ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+ # Xiang Wang <ramwin@qq.com>
4
+
5
+
6
+ import datetime
7
+ from dataclasses import dataclass
8
+ from pathlib import Path
9
+
10
+
11
+ @dataclass
12
+ class PathWithDatetime:
13
+ path: Path
14
+ datetime: datetime.datetime
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: logging-ext2
3
- Version: 0.0.1
3
+ Version: 0.1.0
4
4
  Summary: extend python logging library
5
5
  Project-URL: Documentation, https://github.com/Xiang Wang/logging-ext2#readme
6
6
  Project-URL: Issues, https://github.com/Xiang Wang/logging-ext2/issues
@@ -0,0 +1,8 @@
1
+ logging_ext2/__about__.py,sha256=1RdZckQx6I2F1xvr1wmGF9zZKGRTU0NyAEl0IEUEwOs,121
2
+ logging_ext2/__init__.py,sha256=BA6rgdmh3z2v01cbHU1FRMsBIRV5vQjh2w9FzG2F2GY,99
3
+ logging_ext2/handlers.py,sha256=lG5CnPSO0fYouzmwfReASCCRzZzLv7gwKY8WCJHImy0,2426
4
+ logging_ext2/types.py,sha256=cLjODjTgxrZEXUZzOkrFm3s6bUG3Itc_fS7KJ-MSWC8,237
5
+ logging_ext2-0.1.0.dist-info/METADATA,sha256=4pcS8ViOlx2VcFk6lZ2MxV1GbjzHOaA4sfSLjHh8RTE,1444
6
+ logging_ext2-0.1.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
7
+ logging_ext2-0.1.0.dist-info/licenses/LICENSE,sha256=RckqF9bdq7oO9RrL4iZYuXNhkIcXexbXNF2ToiaJymU,1067
8
+ logging_ext2-0.1.0.dist-info/RECORD,,
@@ -1,6 +0,0 @@
1
- logging_ext2/__about__.py,sha256=ukYgyXwD_W0QxMVx6Epm_vepx0sXhPK3A5dPE1PkIGw,121
2
- logging_ext2/__init__.py,sha256=BA6rgdmh3z2v01cbHU1FRMsBIRV5vQjh2w9FzG2F2GY,99
3
- logging_ext2-0.0.1.dist-info/METADATA,sha256=9wlVkWAIItOLdspWsvbTBbzf_udLkQNjIvvGkSTVyIQ,1444
4
- logging_ext2-0.0.1.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
5
- logging_ext2-0.0.1.dist-info/licenses/LICENSE,sha256=RckqF9bdq7oO9RrL4iZYuXNhkIcXexbXNF2ToiaJymU,1067
6
- logging_ext2-0.0.1.dist-info/RECORD,,