Refactor removing log handlers

This commit is contained in:
James R. Barlow
2021-04-13 13:16:21 -07:00
parent a90b9e669f
commit 8f8aaa93ed
3 changed files with 11 additions and 4 deletions
+2 -3
View File
@@ -29,6 +29,7 @@ from tqdm import tqdm
from ocrmypdf import Executor, hookimpl
from ocrmypdf._logging import TqdmConsole
from ocrmypdf.exceptions import InputFileError
from ocrmypdf.helpers import remove_all_log_handlers
Queue = Union[multiprocessing.Queue, queue.Queue]
@@ -74,9 +75,7 @@ def process_init(q: Queue, user_init: Callable[[], None], loglevel):
# Remove any log handlers that belong to the parent process
root = logging.getLogger()
for handler in root.handlers[:]:
root.removeHandler(handler)
handler.close() # To ensure handlers with opened resources are released
remove_all_log_handlers(root)
# Set up our single log handler to forward messages to the parent
root.setLevel(loglevel)
+2 -1
View File
@@ -22,6 +22,7 @@ from unittest.mock import Mock
from ocrmypdf import Executor, hookimpl
from ocrmypdf._concurrent import NullProgressBar
from ocrmypdf.exceptions import InputFileError
from ocrmypdf.helpers import remove_all_log_handlers
class MessageType(Enum):
@@ -61,8 +62,8 @@ def process_loop(
# Reconfigure the root logger for this process to send all messages to a queue
h = ConnectionLogHandler(conn)
root = logging.getLogger()
remove_all_log_handlers(root)
root.setLevel(loglevel)
root.handlers = []
root.addHandler(h)
user_init()
+7
View File
@@ -223,6 +223,13 @@ def clamp(n, smallest, largest): # mypy doesn't understand types for this
return max(smallest, min(n, largest))
def remove_all_log_handlers(logger):
"Remove all log handlers, usually used in a child process."
for handler in logger.handlers[:]:
logger.removeHandler(handler)
handler.close() # To ensure handlers with opened resources are released
def pikepdf_enable_mmap():
# try:
# if pikepdf._qpdf.set_access_default_mmap(True):