MapleX 3.1.0.dev4__tar.gz → 3.1.0.dev5__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.
Files changed (37) hide show
  1. {maplex-3.1.0.dev4/src/MapleX.egg-info → maplex-3.1.0.dev5}/PKG-INFO +1 -1
  2. {maplex-3.1.0.dev4 → maplex-3.1.0.dev5}/pyproject.toml +1 -1
  3. {maplex-3.1.0.dev4 → maplex-3.1.0.dev5/src/MapleX.egg-info}/PKG-INFO +1 -1
  4. {maplex-3.1.0.dev4 → maplex-3.1.0.dev5}/src/maplex/__init__.py +1 -1
  5. {maplex-3.1.0.dev4 → maplex-3.1.0.dev5}/src/maplex/library/logger/__init__.py +2 -0
  6. {maplex-3.1.0.dev4 → maplex-3.1.0.dev5}/src/maplex/library/logger/consts.py +4 -1
  7. maplex-3.1.0.dev5/src/maplex/library/logger/file_handler.py +103 -0
  8. {maplex-3.1.0.dev4 → maplex-3.1.0.dev5}/src/maplex/mapleLogger.py +7 -41
  9. maplex-3.1.0.dev4/src/maplex/library/logger/file_handler.py +0 -3
  10. {maplex-3.1.0.dev4 → maplex-3.1.0.dev5}/LICENSE +0 -0
  11. {maplex-3.1.0.dev4 → maplex-3.1.0.dev5}/MANIFEST.in +0 -0
  12. {maplex-3.1.0.dev4 → maplex-3.1.0.dev5}/README.md +0 -0
  13. {maplex-3.1.0.dev4 → maplex-3.1.0.dev5}/logErrorOutputSample.png +0 -0
  14. {maplex-3.1.0.dev4 → maplex-3.1.0.dev5}/logOutputSample.png +0 -0
  15. {maplex-3.1.0.dev4 → maplex-3.1.0.dev5}/readmes/LoggingBestPractice.md +0 -0
  16. {maplex-3.1.0.dev4 → maplex-3.1.0.dev5}/readmes/README_ConsoleColors.md +0 -0
  17. {maplex-3.1.0.dev4 → maplex-3.1.0.dev5}/readmes/README_Exceptions.md +0 -0
  18. {maplex-3.1.0.dev4 → maplex-3.1.0.dev5}/readmes/README_Json.md +0 -0
  19. {maplex-3.1.0.dev4 → maplex-3.1.0.dev5}/readmes/README_Logger.md +0 -0
  20. {maplex-3.1.0.dev4 → maplex-3.1.0.dev5}/readmes/README_MapleTree.md +0 -0
  21. {maplex-3.1.0.dev4 → maplex-3.1.0.dev5}/setup.cfg +0 -0
  22. {maplex-3.1.0.dev4 → maplex-3.1.0.dev5}/src/MapleX.egg-info/SOURCES.txt +0 -0
  23. {maplex-3.1.0.dev4 → maplex-3.1.0.dev5}/src/MapleX.egg-info/dependency_links.txt +0 -0
  24. {maplex-3.1.0.dev4 → maplex-3.1.0.dev5}/src/MapleX.egg-info/requires.txt +0 -0
  25. {maplex-3.1.0.dev4 → maplex-3.1.0.dev5}/src/MapleX.egg-info/top_level.txt +0 -0
  26. {maplex-3.1.0.dev4 → maplex-3.1.0.dev5}/src/maplex/jsonHandler.py +0 -0
  27. {maplex-3.1.0.dev4 → maplex-3.1.0.dev5}/src/maplex/library/logger/config.py +0 -0
  28. {maplex-3.1.0.dev4 → maplex-3.1.0.dev5}/src/maplex/library/logger/formatter.py +0 -0
  29. {maplex-3.1.0.dev4 → maplex-3.1.0.dev5}/src/maplex/library/logger/log_levels.py +0 -0
  30. {maplex-3.1.0.dev4 → maplex-3.1.0.dev5}/src/maplex/library/logger/utilities.py +0 -0
  31. {maplex-3.1.0.dev4 → maplex-3.1.0.dev5}/src/maplex/mapleColors.py +0 -0
  32. {maplex-3.1.0.dev4 → maplex-3.1.0.dev5}/src/maplex/mapleExceptions.py +0 -0
  33. {maplex-3.1.0.dev4 → maplex-3.1.0.dev5}/src/maplex/mapleTreeEditor.py +0 -0
  34. {maplex-3.1.0.dev4 → maplex-3.1.0.dev5}/src/maplex/utils.py +0 -0
  35. {maplex-3.1.0.dev4 → maplex-3.1.0.dev5}/tests/test_logger_unittest.py +0 -0
  36. {maplex-3.1.0.dev4 → maplex-3.1.0.dev5}/tests/test_maplejson_unittest.py +0 -0
  37. {maplex-3.1.0.dev4 → maplex-3.1.0.dev5}/tests/test_mapletree_unittest.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: MapleX
3
- Version: 3.1.0.dev4
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/
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "MapleX"
7
- version = "3.1.0.dev4"
7
+ version = "3.1.0.dev5"
8
8
  description = """A Python library for simple logging, json file operations, Maple file format operations, and console color utilities."""
9
9
  keywords = ["logging", "logger", "json", "file operations", "maple file format", "console colors"]
10
10
  readme = "README.md"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: MapleX
3
- Version: 3.1.0.dev4
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/
@@ -52,7 +52,7 @@ __all__ = [
52
52
  'winUnHide'
53
53
  ]
54
54
 
55
- __version__ = "3.1.0.dev4"
55
+ __version__ = "3.1.0.dev5"
56
56
  __author__ = "Ryuji Hazama"
57
57
  __license__ = "MIT"
58
58
 
@@ -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",
@@ -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'
@@ -0,0 +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
+
11
+ """
12
+ File handler for logging.
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}")
@@ -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,3 +0,0 @@
1
- """
2
- File handler for logging.
3
- """
File without changes
File without changes
File without changes
File without changes