Skip to content

Commit faddfc3

Browse files
ivolnistovIlya Volnistov
authored andcommitted
fix: log configuring when running under spawn and add --log-format (#435)
* 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. * fix linting and formatting --------- Co-authored-by: Ilya Volnistov <i.volnistov@gaijin.team>
1 parent 0ea005f commit faddfc3

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

taskiq/cli/worker/args.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ class WorkerArgs:
3030
fs_discover: bool = False
3131
configure_logging: bool = True
3232
log_level: LogLevel = LogLevel.INFO
33+
log_format: str = (
34+
"[%(asctime)s][%(name)s][%(levelname)-7s][%(processName)s] %(message)s"
35+
)
3336
workers: int = 2
3437
max_threadpool_threads: Optional[int] = None
3538
max_process_pool_processes: Optional[int] = None
@@ -118,6 +121,12 @@ def from_cli(
118121
choices=[level.name for level in LogLevel],
119122
help="worker log level",
120123
)
124+
parser.add_argument(
125+
"--log-format",
126+
default="[%(asctime)s][%(name)s][%(levelname)-7s]"
127+
"[%(processName)s] %(message)s",
128+
help="worker log format",
129+
)
121130
parser.add_argument(
122131
"--workers",
123132
"-w",

taskiq/cli/worker/run.py

Lines changed: 7 additions & 3 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 get_start_method, set_start_method
99
from sys import platform
1010
from typing import Any, Optional, Type
1111

@@ -86,6 +86,11 @@ def start_listen(args: WorkerArgs) -> None:
8686
"""
8787
shutdown_event = asyncio.Event()
8888
hardkill_counter = 0
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+
)
8994

9095
def interrupt_handler(signum: int, _frame: Any) -> None:
9196
"""
@@ -186,8 +191,7 @@ def run_worker(args: WorkerArgs) -> Optional[int]:
186191
if args.configure_logging:
187192
logging.basicConfig(
188193
level=logging.getLevelName(args.log_level),
189-
format="[%(asctime)s][%(name)s][%(levelname)-7s]"
190-
"[%(processName)s] %(message)s",
194+
format=args.log_format,
191195
)
192196
logging.getLogger("taskiq").setLevel(level=logging.getLevelName(args.log_level))
193197
logging.getLogger("watchdog.observers.inotify_buffer").setLevel(level=logging.INFO)

0 commit comments

Comments
 (0)