From 55918134fd91dd00cf6347c84112bfb897850f36 Mon Sep 17 00:00:00 2001 From: MattHag <16444067+MattHag@users.noreply.github.com> Date: Wed, 6 Nov 2024 20:34:51 +0100 Subject: [PATCH] Introduce custom logger Implement logger that internally checks if log level is enabled. Thus, unnecessary log message computation costs are avoid, when logging is disabled and logging code can be cut in half. Related #2663 --- lib/solaar/custom_logger.py | 28 ++++++++++++++++++++++++++++ lib/solaar/gtk.py | 2 ++ 2 files changed, 30 insertions(+) create mode 100644 lib/solaar/custom_logger.py diff --git a/lib/solaar/custom_logger.py b/lib/solaar/custom_logger.py new file mode 100644 index 0000000000..a4337bf171 --- /dev/null +++ b/lib/solaar/custom_logger.py @@ -0,0 +1,28 @@ +import logging + + +class CustomLogger(logging.Logger): + """Logger, that avoids unnecessary string computations. + + Does not compute messages for disabled log levels. + """ + + def debug(self, msg, *args, **kwargs): + if self.isEnabledFor(logging.DEBUG): + super().debug(msg, *args, **kwargs) + + def info(self, msg, *args, **kwargs): + if self.isEnabledFor(logging.INFO): + super().info(msg, *args, **kwargs) + + def warning(self, msg, *args, **kwargs): + if self.isEnabledFor(logging.WARNING): + super().warning(msg, *args, **kwargs) + + def error(self, msg, *args, **kwargs): + if self.isEnabledFor(logging.ERROR): + super().error(msg, *args, **kwargs) + + def critical(self, msg, *args, **kwargs): + if self.isEnabledFor(logging.CRITICAL): + super().critical(msg, *args, **kwargs) diff --git a/lib/solaar/gtk.py b/lib/solaar/gtk.py index 1cbee6188a..9d9aea6230 100755 --- a/lib/solaar/gtk.py +++ b/lib/solaar/gtk.py @@ -36,7 +36,9 @@ from solaar import dbus from solaar import listener from solaar import ui +from solaar.custom_logger import CustomLogger +logging.setLoggerClass(CustomLogger) logger = logging.getLogger(__name__)