|
8 | 8 | from pathlib import Path |
9 | 9 | from typing import TYPE_CHECKING, Any |
10 | 10 |
|
11 | | -from .storage import storage |
12 | 11 | from .utils.unicode import unicode_ljust, unicode_rjust |
13 | 12 |
|
14 | 13 | if TYPE_CHECKING: |
@@ -147,30 +146,46 @@ def log(message: str, level: int = logging.DEBUG) -> None: |
147 | 146 | log_adapter.log(level, message) |
148 | 147 |
|
149 | 148 |
|
150 | | -def _check_log_permissions() -> None: |
151 | | - filename = storage.get('LOG_FILE', None) |
152 | | - log_dir = storage.get('LOG_PATH', Path('./')) |
| 149 | +class Logger: |
| 150 | + def __init__(self, path: Path = Path('/var/log/archinstall')) -> None: |
| 151 | + self._path = path |
153 | 152 |
|
154 | | - if not filename: |
155 | | - raise ValueError('No log file name defined') |
| 153 | + @property |
| 154 | + def path(self) -> Path: |
| 155 | + return self._path / 'install.log' |
156 | 156 |
|
157 | | - log_file = log_dir / filename |
| 157 | + @property |
| 158 | + def directory(self) -> Path: |
| 159 | + return self._path |
158 | 160 |
|
159 | | - try: |
160 | | - log_dir.mkdir(exist_ok=True, parents=True) |
161 | | - log_file.touch(exist_ok=True) |
| 161 | + def _check_permissions(self) -> None: |
| 162 | + log_file = self.path |
162 | 163 |
|
163 | | - with log_file.open('a') as fp: |
164 | | - fp.write('') |
165 | | - except PermissionError: |
166 | | - # Fallback to creating the log file in the current folder |
167 | | - fallback_dir = Path('./').absolute() |
168 | | - fallback_log_file = fallback_dir / filename |
| 164 | + try: |
| 165 | + self._path.mkdir(exist_ok=True, parents=True) |
| 166 | + log_file.touch(exist_ok=True) |
| 167 | + |
| 168 | + with log_file.open('a') as f: |
| 169 | + f.write('') |
| 170 | + except PermissionError: |
| 171 | + # Fallback to creating the log file in the current folder |
| 172 | + logger._path = Path('./').absolute() |
| 173 | + |
| 174 | + warn( |
| 175 | + f'Not enough permission to place log file at {log_file},', |
| 176 | + 'creating it in {logger.path} instead' |
| 177 | + ) |
| 178 | + |
| 179 | + def log(self, level: int, content: str) -> None: |
| 180 | + self._check_permissions() |
169 | 181 |
|
170 | | - fallback_log_file.touch(exist_ok=True) |
| 182 | + with self.path.open('a') as f: |
| 183 | + ts = _timestamp() |
| 184 | + level_name = logging.getLevelName(level) |
| 185 | + f.write(f'[{ts}] - {level_name} - {content}\n') |
171 | 186 |
|
172 | | - storage['LOG_PATH'] = fallback_dir |
173 | | - warn(f'Not enough permission to place log file at {log_file}, creating it in {fallback_log_file} instead') |
| 187 | + |
| 188 | +logger = Logger() |
174 | 189 |
|
175 | 190 |
|
176 | 191 | def _supports_color() -> bool: |
@@ -309,25 +324,15 @@ def log( |
309 | 324 | reset: bool = False, |
310 | 325 | font: list[Font] = [], |
311 | 326 | ) -> None: |
312 | | - # leave this check here as we need to setup the logging |
313 | | - # right from the beginning when the modules are loaded |
314 | | - _check_log_permissions() |
| 327 | + text = ' '.join([str(x) for x in msgs]) |
315 | 328 |
|
316 | | - text = orig_string = ' '.join([str(x) for x in msgs]) |
| 329 | + logger.log(level, text) |
317 | 330 |
|
318 | 331 | # Attempt to colorize the output if supported |
319 | 332 | # Insert default colors and override with **kwargs |
320 | 333 | if _supports_color(): |
321 | 334 | text = _stylize_output(text, fg, bg, reset, font) |
322 | 335 |
|
323 | | - log_file = storage['LOG_PATH'] / storage['LOG_FILE'] |
324 | | - |
325 | | - with log_file.open('a') as fp: |
326 | | - ts = _timestamp() |
327 | | - level_name = logging.getLevelName(level) |
328 | | - out = f'[{ts}] - {level_name} - {orig_string}\n' |
329 | | - fp.write(out) |
330 | | - |
331 | 336 | Journald.log(text, level=level) |
332 | 337 |
|
333 | 338 | if level != logging.DEBUG: |
|
0 commit comments