| 399 | |
| 400 | |
| 401 | class InteractivePrinter(BaseLiveTailPrinter): |
| 402 | _PROTECTED_KEYWORDS = [Keyword("ERROR"), Keyword("INFO"), Keyword("WARN")] |
| 403 | |
| 404 | def __init__( |
| 405 | self, |
| 406 | ui, |
| 407 | output: LiveTailBuffer, |
| 408 | log_events: list, |
| 409 | session_metadata: LiveTailSessionMetadata, |
| 410 | keywords_to_highlight: dict, |
| 411 | ) -> None: |
| 412 | self._ui = ui |
| 413 | self._output = output |
| 414 | self._log_events = log_events |
| 415 | self._session_metadata = session_metadata |
| 416 | self._log_events_displayed = 0 |
| 417 | self._is_sampled = False |
| 418 | self._keywords_to_highlight = keywords_to_highlight |
| 419 | self._format = OutputFormat.JSON |
| 420 | colorama.init(autoreset=True, strip=False) # noqa |
| 421 | |
| 422 | @property |
| 423 | def log_events_displayed(self): |
| 424 | return self._log_events_displayed |
| 425 | |
| 426 | @property |
| 427 | def is_sampled(self): |
| 428 | return self._is_sampled |
| 429 | |
| 430 | @property |
| 431 | def output_format(self): |
| 432 | return self._format |
| 433 | |
| 434 | def toggle_formatting(self): |
| 435 | self._format = ( |
| 436 | OutputFormat.PLAIN_TEXT |
| 437 | if self._format == OutputFormat.JSON |
| 438 | else OutputFormat.JSON |
| 439 | ) |
| 440 | |
| 441 | def _color_log_event(self, log_event: str): |
| 442 | for keyword in ( |
| 443 | list(self._keywords_to_highlight.values()) |
| 444 | + self._PROTECTED_KEYWORDS |
| 445 | ): |
| 446 | log_event = keyword.highlight(log_event) |
| 447 | |
| 448 | return log_event |
| 449 | |
| 450 | def _format_log_event(self, log_event: str): |
| 451 | if self._format == OutputFormat.JSON: |
| 452 | try: |
| 453 | log_event = json.loads(log_event) |
| 454 | return json.dumps(log_event, indent=4) |
| 455 | except json.decoder.JSONDecodeError: |
| 456 | pass |
| 457 | |
| 458 | return log_event |