-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlog_filter.py
More file actions
executable file
·67 lines (54 loc) · 2.23 KB
/
log_filter.py
File metadata and controls
executable file
·67 lines (54 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python
"""Filter Home Assistant logs for a specific custom integration."""
import argparse
import logging
import sys
from pathlib import Path
def filter_ha_logs(log_file_path: Path, domain: str) -> None:
"""
Read a Home Assistant log file and print lines matching the domain's logger.
Args:
log_file_path: The path to the Home Assistant log file.
domain: The domain of your custom integration (e.g., 'power_load_balancer').
"""
logger = logging.getLogger(__name__)
# The logger name for a custom integration is typically 'custom_components.<domain>'
target_logger_prefix = f"custom_components.{domain}"
# We'll look for a pattern like '[custom_components.power_load_balancer'
try:
with Path.open(log_file_path, encoding="utf-8") as f:
for line in f:
# Check if the line contains the target logger prefix within brackets
if f"[{target_logger_prefix}" in line:
logger.info(
line.strip()
) # Log the line, removing leading/trailing whitespace
except FileNotFoundError:
logger.exception("Log file not found at %s", log_file_path)
sys.exit(1)
except Exception:
logger.exception("An error occurred: %s")
sys.exit(1)
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO, format="%(message)s")
parser = argparse.ArgumentParser(
description="Filter Home Assistant logs for a specific custom integration."
)
parser.add_argument(
"log_file_path",
help="The full path to the Home Assistant log file "
"(e.g., /config/home-assistant.log).",
)
parser.add_argument(
"-d",
"--domain",
default="power_load_balancer",
help="The domain of your custom integration (default: power_load_balancer).",
)
args = parser.parse_args()
logger = logging.getLogger(__name__)
# Basic check if the path exists, though open() will also catch FileNotFoundError
if not Path.exists(args.log_file_path):
logger.error("Specified log file path does not exist: %s", args.log_file_path)
sys.exit(1)
filter_ha_logs(args.log_file_path, args.domain)