Some checks failed
- Added @click.version_option decorator to main() in commands.py - Imported __version__ from loglens package - Resolves CI build failure: 'loglens --version' command not found
76 lines
2.5 KiB
Python
76 lines
2.5 KiB
Python
from typing import Optional
|
|
|
|
from rich.console import Console
|
|
from rich.table import Table
|
|
|
|
from loglens.analyzers.analyzer import AnalysisResult, ParsedEntry
|
|
|
|
|
|
class TableFormatter:
|
|
"""Format analysis results as a table."""
|
|
|
|
def __init__(self, max_entries: int = 100):
|
|
self.max_entries = max_entries
|
|
self.console = Console()
|
|
|
|
def format(self, result: AnalysisResult) -> None:
|
|
"""Format result as a table."""
|
|
self._print_summary(result)
|
|
self._print_entries(result.entries)
|
|
|
|
def _print_summary(self, result: AnalysisResult) -> None:
|
|
"""Print summary statistics."""
|
|
self.console.print("[bold]Log Analysis Summary[/]")
|
|
self.console.print(f" Total Lines: {result.total_lines}")
|
|
self.console.print(f" Format: {result.format_detected.value}")
|
|
|
|
severity_colors = {
|
|
"critical": "red",
|
|
"error": "red",
|
|
"warning": "yellow",
|
|
"info": "blue",
|
|
"debug": "grey",
|
|
}
|
|
|
|
for severity, count in [
|
|
("critical", result.critical_count),
|
|
("error", result.error_count),
|
|
("warning", result.warning_count),
|
|
("info", result.info_count if hasattr(result, 'info_count') else 0),
|
|
("debug", result.debug_count),
|
|
]:
|
|
color = severity_colors.get(severity, "white")
|
|
self.console.print(f" [{color}]{severity.upper()}: {count}[/]")
|
|
|
|
if result.suggestions:
|
|
self.console.print("\n[bold]Suggestions:[/]")
|
|
for i, suggestion in enumerate(result.suggestions, 1):
|
|
self.console.print(f" {i}. {suggestion}")
|
|
|
|
def _print_entries(self, entries: list[ParsedEntry]) -> None:
|
|
"""Print log entries as a table."""
|
|
if not entries:
|
|
return
|
|
|
|
table = Table(title="Log Entries")
|
|
table.add_column("Timestamp", style="cyan", no_wrap=True)
|
|
table.add_column("Level", style="magenta")
|
|
table.add_column("Message", style="green")
|
|
|
|
for entry in entries[:self.max_entries]:
|
|
severity_style = {
|
|
"critical": "bold red",
|
|
"error": "red",
|
|
"warning": "yellow",
|
|
"info": "blue",
|
|
"debug": "grey",
|
|
}.get(entry.severity, "white")
|
|
|
|
table.add_row(
|
|
entry.timestamp or "N/A",
|
|
f"[{severity_style}]{entry.level or 'N/A'}[/]",
|
|
entry.message[:100] if entry.message else "N/A",
|
|
)
|
|
|
|
self.console.print(table)
|