Remove a previously added handler and stop sending logs to its sink. Parameters ---------- handler_id : |int| or ``None`` The id of the sink to remove, as it was returned by the |add| method. If ``None``, all handlers are removed. The pre-configured h
(self, handler_id=None)
| 1018 | return handler_id |
| 1019 | |
| 1020 | def remove(self, handler_id=None): |
| 1021 | """Remove a previously added handler and stop sending logs to its sink. |
| 1022 | |
| 1023 | Parameters |
| 1024 | ---------- |
| 1025 | handler_id : |int| or ``None`` |
| 1026 | The id of the sink to remove, as it was returned by the |add| method. If ``None``, all |
| 1027 | handlers are removed. The pre-configured handler is guaranteed to have the index ``0``. |
| 1028 | |
| 1029 | Raises |
| 1030 | ------ |
| 1031 | ValueError |
| 1032 | If ``handler_id`` is not ``None`` but there is no active handler with such id. |
| 1033 | |
| 1034 | Examples |
| 1035 | -------- |
| 1036 | >>> i = logger.add(sys.stderr, format="{message}") |
| 1037 | >>> logger.info("Logging") |
| 1038 | Logging |
| 1039 | >>> logger.remove(i) |
| 1040 | >>> logger.info("No longer logging") |
| 1041 | """ |
| 1042 | if not (handler_id is None or isinstance(handler_id, int)): |
| 1043 | raise TypeError( |
| 1044 | "Invalid handler id, it should be an integer as returned " |
| 1045 | "by the 'add()' method (or None), not: '%s'" % type(handler_id).__name__ |
| 1046 | ) |
| 1047 | |
| 1048 | with self._core.lock: |
| 1049 | if handler_id is not None and handler_id not in self._core.handlers: |
| 1050 | raise ValueError("There is no existing handler with id %d" % handler_id) from None |
| 1051 | |
| 1052 | if handler_id is None: |
| 1053 | handler_ids = list(self._core.handlers) |
| 1054 | else: |
| 1055 | handler_ids = [handler_id] |
| 1056 | |
| 1057 | for handler_id in handler_ids: |
| 1058 | handlers = self._core.handlers.copy() |
| 1059 | handler = handlers.pop(handler_id) |
| 1060 | |
| 1061 | # This needs to be done first in case "stop()" raises an exception |
| 1062 | levelnos = (h.levelno for h in handlers.values()) |
| 1063 | self._core.min_level = min(levelnos, default=float("inf")) |
| 1064 | self._core.handlers = handlers |
| 1065 | |
| 1066 | handler.stop() |
| 1067 | |
| 1068 | def complete(self): |
| 1069 | """Wait for the end of enqueued messages and asynchronous tasks scheduled by handlers. |