summary refs log tree commit diff stats
diff options
context:
space:
mode:
authortocariimaa <87833010+tocariimaa@users.noreply.github.com>2024-09-20 15:32:23 +0000
committerGitHub <noreply@github.com>2024-09-20 17:32:23 +0200
commitd51d88700b2fb3bd228d5e8f7385e2e4a2e2880c (patch)
treec331a98defbdb4b4f1f1e3a4975f9caa410d182e
parent37dba853c9e2c41b445f1bbb7cfcf195773e6eee (diff)
downloadNim-d51d88700b2fb3bd228d5e8f7385e2e4a2e2880c.tar.gz
Implement `removeHandler` in std/logging module (fixes #23757) (#24143)
Since the module allows for a handler to be added multiple times, for
the sake of consistency, `removeHandler` only removes the first found
instance of the handler in the `handlers` seq. So for n calls of
`addHandler` using the same handler, n calls of `removeHandler` are
required.

fixes #23757

---------

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
-rw-r--r--lib/pure/logging.nim11
1 files changed, 11 insertions, 0 deletions
diff --git a/lib/pure/logging.nim b/lib/pure/logging.nim
index e3f0240a2..c30f68af8 100644
--- a/lib/pure/logging.nim
+++ b/lib/pure/logging.nim
@@ -839,6 +839,7 @@ proc addHandler*(handler: Logger) =
   ##   each of those threads.
   ##
   ## See also:
+  ## * `removeHandler proc`_
   ## * `getHandlers proc<#getHandlers>`_
   runnableExamples:
     var logger = newConsoleLogger()
@@ -846,6 +847,16 @@ proc addHandler*(handler: Logger) =
     doAssert logger in getHandlers()
   handlers.add(handler)
 
+proc removeHandler*(handler: Logger) =
+  ## Removes a logger from the list of registered handlers.
+  ##
+  ## Note that for n times a logger is registered, n calls to this proc
+  ## are required to remove that logger.
+  for i, hnd in handlers:
+    if hnd == handler:
+      handlers.delete(i)
+      return
+
 proc getHandlers*(): seq[Logger] =
   ## Returns a list of all the registered handlers.
   ##