loguru icon indicating copy to clipboard operation
loguru copied to clipboard

Can't enqueue=True be used with multiple gunicorns?

Open liuqijie6 opened this issue 1 year ago • 1 comments

When I set enqueue=True, my gunicron cannot start.

logger.add(log_path,
                   rotation=CONFIG.get('Log', 'rotation'),
                   encoding="utf-8",
                   enqueue=True,
                   retention=CONFIG.get('Log', 'retention'),
                   # compression=CONFIG.get('Log', 'compression'),
                   format=CONFIG.get("Log", "format"),
                   level="DEBUG",
                   # diagnose=True,
                   serialize=True
                   )

gunicron.conf.py

from config import CONFIG
from config.log_info import PORT

host, port = CONFIG.get("web_server", "host"), CONFIG.get("web_server", "port")
bind = f"{host}:{port}"
PORT["port"] = port
cpu_count = multiprocessing.cpu_count()
daemon = False
worker_class = 'eventlet'  # 'gevent' sync  eventlet
workers = cpu_count
threads = cpu_count if worker_class == "sync" else 1
capture_output = True  # 打印存入error日志
accesslog = "-"

supervisord.log

2024-05-04 22:42:17,957 INFO success: CheckIn2 entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2024-05-04 22:43:46,437 INFO waiting for CheckIn to stop
2024-05-04 22:43:48,437 INFO waiting for CheckIn to stop
2024-05-04 22:43:50,437 INFO waiting for CheckIn to stop
2024-05-04 22:43:52,437 INFO waiting for CheckIn to stop
2024-05-04 22:43:54,437 INFO waiting for CheckIn to stop
2024-05-04 22:43:56,437 WARN killing 'CheckIn' (33945) with SIGKILL
2024-05-04 22:43:56,437 INFO waiting for CheckIn to stop
2024-05-04 22:43:56,441 INFO stopped: CheckIn (terminated by SIGKILL)
2024-05-04 22:43:56,443 INFO waiting for CheckIn2 to stop
2024-05-04 22:43:58,443 INFO waiting for CheckIn2 to stop
2024-05-04 22:44:00,443 INFO waiting for CheckIn2 to stop
2024-05-04 22:44:02,443 INFO waiting for CheckIn2 to stop
2024-05-04 22:44:04,443 INFO waiting for CheckIn2 to stop
2024-05-04 22:44:06,442 WARN killing 'CheckIn2' (33967) with SIGKILL
2024-05-04 22:44:06,443 INFO waiting for CheckIn2 to stop
2024-05-04 22:44:06,446 INFO stopped: CheckIn2 (terminated by SIGKILL)
2024-05-04 22:46:42,368 INFO spawned: 'CheckIn' with pid 37788
2024-05-04 22:46:43,724 INFO success: CheckIn entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2024-05-04 22:49:45,086 INFO waiting for CheckIn to stop
2024-05-04 22:49:47,086 INFO waiting for CheckIn to stop
2024-05-04 22:49:49,086 INFO waiting for CheckIn to stop
2024-05-04 22:49:51,086 INFO waiting for CheckIn to stop
2024-05-04 22:49:53,086 INFO waiting for CheckIn to stop
2024-05-04 22:49:55,086 WARN killing 'CheckIn' (37788) with SIGKILL
2024-05-04 22:49:55,087 INFO waiting for CheckIn to stop
2024-05-04 22:49:55,089 INFO stopped: CheckIn (terminated by SIGKILL)
2024-05-04 22:49:55,093 INFO spawned: 'CheckIn' with pid 40555
2024-05-04 22:49:56,456 INFO success: CheckIn entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2024-05-04 22:49:56,458 INFO spawned: 'CheckIn2' with pid 40592
2024-05-04 22:49:57,835 INFO success: CheckIn2 entered RUNNING state, process has stayed up for > than 1 seconds (startsecs

What I want to solve is to run multiple gunincorns, and then the log output can be in the same file, and the log order is not lost. 截屏2024-05-04 23 00 07 The difference between them is that the ports are different。

Can you help me?

liuqijie6 avatar May 04 '24 15:05 liuqijie6

The enqueue=True does not interoperate well with Gunicorn workers. This is because the workers does not inherit the logger from the parent process.

For now, until a better solution is implemented, it's advised to use one different file per worker.

Delgan avatar May 09 '24 12:05 Delgan

Actually, Loguru and Gunicorn should work fine together. The key point is to make sure that the logger is configured exactly once and in the master process (before the workers are started). This should be possible using the on_starting server hook.

See this comment for a complete example: https://github.com/Delgan/loguru/issues/1111#issuecomment-3357834497

Delgan avatar Oct 01 '25 19:10 Delgan