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>
This commit is contained in:
tocariimaa
2024-09-20 15:32:23 +00:00
committed by GitHub
parent 37dba853c9
commit d51d88700b

View File

@@ -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.
##