Skip to content

Commit 49875a6

Browse files
committed
fix: log configuring when running under spawn and add --log-format
Fixed issue with logging configuration when using multiprocessing 'spawn' method on macOS. The spawn method reimports all modules which caused logging configuration to be lost. Added `--log-format` argument to easily customize logging format across all processes, including spawned child processes.
1 parent 0c4c3a1 commit 49875a6

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

taskiq/cli/worker/args.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class WorkerArgs:
3030
fs_discover: bool = False
3131
configure_logging: bool = True
3232
log_level: LogLevel = LogLevel.INFO
33+
log_format: str = "[%(asctime)s][%(name)s][%(levelname)-7s][%(processName)s] %(message)s"
3334
workers: int = 2
3435
max_threadpool_threads: Optional[int] = None
3536
max_process_pool_processes: Optional[int] = None
@@ -118,6 +119,11 @@ def from_cli(
118119
choices=[level.name for level in LogLevel],
119120
help="worker log level",
120121
)
122+
parser.add_argument(
123+
"--log-format",
124+
default="[%(asctime)s][%(name)s][%(levelname)-7s][%(processName)s] %(message)s",
125+
help="worker log format",
126+
)
121127
parser.add_argument(
122128
"--workers",
123129
"-w",

taskiq/cli/worker/run.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import signal
66
import sys
77
from concurrent.futures import Executor, ProcessPoolExecutor, ThreadPoolExecutor
8-
from multiprocessing import set_start_method
8+
from multiprocessing import set_start_method, get_start_method
99
from sys import platform
1010
from typing import Any, Optional, Type
1111

@@ -86,7 +86,11 @@ def start_listen(args: WorkerArgs) -> None:
8686
"""
8787
shutdown_event = asyncio.Event()
8888
hardkill_counter = 0
89-
89+
if args.configure_logging and get_start_method() == "spawn":
90+
logging.basicConfig(
91+
level=logging.getLevelName(args.log_level),
92+
format=args.log_format,
93+
)
9094
def interrupt_handler(signum: int, _frame: Any) -> None:
9195
"""
9296
Signal handler.
@@ -186,8 +190,7 @@ def run_worker(args: WorkerArgs) -> Optional[int]:
186190
if args.configure_logging:
187191
logging.basicConfig(
188192
level=logging.getLevelName(args.log_level),
189-
format="[%(asctime)s][%(name)s][%(levelname)-7s]"
190-
"[%(processName)s] %(message)s",
193+
format=args.log_format,
191194
)
192195
logging.getLogger("taskiq").setLevel(level=logging.getLevelName(args.log_level))
193196
logging.getLogger("watchdog.observers.inotify_buffer").setLevel(level=logging.INFO)

0 commit comments

Comments
 (0)