Log Viewer¶
The LogViewer class provides interactive log viewing capabilities with support for both JSON and plaintext log files.
Quick Reference¶
from pylogshield import LogViewer
from pathlib import Path
# Create a viewer for a log file
viewer = LogViewer(Path("~/.logs/my_app.log").expanduser())
# Display last 100 logs
viewer.display_logs(limit=100)
# Filter by level and keyword
viewer.display_logs(limit=50, level="ERROR", keyword="database")
# Live follow (like tail -f)
viewer.follow_logs(level="INFO", interval=0.5)
Features¶
| Feature | Description |
|---|---|
| Static viewing | Display last N lines with filtering |
| Live following | Real-time updates as logs are written |
| Level filtering | Show only logs at or above a level |
| Keyword search | Filter logs containing specific text |
| JSON support | Auto-detects and parses JSON logs |
| Rich output | Colorized table output with Rich |
Examples¶
Basic Log Viewing¶
from pylogshield import LogViewer
from pathlib import Path
viewer = LogViewer(Path("/var/log/app.log"))
# Display last 200 lines (default)
viewer.display_logs()
# Display last 50 lines
viewer.display_logs(limit=50)
Filtering by Log Level¶
viewer = LogViewer(Path("app.log"))
# Show only ERROR and above
viewer.display_logs(level="ERROR")
# Show only WARNING and above
viewer.display_logs(level="WARNING", limit=100)
# Using numeric level
viewer.display_logs(level=30) # WARNING = 30
Keyword Filtering¶
viewer = LogViewer(Path("app.log"))
# Search for logs containing "user_id"
viewer.display_logs(keyword="user_id")
# Combine level and keyword filters
viewer.display_logs(level="ERROR", keyword="timeout", limit=50)
Live Following¶
viewer = LogViewer(Path("app.log"))
# Follow logs in real-time (press Ctrl+C to stop)
viewer.follow_logs()
# With filtering
viewer.follow_logs(level="ERROR", keyword="critical")
# Custom refresh interval (default: 0.5 seconds)
viewer.follow_logs(interval=1.0)
# Limit buffer size (default: 500 lines)
viewer.follow_logs(max_lines=200)
JSON Log Parsing¶
The viewer automatically detects and parses JSON-formatted logs:
# If your log file contains lines like:
# {"timestamp": "2024-01-15T10:30:00", "level": "INFO", "message": "User logged in"}
viewer = LogViewer(Path("json_app.log"))
viewer.display_logs(limit=50)
# The viewer extracts timestamp, level, and message fields
API Reference¶
LogViewer(log_file: Path)
¶
Interactive log viewer with support for JSON and plaintext log files.
Provides both static viewing (tail) and live following (tail -f) capabilities with filtering by log level and keyword search.
| PARAMETER | DESCRIPTION |
|---|---|
|
Path to the log file to view.
TYPE:
|
| ATTRIBUTE | DESCRIPTION |
|---|---|
log_file |
Resolved path to the log file.
TYPE:
|
console |
Rich console instance for output.
TYPE:
|
Examples:
>>> viewer = LogViewer(Path("/var/log/app.log"))
>>> viewer.display_logs(limit=100, level="ERROR")
>>> viewer.follow_logs(level="INFO", keyword="user")
| METHOD | DESCRIPTION |
|---|---|
display_logs |
Display log entries in a formatted Rich table. |
follow_logs |
Live-follow the log file, displaying new lines as they are written. |
Source code in src/pylogshield/viewer.py
display_logs(*, limit: int = 200, level: Optional[Union[int, str]] = None, keyword: Optional[str] = None) -> bool
¶
Display log entries in a formatted Rich table.
| PARAMETER | DESCRIPTION |
|---|---|
|
Maximum number of lines to display from the end. Default is 200.
TYPE:
|
|
Minimum log level to display (e.g., "INFO" or 20). Default is None (show all levels).
TYPE:
|
|
Only show lines containing this keyword (case-insensitive). Default is None (no filtering).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if logs were rendered successfully, False if file not found. |
Source code in src/pylogshield/viewer.py
follow_logs(*, level: Optional[Union[int, str]] = None, keyword: Optional[str] = None, interval: float = 0.5, max_lines: int = 500) -> bool
¶
Live-follow the log file, displaying new lines as they are written.
Similar to tail -f, this method continuously monitors the log file
and displays new entries in a Rich live-updating table.
| PARAMETER | DESCRIPTION |
|---|---|
|
Minimum log level to display. Default is None (show all levels).
TYPE:
|
|
Only show lines containing this keyword (case-insensitive). Default is None (no filtering).
TYPE:
|
|
Refresh interval in seconds. Default is 0.5.
TYPE:
|
|
Maximum lines to keep in the rolling display window. Default is 500.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if following completed (via Ctrl+C), False if file not found. |
Notes
- Starts at end of file (only shows new lines).
- Automatically handles log rotation (file truncation).
- Press Ctrl+C to stop following.
Source code in src/pylogshield/viewer.py
| Python | |
|---|---|
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 | |