|
15 | 15 | LEVEL_NAMES = ["DBG", "INF", "WRN", "ERR"]
|
16 | 16 | LEVEL_COLOR = ["dark_grey", "green", "yellow", "red"]
|
17 | 17 |
|
18 |
| -console_lock = threading.Lock() |
19 |
| - |
20 |
| - |
21 |
| -def Log(level, context, msg): |
22 |
| - # Check if the current level meets the threshold |
23 |
| - if level >= Config.LOG_LEVEL: # Use the LOG_LEVEL from the Config module |
24 |
| - # Check if the context is in the list of ignored contexts |
25 |
| - if context in Config.IGNORED_LOG_CONTEXTS: |
26 |
| - return |
27 |
| - with console_lock: |
28 |
| - timestamp = colored(datetime.datetime.now().strftime("%m/%d/%y %H:%M:%S"), "dark_grey") |
29 |
| - # Translate level number to name and color |
30 |
| - level_name = colored(LEVEL_NAMES[level], LEVEL_COLOR[level]) |
31 |
| - # Left justify the context and color it blue |
32 |
| - context = colored(context.ljust(14), "blue") |
33 |
| - # Left justify the threadid and color it blue |
34 |
| - thread_id = colored(str(threading.get_ident()).ljust(5), "blue") |
35 |
| - # color the msg based on the level |
36 |
| - msg = colored(msg, LEVEL_COLOR[level]) |
37 |
| - print(f"{thread_id} {timestamp} {level_name}: [{context}] {msg}") |
| 18 | + |
| 19 | +class BaseLogger: |
| 20 | + def __init__(self): |
| 21 | + self._lock = threading.Lock() |
| 22 | + |
| 23 | + def Log(self, level, context, msg): |
| 24 | + # Check if the current level meets the threshold |
| 25 | + if level >= Config.LOG_LEVEL: # Use the LOG_LEVEL from the Config module |
| 26 | + # Check if the context is in the list of ignored contexts |
| 27 | + if context in Config.IGNORED_LOG_CONTEXTS: |
| 28 | + return |
| 29 | + with self._lock: |
| 30 | + self.WriteLog(level, context, msg) |
| 31 | + |
| 32 | + def WriteLog(self, level, context, msg): |
| 33 | + raise NotImplementedError("Subclasses must implement this method") |
| 34 | + |
| 35 | + |
| 36 | +class ConsoleLogger(BaseLogger): |
| 37 | + def __init__(self): |
| 38 | + super().__init__() |
| 39 | + |
| 40 | + def WriteLog(self, level, context, msg): |
| 41 | + timestamp = colored(datetime.datetime.now().strftime("%m/%d/%y %H:%M:%S"), "pink") |
| 42 | + # Translate level number to name and color |
| 43 | + level_name = colored(LEVEL_NAMES[level], LEVEL_COLOR[level]) |
| 44 | + # Left justify the context and color it blue |
| 45 | + context = colored(context.ljust(14), "blue") |
| 46 | + # Left justify the threadid and color it blue |
| 47 | + thread_id = colored(str(threading.get_ident()).ljust(5), "blue") |
| 48 | + # color the msg based on the level |
| 49 | + msg = colored(msg, LEVEL_COLOR[level]) |
| 50 | + print(f"{thread_id} {timestamp} {level_name}: [{context}] {msg}") |
| 51 | + |
| 52 | + |
| 53 | +LOGGER = ConsoleLogger() |
38 | 54 |
|
39 | 55 |
|
40 | 56 | def Debug(context, message):
|
41 |
| - Log(DEBUG, context, message) |
| 57 | + LOGGER.Log(DEBUG, context, message) |
42 | 58 |
|
43 | 59 |
|
44 | 60 | def Info(context, message):
|
45 |
| - Log(INFO, context, message) |
| 61 | + LOGGER.Log(INFO, context, message) |
46 | 62 |
|
47 | 63 |
|
48 | 64 | def Warn(context, message):
|
49 |
| - Log(WARN, context, message) |
| 65 | + LOGGER.Log(WARN, context, message) |
50 | 66 |
|
51 | 67 |
|
52 | 68 | def Error(context, message):
|
53 |
| - Log(ERROR, context, message) |
| 69 | + LOGGER.Log(ERROR, context, message) |
54 | 70 |
|
55 | 71 |
|
56 | 72 | def shorten(msg, num_parts=5, max_len=100):
|
|
0 commit comments