MapleX 3.1.0.dev4__py3-none-any.whl → 3.1.0.dev5__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.
- maplex/__init__.py +1 -1
- maplex/library/logger/__init__.py +2 -0
- maplex/library/logger/consts.py +4 -1
- maplex/library/logger/file_handler.py +101 -1
- maplex/mapleLogger.py +7 -41
- {maplex-3.1.0.dev4.dist-info → maplex-3.1.0.dev5.dist-info}/METADATA +1 -1
- {maplex-3.1.0.dev4.dist-info → maplex-3.1.0.dev5.dist-info}/RECORD +10 -10
- {maplex-3.1.0.dev4.dist-info → maplex-3.1.0.dev5.dist-info}/WHEEL +0 -0
- {maplex-3.1.0.dev4.dist-info → maplex-3.1.0.dev5.dist-info}/licenses/LICENSE +0 -0
- {maplex-3.1.0.dev4.dist-info → maplex-3.1.0.dev5.dist-info}/top_level.txt +0 -0
maplex/__init__.py
CHANGED
|
@@ -3,11 +3,13 @@ Split mapleLogger.py into multiple files for better organization and maintainabi
|
|
|
3
3
|
"""
|
|
4
4
|
|
|
5
5
|
from .config import LoggerConfig
|
|
6
|
+
from .file_handler import FileHandler
|
|
6
7
|
from .formatter import Formatter
|
|
7
8
|
from .log_levels import LogLevel
|
|
8
9
|
from .utilities import *
|
|
9
10
|
|
|
10
11
|
__all__ = [
|
|
12
|
+
"FileHandler",
|
|
11
13
|
"Formatter",
|
|
12
14
|
"LoggerConfig",
|
|
13
15
|
"LogLevel",
|
maplex/library/logger/consts.py
CHANGED
|
@@ -22,4 +22,7 @@ CALLER_NAME = "CallerName"
|
|
|
22
22
|
PROCESS_ID = "pid"
|
|
23
23
|
|
|
24
24
|
FILE_FORMAT = '({pid}) {timestamp} [{level}]{func} {callerName}{callerFunc}({callerLine})'
|
|
25
|
-
CONSOLE_FORMAT = '[{level}]{func} {callerFunc}{callerLine}'
|
|
25
|
+
CONSOLE_FORMAT = '[{level}]{func} {callerFunc}{callerLine}'
|
|
26
|
+
|
|
27
|
+
FILE_MODE_OVERWRITE = 'overwrite'
|
|
28
|
+
FILE_MODE_DAILY = 'daily'
|
|
@@ -1,3 +1,103 @@
|
|
|
1
|
+
from datetime import datetime
|
|
2
|
+
import os
|
|
3
|
+
import pathlib
|
|
4
|
+
import sys
|
|
5
|
+
|
|
6
|
+
sys.path.append(str(pathlib.Path(__file__).parent.parent.parent))
|
|
7
|
+
|
|
8
|
+
from .consts import *
|
|
9
|
+
from mapleExceptions import MapleLoggerException
|
|
10
|
+
|
|
1
11
|
"""
|
|
2
12
|
File handler for logging.
|
|
3
|
-
"""
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
class FileHandler:
|
|
16
|
+
|
|
17
|
+
"""
|
|
18
|
+
FileHandler handles log file size management for the Logger class. It checks the size of the log file and creates a new one if the size exceeds the specified limit.
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
def __init__(
|
|
22
|
+
self,
|
|
23
|
+
logFilePath: str,
|
|
24
|
+
maxFileSize: int,
|
|
25
|
+
fileMode: str
|
|
26
|
+
) -> None:
|
|
27
|
+
|
|
28
|
+
"""
|
|
29
|
+
Initialize a FileHandler instance.
|
|
30
|
+
|
|
31
|
+
:param logFilePath: The path to the log file.
|
|
32
|
+
:param maxFileSize: The maximum size of the log file in bytes.
|
|
33
|
+
"""
|
|
34
|
+
|
|
35
|
+
self.logFilePath = logFilePath
|
|
36
|
+
self.maxFileSize = maxFileSize
|
|
37
|
+
self.fileMode = fileMode
|
|
38
|
+
|
|
39
|
+
def __file_size_exceeded(self) -> bool:
|
|
40
|
+
|
|
41
|
+
"""
|
|
42
|
+
Check if the log file exists and if its size exceeds the maximum file size.
|
|
43
|
+
:return: True if the file size exceeds the maximum, False otherwise.
|
|
44
|
+
"""
|
|
45
|
+
|
|
46
|
+
if os.path.exists(self.logFilePath):
|
|
47
|
+
|
|
48
|
+
fileSize = os.path.getsize(self.logFilePath)
|
|
49
|
+
return fileSize >= self.maxFileSize
|
|
50
|
+
|
|
51
|
+
return False
|
|
52
|
+
|
|
53
|
+
def __overwrite_log_file(self) -> None:
|
|
54
|
+
|
|
55
|
+
"""
|
|
56
|
+
Overwrite the log file by swapping the current log file with a new one.
|
|
57
|
+
The old log file is renamed with "_old" suffix and a new log file is created with the original name.
|
|
58
|
+
"""
|
|
59
|
+
|
|
60
|
+
oldLogFilePath = self.logFilePath + "_old"
|
|
61
|
+
|
|
62
|
+
if os.path.exists(oldLogFilePath):
|
|
63
|
+
os.remove(oldLogFilePath)
|
|
64
|
+
|
|
65
|
+
os.rename(self.logFilePath, oldLogFilePath)
|
|
66
|
+
|
|
67
|
+
async def check_file_size(self) -> None:
|
|
68
|
+
|
|
69
|
+
"""
|
|
70
|
+
Check the log file size and create a new log file if the size exceeds the maximum file size.
|
|
71
|
+
"""
|
|
72
|
+
|
|
73
|
+
try:
|
|
74
|
+
|
|
75
|
+
if self.maxFileSize < 1 or not self.__file_size_exceeded():
|
|
76
|
+
return
|
|
77
|
+
|
|
78
|
+
if self.fileMode == FILE_MODE_OVERWRITE:
|
|
79
|
+
|
|
80
|
+
self.__overwrite_log_file()
|
|
81
|
+
return
|
|
82
|
+
|
|
83
|
+
if self.fileMode == FILE_MODE_DAILY:
|
|
84
|
+
|
|
85
|
+
dateStr = ""
|
|
86
|
+
|
|
87
|
+
else:
|
|
88
|
+
|
|
89
|
+
dateStr = f"_{datetime.now():%Y%m%d_%H%M%S}"
|
|
90
|
+
|
|
91
|
+
i = 0
|
|
92
|
+
logCopyFilePath = f"{self.logFilePath}{dateStr}{i}.log"
|
|
93
|
+
|
|
94
|
+
while os.path.exists(logCopyFilePath):
|
|
95
|
+
|
|
96
|
+
i += 1
|
|
97
|
+
logCopyFilePath = f"{self.logFilePath}{dateStr}{i}.log"
|
|
98
|
+
|
|
99
|
+
os.rename(self.logFilePath, logCopyFilePath)
|
|
100
|
+
|
|
101
|
+
except Exception as e:
|
|
102
|
+
|
|
103
|
+
raise MapleLoggerException(f"Failed to manage log file size: {e}")
|
maplex/mapleLogger.py
CHANGED
|
@@ -55,6 +55,11 @@ class Logger:
|
|
|
55
55
|
}
|
|
56
56
|
self.config = LoggerConfig(loggerParams)
|
|
57
57
|
self.formatter = Formatter(self.config.serialize())
|
|
58
|
+
self.fileHandler = FileHandler(
|
|
59
|
+
logFilePath=self.config.logfile,
|
|
60
|
+
maxFileSize=self.config.maxLogSize,
|
|
61
|
+
fileMode=self.config.fileMode
|
|
62
|
+
)
|
|
58
63
|
self.DEFAULT_CALLER_DEPTH = 3
|
|
59
64
|
|
|
60
65
|
except Exception as ex:
|
|
@@ -181,51 +186,12 @@ class Logger:
|
|
|
181
186
|
if i == 2:
|
|
182
187
|
raise
|
|
183
188
|
|
|
189
|
+
self.fileHandler.check_file_size()
|
|
190
|
+
|
|
184
191
|
except Exception as ex:
|
|
185
192
|
|
|
186
193
|
raise MapleLoggerException(f"Failed to write log: {ex}") from ex
|
|
187
194
|
|
|
188
|
-
if self.config.maxLogSize > 0:
|
|
189
|
-
|
|
190
|
-
# Check file size
|
|
191
|
-
|
|
192
|
-
try:
|
|
193
|
-
|
|
194
|
-
if path.exists(self.config.logfile) and path.getsize(self.config.logfile) > self.config.maxLogSize:
|
|
195
|
-
|
|
196
|
-
# Rename log file
|
|
197
|
-
|
|
198
|
-
if self.config.fileMode == "overwrite":
|
|
199
|
-
|
|
200
|
-
if path.isfile(f"{self.config.logfile}_old.log"):
|
|
201
|
-
|
|
202
|
-
os.remove(f"{self.config.logfile}_old.log")
|
|
203
|
-
|
|
204
|
-
os.rename(self.config.logfile, f"{self.config.logfile}_old.log")
|
|
205
|
-
return
|
|
206
|
-
|
|
207
|
-
elif self.config.fileMode == "daily":
|
|
208
|
-
|
|
209
|
-
dateStr = ""
|
|
210
|
-
|
|
211
|
-
else:
|
|
212
|
-
|
|
213
|
-
dateStr = f"_{datetime.now():%Y%m%d_%H%M%S}"
|
|
214
|
-
|
|
215
|
-
i = 0
|
|
216
|
-
logCopyFile = f"{self.config.logfile}{dateStr}{i}.log"
|
|
217
|
-
|
|
218
|
-
while path.isfile(logCopyFile):
|
|
219
|
-
|
|
220
|
-
i += 1
|
|
221
|
-
logCopyFile = f"{self.config.logfile}{dateStr}{i}.log"
|
|
222
|
-
|
|
223
|
-
os.rename(self.config.logfile, logCopyFile)
|
|
224
|
-
|
|
225
|
-
except Exception as ex:
|
|
226
|
-
|
|
227
|
-
raise MapleLoggerException(f"Failed to rotate log file: {ex}") from ex
|
|
228
|
-
|
|
229
195
|
#
|
|
230
196
|
################################
|
|
231
197
|
# Trace
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: MapleX
|
|
3
|
-
Version: 3.1.0.
|
|
3
|
+
Version: 3.1.0.dev5
|
|
4
4
|
Summary: A Python library for simple logging, json file operations, Maple file format operations, and console color utilities.
|
|
5
5
|
Author: Ryuji Hazama
|
|
6
6
|
Project-URL: PyPI, https://pypi.org/project/MapleX/
|
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
maplex/__init__.py,sha256=
|
|
1
|
+
maplex/__init__.py,sha256=H0C9rORHtlwLhVbk4DEXHCYeBIWRqPpioh1OWSRmuqc,1881
|
|
2
2
|
maplex/jsonHandler.py,sha256=HZ4uCUMzs2oaAYWgInc6gzo960q46e7IOk7tTMzqdAE,7445
|
|
3
3
|
maplex/mapleColors.py,sha256=ASg-GstHkyXsq4mVMuFz8Qaq3nQeFayQWcCy9iHzjTs,1543
|
|
4
4
|
maplex/mapleExceptions.py,sha256=zN-GLYNGSWalQ8GiFH-BxRzYhSfbv6V-1O3cA46yAUo,4924
|
|
5
|
-
maplex/mapleLogger.py,sha256=
|
|
5
|
+
maplex/mapleLogger.py,sha256=rhSAFkrFt5vsHigvmyXaMp7kOi7LLA_oS5di6uIrYco,10380
|
|
6
6
|
maplex/mapleTreeEditor.py,sha256=5596bARJWHXyGUeXWRYx9lKSfaZdBmGQLvkF_OiKzZ0,29249
|
|
7
7
|
maplex/utils.py,sha256=TcUdfn9uhk9zwMTxtWgsZ1ZKdc2_i0ysgrksMbzvX1I,603
|
|
8
|
-
maplex/library/logger/__init__.py,sha256=
|
|
8
|
+
maplex/library/logger/__init__.py,sha256=TzK7rP_gnljIa_cSpApJgxQBD4huQO9qS5HJGcyuXAI,419
|
|
9
9
|
maplex/library/logger/config.py,sha256=l7YYUv3ZpO3q8Z_7SHGQ6rql8nd1-s5g9SBeZ6gZGi8,10924
|
|
10
|
-
maplex/library/logger/consts.py,sha256=
|
|
11
|
-
maplex/library/logger/file_handler.py,sha256=
|
|
10
|
+
maplex/library/logger/consts.py,sha256=b7fn9TYjJhPgaNPCtYP6f-8SoMGclbRY7TjuyR44kMI,780
|
|
11
|
+
maplex/library/logger/file_handler.py,sha256=MJpguK3oj3v3igGAQCHwtcxtLyC9bVDlUxs1ZekHPS4,2746
|
|
12
12
|
maplex/library/logger/formatter.py,sha256=aThfUV9vxtcSkNNFp9ukCtZFJbuHIuZsAQjpXc03cFc,7260
|
|
13
13
|
maplex/library/logger/log_levels.py,sha256=q4NpokbM6vEabLC7OS-JHZgnhbDROk0yHa5-uM3t_Uw,186
|
|
14
14
|
maplex/library/logger/utilities.py,sha256=-IIPsvrebE4K2fP0fIHT-D55463Zkq3M4SlD0dEZINI,2874
|
|
15
|
-
maplex-3.1.0.
|
|
16
|
-
maplex-3.1.0.
|
|
17
|
-
maplex-3.1.0.
|
|
18
|
-
maplex-3.1.0.
|
|
19
|
-
maplex-3.1.0.
|
|
15
|
+
maplex-3.1.0.dev5.dist-info/licenses/LICENSE,sha256=M7R85ReZ2srvowIPO_c9sHI3DV7QmxnLH93esXVhwGI,1068
|
|
16
|
+
maplex-3.1.0.dev5.dist-info/METADATA,sha256=QYh9oUNUMi-YQxFGHlyXvJA7H4Zf7x8cgVuIhwN5xyY,7389
|
|
17
|
+
maplex-3.1.0.dev5.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
|
|
18
|
+
maplex-3.1.0.dev5.dist-info/top_level.txt,sha256=ME8uFV82K0y0KYJrHVTMgc0hEBV67iXXUKsvom7BO5g,7
|
|
19
|
+
maplex-3.1.0.dev5.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|