Home Information - Your home, your data, organized.
Python
Added ability to suppress API status request logging messages.
Anthony Cassandra8a2ae299d399
diff --git a/src/hi/apps/security/monitors.py b/src/hi/apps/security/monitors.pyindex 098a2f87..427aab7d 100644--- a/src/hi/apps/security/monitors.py+++ b/src/hi/apps/security/monitors.py@@ -14,7 +14,7 @@ logger = logging.getLogger(__name__)class SecurityMonitor( PeriodicMonitor, SettingsMixin, SecurityMixin ):- SECURITY_POLLING_INTERVAL_SECS = 60+ SECURITY_POLLING_INTERVAL_SECS = 5def __init__( self ):super().__init__(@@ -29,7 +29,7 @@ class SecurityMonitor( PeriodicMonitor, SettingsMixin, SecurityMixin ):returnasync def _check_security_state( self ):- logger.debug( 'Checking security state.' )+ logger.debug( 'Security monitor: Checking security state.' )settings_manager = await self.settings_manager_async()if not settings_manager:return@@ -44,6 +44,7 @@ class SecurityMonitor( PeriodicMonitor, SettingsMixin, SecurityMixin ):try:# Some states do not allow automated changesif not security_manager.security_state.auto_change_allowed:+ logger.debug( f'Security state "{security_manager.security_state}". Auto-change not allowed.' )returnday_start_time_of_day = settings_manager.get_setting_value(@@ -73,7 +74,8 @@ class SecurityMonitor( PeriodicMonitor, SettingsMixin, SecurityMixin ):new_security_state = SecurityState.NIGHT,)return-++ logger.debug( 'Security monitor: No change needed.' )finally:self._last_security_state_check_datetime = current_datetimereturndiff --git a/src/hi/apps/security/security_manager.py b/src/hi/apps/security/security_manager.pyindex 5abab0fd..72108bee 100644--- a/src/hi/apps/security/security_manager.py+++ b/src/hi/apps/security/security_manager.py@@ -195,8 +195,11 @@ class SecurityManager( Singleton, SettingsMixin ):returndef update_security_state_auto( self, new_security_state : SecurityState ):- """Special updating when coming from automation since extra handling is- needed if state is in a delayed transition (via SET_AWAY or SNOOZE)."""+ """+ Special updating when coming from automation since extra handling is+ needed if state is in a delayed transition (via SET_AWAY or+ SNOOZE).+ """with self._security_status_lock:if not self._security_state.auto_change_allowed:logger.warning( f'Security state auto update but state={self._security_state}' )diff --git a/src/hi/log_filters.py b/src/hi/log_filters.pynew file mode 100644index 00000000..4ef48c89--- /dev/null+++ b/src/hi/log_filters.py@@ -0,0 +1,39 @@+import logging+import re++from django.conf import settings+from django.urls import resolve+++class SuppressSelectRequestEndpointsFilter(logging.Filter):+ """Filter out logs for specific URL names instead of hardcoded paths."""++ URL_NAMES_TO_FILTER = {+ "api_status",+ }++ def filter( self, record ):+ if not settings.SUPPRESS_SELECT_REQUEST_ENPOINTS_LOGGING:+ return True+ try:+ if not hasattr(record, "args") or ( len(record.args) < 1 ):+ return++ request_line = record.args[0]++ if not isinstance( request_line, str ):+ # Sometimes it apears to be a PosixPath, but for requests it is a string.+ return++ match = re.match( r'^[A-Z]+ (/[^ ?]*)', request_line )+ if not match:+ return++ request_path = match.group(1)+ match = resolve( request_path )+ if match.url_name in self.URL_NAMES_TO_FILTER:+ return False+ except Exception:+ pass++ return Truediff --git a/src/hi/settings/base.py b/src/hi/settings/base.pyindex fc262a4d..bac67e6c 100644--- a/src/hi/settings/base.py+++ b/src/hi/settings/base.py@@ -383,6 +383,14 @@ BASE_URL_FOR_EMAIL_LINKS = 'https://{SITE_DOMAIN}'# When tests functionality requires knowing if in unit test context.UNIT_TESTING = False+# In development and debugging, because the background javascript is+# polling frequently, this clutters up the console with log messages which+# makes it hard to sort through the other things logging. This allows+# suppressing those via a logging filter. This only applies if the logging+# configuration is using that special filter (in hi.log_filters".+#+SUPPRESS_SELECT_REQUEST_ENPOINTS_LOGGING = True+# In development and debugging, the debug noise and interference from the# background periodic monitoring tasks can be a problem. This gives a way# to turn them off with one setting.diff --git a/src/hi/settings/development.py b/src/hi/settings/development.pyindex 76f83372..b2ee44ed 100644--- a/src/hi/settings/development.py+++ b/src/hi/settings/development.py@@ -91,6 +91,16 @@ STATIC_ROOT = '/tmp/hi/static'LOGGING = {'version': 1,'disable_existing_loggers': False,++ # Since the API status gets polled frequently, this gums up the+ # terminal and make developing and debugging everything else more+ # unpleasant.+ #+ 'filters': {+ 'suppress_select_request_endpoints': {+ '()': 'hi.log_filters.SuppressSelectRequestEndpointsFilter',+ },+ },'formatters': {'simple': {'format': '{levelname} {message}',@@ -100,6 +110,7 @@ LOGGING = {'handlers': {'console': {'class': 'logging.StreamHandler',+ 'filters': [ 'suppress_select_request_endpoints' ],'formatter': 'simple',},},@@ -116,53 +127,58 @@ LOGGING = {'handlers': ['console'],'level': 'INFO',},- "django.core.mail": {- "handlers": ["console"],- "level": "DEBUG",+ 'django.core.mail': {+ 'handlers': ['console'],+ 'level': 'DEBUG',},'hi.apps.alert': {'handlers': ['console' ],- 'level': 'DEBUG',+ 'level': 'INFO','propagate': False,},'hi.apps.control': {'handlers': ['console' ],- 'level': 'DEBUG',+ 'level': 'INFO','propagate': False,},'hi.apps.notify': {'handlers': ['console' ],- 'level': 'DEBUG',+ 'level': 'INFO','propagate': False,},'hi.apps.monitor': {'handlers': ['console' ],- 'level': 'DEBUG',+ 'level': 'INFO','propagate': False,},'hi.apps.sense': {'handlers': ['console' ],- 'level': 'DEBUG',+ 'level': 'INFO',+ 'propagate': False,+ },+ 'hi.apps.security': {+ 'handlers': ['console' ],+ 'level': 'INFO','propagate': False,},'hi.integrations': {'handlers': ['console' ],- 'level': 'DEBUG',+ 'level': 'INFO','propagate': False,},'hi.services.hass': {'handlers': ['console' ],- 'level': 'DEBUG',+ 'level': 'INFO','propagate': False,},'hi.services.zoneminder': {'handlers': ['console' ],- 'level': 'DEBUG',+ 'level': 'INFO','propagate': False,},'hi': {'handlers': ['console' ],- 'level': 'DEBUG',+ 'level': 'INFO',},},}@@ -171,6 +187,7 @@ BASE_URL_FOR_EMAIL_LINKS = 'http:/127.0.0.1:8000/'# Uncomment to suppress email sending and write to console.#-# EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"+# EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'+SUPPRESS_SELECT_REQUEST_ENPOINTS_LOGGING = TrueSUPPRESS_MONITORS = False