|
18 | 18 | import traceback |
19 | 19 | from collections import OrderedDict |
20 | 20 |
|
21 | | -import adafruit_pathlib |
22 | | - |
23 | 21 | from .nvm.counter import Counter |
24 | 22 |
|
25 | 23 |
|
@@ -79,31 +77,25 @@ class LogLevel: |
79 | 77 | class Logger: |
80 | 78 | """Handles logging messages with different severity levels.""" |
81 | 79 |
|
| 80 | + _log_dir: str | None = None |
| 81 | + |
82 | 82 | def __init__( |
83 | 83 | self, |
84 | 84 | error_counter: Counter, |
85 | | - sd_path: adafruit_pathlib.Path = None, |
86 | | - # sd_card: SDCardManager = None, |
87 | 85 | log_level: int = LogLevel.NOTSET, |
88 | 86 | colorized: bool = False, |
89 | 87 | ) -> None: |
90 | 88 | """ |
91 | 89 | Initializes the Logger instance. |
92 | 90 |
|
93 | 91 | Args: |
94 | | - error_counter (Counter): Counter for error occurrences. |
95 | | - log_level (int): Initial log level. |
96 | | - colorized (bool): Whether to colorize output. |
| 92 | + error_counter: Counter for error occurrences. |
| 93 | + log_level: Initial log level. |
| 94 | + colorized: Whether to colorize output. |
97 | 95 | """ |
98 | 96 | self._error_counter: Counter = error_counter |
99 | | - self.sd_path: adafruit_pathlib.Path = sd_path |
100 | 97 | self._log_level: int = log_level |
101 | | - self.colorized: bool = colorized |
102 | | - |
103 | | - try: |
104 | | - self.sd_path = self.sd_path / "logs" |
105 | | - except TypeError as e: |
106 | | - print(f"path not set: {e}") |
| 98 | + self._colorized: bool = colorized |
107 | 99 |
|
108 | 100 | def _can_print_this_level(self, level_value: int) -> bool: |
109 | 101 | """ |
@@ -162,30 +154,15 @@ def _log(self, level: str, level_value: int, message: str, **kwargs) -> None: |
162 | 154 |
|
163 | 155 | json_order.update(kwargs) |
164 | 156 |
|
165 | | - try: |
166 | | - json_output = json.dumps(json_order) |
167 | | - except TypeError as e: |
168 | | - json_output = json.dumps( |
169 | | - OrderedDict( |
170 | | - [ |
171 | | - ("time", asctime), |
172 | | - ("level", "ERROR"), |
173 | | - ("msg", f"Failed to serialize log message: {e}"), |
174 | | - ] |
175 | | - ), |
176 | | - ) |
| 157 | + json_output = json.dumps(json_order) |
177 | 158 |
|
178 | 159 | if self._can_print_this_level(level_value): |
179 | | - # Write to sd card if mounted |
180 | | - if self.path: |
181 | | - if "logs" not in self.sd_path.iterdir(): |
182 | | - print("/sd/logs does not exist, creating...") |
183 | | - os.mkdir("/sd/logs") |
184 | | - |
185 | | - with open("/sd/logs/activity.log", "a") as f: |
| 160 | + if self._log_dir is not None: |
| 161 | + file = self._log_dir + os.sep + "activity.log" |
| 162 | + with open(file, "a") as f: |
186 | 163 | f.write(json_output + "\n") |
187 | 164 |
|
188 | | - if self.colorized: |
| 165 | + if self._colorized: |
189 | 166 | json_output = json_output.replace( |
190 | 167 | f'"level": "{level}"', f'"level": "{LogColors[level]}"' |
191 | 168 | ) |
@@ -256,3 +233,26 @@ def get_error_count(self) -> int: |
256 | 233 | int: The number of errors logged. |
257 | 234 | """ |
258 | 235 | return self._error_counter.get() |
| 236 | + |
| 237 | + def set_log_dir(self, log_dir: str) -> None: |
| 238 | + """ |
| 239 | + Sets the log directory for file logging. |
| 240 | +
|
| 241 | + Args: |
| 242 | + log_dir (str): Directory to save log files. |
| 243 | +
|
| 244 | + Raises: |
| 245 | + ValueError: If the provided path is not a valid directory. |
| 246 | + """ |
| 247 | + try: |
| 248 | + # Octal number 0o040000 is the stat mode indicating the file being stat'd is a directory |
| 249 | + directory_mode: int = 0o040000 |
| 250 | + st_mode = os.stat(log_dir)[0] |
| 251 | + if st_mode != directory_mode: |
| 252 | + raise ValueError( |
| 253 | + f"Logging path must be a directory, received {st_mode}." |
| 254 | + ) |
| 255 | + except OSError as e: |
| 256 | + raise ValueError("Invalid logging path.") from e |
| 257 | + |
| 258 | + self._log_dir = log_dir |
0 commit comments