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