Skip to content

Commit e2bc66b

Browse files
Improved Logger.py
Protection of `raw` logging to be `_raw` Resource heavy `inspect.stack()[1]` is now a less intensive `inspect.currentframe().f_back` Improved `message is not None` check to be more efficient Added regex to remove ANSI escape codes from being logged into the file (as they produce cluttered logs) Signed-off-by: Shahm Najeeb <Nirt_12023@outlook.com>
1 parent d29e2f6 commit e2bc66b

1 file changed

Lines changed: 20 additions & 26 deletions

File tree

CODE/logicytics/Logger.py

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import inspect
44
import logging
55
import os
6+
import re
67
import time
78
from datetime import datetime
89
from typing import Type
@@ -99,13 +100,11 @@ def __init__(self, config: dict = None):
99100

100101
if not os.path.exists(self.filename):
101102
self.newline()
102-
self.raw(
103-
"| Timestamp | LOG Level |"
104-
+ " " * 71
105-
+ "LOG Messages"
106-
+ " " * 71
107-
+ "|"
108-
)
103+
self._raw("| Timestamp | LOG Level |"
104+
+ " " * 71
105+
+ "LOG Messages"
106+
+ " " * 71
107+
+ "|")
109108
elif os.path.exists(self.filename) and config.get("delete_log", False):
110109
with open(self.filename, "w") as f:
111110
f.write(
@@ -177,7 +176,7 @@ def debug(self, message):
177176
if self.color and message != "None" and message is not None:
178177
colorlog.debug(str(message))
179178

180-
def raw(self, message):
179+
def _raw(self, message):
181180
"""
182181
Log a raw message directly to the log file.
183182
@@ -201,12 +200,16 @@ def raw(self, message):
201200
Raises:
202201
Logs internal errors for Unicode or file writing issues without stopping execution
203202
"""
204-
frame = inspect.stack()[1]
205-
if frame.function == "<module>":
203+
frame = inspect.currentframe().f_back
204+
if frame and frame.f_code.co_name == "<module>":
206205
self.__internal(
207206
f"Raw message called from a non-function - This is not recommended"
208207
)
209-
if message != "None" and message is not None:
208+
# Precompiled regex for ANSI escape codes
209+
# Remove all ANSI escape sequences in one pass
210+
message = re.compile(r'\033\[\d+(;\d+)*m').sub('', message)
211+
212+
if message and message != "None":
210213
try:
211214
with open(self.filename, "a", encoding="utf-8") as f:
212215
f.write(f"{str(message)}\n")
@@ -239,9 +242,7 @@ def info(self, message):
239242
"""
240243
if self.color and message != "None" and message is not None:
241244
colorlog.info(str(message))
242-
self.raw(
243-
f"[{self.__timestamp()}] > INFO: | {self.__trunc_message(str(message))}"
244-
)
245+
self._raw(f"[{self.__timestamp()}] > INFO: | {self.__trunc_message(str(message))}")
245246

246247
def warning(self, message):
247248
"""
@@ -251,9 +252,7 @@ def warning(self, message):
251252
"""
252253
if self.color and message != "None" and message is not None:
253254
colorlog.warning(str(message))
254-
self.raw(
255-
f"[{self.__timestamp()}] > WARNING: | {self.__trunc_message(str(message))}"
256-
)
255+
self._raw(f"[{self.__timestamp()}] > WARNING: | {self.__trunc_message(str(message))}")
257256

258257
def error(self, message):
259258
"""
@@ -263,9 +262,7 @@ def error(self, message):
263262
"""
264263
if self.color and message != "None" and message is not None:
265264
colorlog.error(str(message))
266-
self.raw(
267-
f"[{self.__timestamp()}] > ERROR: | {self.__trunc_message(str(message))}"
268-
)
265+
self._raw(f"[{self.__timestamp()}] > ERROR: | {self.__trunc_message(str(message))}")
269266

270267
def critical(self, message):
271268
"""
@@ -275,9 +272,7 @@ def critical(self, message):
275272
"""
276273
if self.color and message != "None" and message is not None:
277274
colorlog.critical(str(message))
278-
self.raw(
279-
f"[{self.__timestamp()}] > CRITICAL: | {self.__trunc_message(str(message))}"
280-
)
275+
self._raw(f"[{self.__timestamp()}] > CRITICAL: | {self.__trunc_message(str(message))}")
281276

282277
def string(self, message, type: str):
283278
"""
@@ -329,9 +324,8 @@ def exception(self, message, exception_type: Type = Exception):
329324
- Includes both the original message and the exception type in the log
330325
"""
331326
if self.color and message != "None" and message is not None:
332-
self.raw(
333-
f"[{self.__timestamp()}] > EXCEPTION:| {self.__trunc_message(f'{message} -> Exception provoked: {str(exception_type)}')}"
334-
)
327+
self._raw(
328+
f"[{self.__timestamp()}] > EXCEPTION:| {self.__trunc_message(f'{message} -> Exception provoked: {str(exception_type)}')}")
335329
raise exception_type(message)
336330

337331
def execution(self, message_log: list[list[str, str]]):

0 commit comments

Comments
 (0)