Format preview worker logs like the main process, tagged with worker pid
Worker stderr is now inherited by the parent, so its log lines land in the server log — but they arrived with the default logging format and a noisy "preview-worker config=..." line at every spawn. - Extract the emoji level-prefix formatting from cista.sanic_logging into cista.util.logformat, which has no Sanic dependency (the worker must not import Sanic: import-time prints could corrupt the stdout protocol). - Worker configures its stderr handler with the same emoji prefixes plus a worker[pid] tag, and the config-loaded info message is removed.
This commit is contained in:
+17
-3
@@ -17,6 +17,7 @@ import gc
|
||||
import io
|
||||
import logging
|
||||
import mimetypes
|
||||
import os
|
||||
import shlex
|
||||
import struct
|
||||
import subprocess
|
||||
@@ -33,9 +34,19 @@ import pyvips
|
||||
from blake3 import blake3
|
||||
|
||||
from cista import config
|
||||
from cista.util.logformat import format_level_prefix
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class _WorkerLogFormatter(logging.Formatter):
|
||||
"""Emoji level prefix like the main process, tagged with the worker pid."""
|
||||
|
||||
def format(self, record: logging.LogRecord) -> str:
|
||||
prefix = format_level_prefix(record.levelno)
|
||||
return f"{prefix}worker[{os.getpid()}]: {record.getMessage()}"
|
||||
|
||||
|
||||
AVIF_FAST_EFFORT = 0
|
||||
|
||||
DOC_PREVIEW_SUFFIXES = {".pdf", ".xps", ".epub", ".mobi"}
|
||||
@@ -554,11 +565,14 @@ def _run_loop() -> None:
|
||||
|
||||
|
||||
def main() -> None:
|
||||
# Configure all log output to stderr before any imports that may emit logs.
|
||||
logging.basicConfig(stream=sys.stderr, level=logging.INFO)
|
||||
# Configure all log output to stderr before any imports that may emit
|
||||
# logs. stderr is inherited by the parent, so this lands in the server
|
||||
# log, formatted like the main process and tagged with the worker pid.
|
||||
handler = logging.StreamHandler(sys.stderr)
|
||||
handler.setFormatter(_WorkerLogFormatter())
|
||||
logging.basicConfig(level=logging.INFO, handlers=[handler])
|
||||
try:
|
||||
config.load_config()
|
||||
logger.info("preview-worker config=%s", config.conffile)
|
||||
except Exception:
|
||||
logger.exception("preview-worker failed to load config at startup")
|
||||
if len(sys.argv) > 1:
|
||||
|
||||
Reference in New Issue
Block a user