added control logger
This commit is contained in:
+35
-5
@@ -79,6 +79,13 @@ class Controller:
|
||||
def handle_event(self, message: dict) -> None:
|
||||
"""Route a validated plugin event to its configured action."""
|
||||
self._validate_event(message)
|
||||
LOG.info(
|
||||
"event received: sender=%s event=%s id=%s text=%r",
|
||||
message["sender"],
|
||||
message["event"],
|
||||
message["id"],
|
||||
message["text"],
|
||||
)
|
||||
plugin_spec = self.plugin_specs.get(message["sender"])
|
||||
if not isinstance(plugin_spec, dict):
|
||||
LOG.warning("no controller route for plugin %s", message["sender"])
|
||||
@@ -104,7 +111,19 @@ class Controller:
|
||||
f"for plugin {message['sender']!r}"
|
||||
)
|
||||
handler = _configured_callable(action_spec, "handler", "on_event")
|
||||
LOG.info(
|
||||
"action started: action=%s event=%s id=%s",
|
||||
action_name,
|
||||
message["event"],
|
||||
message["id"],
|
||||
)
|
||||
handler(self._component_config(action_spec), message)
|
||||
LOG.info(
|
||||
"action completed: action=%s event=%s id=%s",
|
||||
action_name,
|
||||
message["event"],
|
||||
message["id"],
|
||||
)
|
||||
|
||||
def _load_plugins(self) -> None:
|
||||
for name, spec in self.plugin_specs.items():
|
||||
@@ -164,17 +183,28 @@ def run(config: dict, once: bool = False) -> int:
|
||||
return controller.check_once() if once else controller.run()
|
||||
|
||||
|
||||
def configure_logging(config: dict) -> None:
|
||||
"""Configure console logging and an optional append-only log file."""
|
||||
logging_config = config.get("controller", {}).get("logging", {})
|
||||
level = getattr(logging, str(logging_config.get("level", "INFO")).upper())
|
||||
handlers: list[logging.Handler] = [logging.StreamHandler()]
|
||||
filename = logging_config.get("file")
|
||||
if filename:
|
||||
handlers.append(logging.FileHandler(filename, encoding="utf-8"))
|
||||
logging.basicConfig(
|
||||
level=level,
|
||||
format="%(asctime)s %(levelname)s %(message)s",
|
||||
handlers=handlers,
|
||||
)
|
||||
|
||||
|
||||
def main() -> int:
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--config", type=Path, default=Path(__file__).with_name("service.yaml"))
|
||||
parser.add_argument("--once", action="store_true", help="check plugins once")
|
||||
args = parser.parse_args()
|
||||
config = load_config(args.config)
|
||||
logging_config = config.get("controller", {}).get("logging", {})
|
||||
logging.basicConfig(
|
||||
level=getattr(logging, str(logging_config.get("level", "INFO")).upper()),
|
||||
format="%(asctime)s %(levelname)s %(message)s",
|
||||
)
|
||||
configure_logging(config)
|
||||
return run(config, args.once)
|
||||
|
||||
|
||||
|
||||
+6
-5
@@ -17,6 +17,7 @@ controller:
|
||||
|
||||
logging:
|
||||
level: INFO
|
||||
file: /tmp/home_control_log.txt
|
||||
|
||||
arrival_detection:
|
||||
enabled: true
|
||||
@@ -29,7 +30,7 @@ arrival_detection:
|
||||
verify_tls: true
|
||||
hosts_oid: lanhosts
|
||||
# Log every router/API call and its result without logging credentials.
|
||||
debug: true
|
||||
debug: false
|
||||
|
||||
devices:
|
||||
- id: ignace
|
||||
@@ -69,15 +70,15 @@ notify:
|
||||
|
||||
cloud_logger:
|
||||
enabled: true
|
||||
event_url: http://www.suy.nl:29651/home_logger/event
|
||||
event_url: http://www.suy.nl/home_logger/event
|
||||
timeout: 10
|
||||
filter:
|
||||
# Empty accept lists allow all values. Ignore lists always take precedence.
|
||||
accept:
|
||||
events: [empty]
|
||||
events: []
|
||||
ids: []
|
||||
ignore:
|
||||
events: [arrived, departed]
|
||||
events: []
|
||||
ids: []
|
||||
# Available placeholders: {sender}, {event}, {id}, {text}.
|
||||
message: "{datetime} {sender}: {event} ({id})"
|
||||
message: "{datetime} {sender}: {event} ({id})"
|
||||
|
||||
Reference in New Issue
Block a user