[elbe-devel] [PATCH 4/8] elbepack: log: simplify loghandler registration

Thomas Weißschuh thomas.weissschuh at linutronix.de
Fri Aug 9 14:47:19 CEST 2024


Instead of registering the loghandler somewhere inside the decorator,
use an explicit table.
This makes the decorator simpler and the logic easier to follow.

Signed-off-by: Thomas Weißschuh <thomas.weissschuh at linutronix.de>
---
 elbepack/log.py | 33 ++++++++++++++++-----------------
 1 file changed, 16 insertions(+), 17 deletions(-)

diff --git a/elbepack/log.py b/elbepack/log.py
index d7b66b1e2497..58fb3333b368 100644
--- a/elbepack/log.py
+++ b/elbepack/log.py
@@ -19,8 +19,6 @@ msgonly_fmt = logging.Formatter('%(message)s')
 log = logging.getLogger('log')
 soap = logging.getLogger('soap')
 
-logging_methods = []
-
 
 class LoggingQueue(collections.deque):
     def __init__(self):
@@ -107,19 +105,16 @@ def with_list(func):
     return wrapper
 
 
-def logging_method(name):
-    def decorator(func):
-        def wrapper(*args, **kwargs):
-            for handlers in func(*args, **kwargs):
-                for h in handlers:
-                    local.handlers.append(h)
-                    root.addHandler(h)
-        logging_methods.append((name, wrapper))
-        return wrapper
-    return decorator
+def logging_method(func):
+    def wrapper(*args, **kwargs):
+        for handlers in func(*args, **kwargs):
+            for h in handlers:
+                local.handlers.append(h)
+                root.addHandler(h)
+    return wrapper
 
 
- at logging_method('streams')
+ at logging_method
 @with_list
 def add_stream_handlers(streams):
 
@@ -135,7 +130,7 @@ def add_stream_handlers(streams):
         yield [out]
 
 
- at logging_method('projects')
+ at logging_method
 @with_list
 def add_project_handlers(projects):
 
@@ -161,6 +156,12 @@ def add_project_handlers(projects):
         yield [validation, report, log, echo, soap]
 
 
+_logging_methods = {
+    'streams': add_stream_handlers,
+    'projects': add_project_handlers,
+}
+
+
 @contextmanager
 def elbe_logging(*args, **kwargs):
     try:
@@ -174,9 +175,7 @@ def open_logging(targets):
 
     close_logging()
 
-    for method in logging_methods:
-        key = method[0]
-        call = method[1]
+    for key, call in _logging_methods.items():
         if key in targets:
             call(targets[key])
 

-- 
2.46.0



More information about the elbe-devel mailing list