Format log record into grid layout.
(self, record: logging.LogRecord)
| 288 | self.burst_suppression = burst_suppression |
| 289 | |
| 290 | def format(self, record: logging.LogRecord) -> str: |
| 291 | """Format log record into grid layout.""" |
| 292 | # Skip during Python shutdown to avoid ImportError |
| 293 | if sys.meta_path is None: |
| 294 | return record.getMessage() |
| 295 | |
| 296 | # Extract context variables with defaults |
| 297 | try: |
| 298 | req_id = request_id_var.get() |
| 299 | except LookupError: |
| 300 | req_id = " " |
| 301 | |
| 302 | try: |
| 303 | source = source_var.get() |
| 304 | except LookupError: |
| 305 | source = "SYS" |
| 306 | |
| 307 | # Normalize source to 5-letter code |
| 308 | source_normalized = normalize_source(source) |
| 309 | |
| 310 | # Column 1: Time (HH:MM:SS.mmm) - no date |
| 311 | now = datetime.now() |
| 312 | timestamp = now.strftime("%H:%M:%S.") + f"{int(now.microsecond / 1000):03d}" |
| 313 | if self.colorize: |
| 314 | time_col = f"{Colors.TIME}{timestamp}{Colors.RESET}" |
| 315 | else: |
| 316 | time_col = timestamp |
| 317 | |
| 318 | # Column 2: Level (3 chars) |
| 319 | level_abbrev = Colors.LEVEL_ABBREV.get( |
| 320 | record.levelname, record.levelname[:3].upper() |
| 321 | ) |
| 322 | if self.colorize: |
| 323 | level_color = Colors.LEVELS.get(record.levelname, Fore.WHITE) |
| 324 | level_col = f"{level_color}{level_abbrev}{Colors.RESET}" |
| 325 | else: |
| 326 | level_col = level_abbrev |
| 327 | |
| 328 | # Column 3: Source (fixed 5-char width) |
| 329 | if self.colorize: |
| 330 | source_color = Colors.SOURCES.get(source_normalized, Fore.WHITE) |
| 331 | source_col = f"{source_color}{source_normalized}{Colors.RESET}" |
| 332 | else: |
| 333 | source_col = source_normalized |
| 334 | |
| 335 | # Column 4: Request ID (fixed 7-char width) |
| 336 | id_display = req_id[: Columns.ID].ljust(Columns.ID) |
| 337 | if self.colorize: |
| 338 | id_col = f"{Colors.REQUEST_ID}{id_display}{Colors.RESET}" |
| 339 | else: |
| 340 | id_col = id_display |
| 341 | |
| 342 | # Column 6: Message with semantic highlighting |
| 343 | message = record.getMessage() |
| 344 | |
| 345 | # Skip separator lines (dashed lines) |
| 346 | if message.strip().startswith("---") or message.strip().startswith("==="): |
| 347 | # Skip separator lines entirely |