Skip to content

Commit 0197256

Browse files
committed
Fixed: Worker Canonical Logs not Output to CloudWatch
Adds the `canonical: True` flag to worker log output. The watchtower handler filters log messages based on this flag, and does not send them to cloudwatch if it is not present. This change also changes the log pattern for worker logs, updating them to better take advantage of the JSON log pattern already used across the API.
1 parent cce8d1b commit 0197256

File tree

1 file changed

+9
-16
lines changed

1 file changed

+9
-16
lines changed

src/mavedb/lib/logging/canonical.py

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import logging
2-
import json
32
from typing import Any, Optional
43

54
from arq import ArqRedis
@@ -27,8 +26,7 @@ async def log_job(ctx: dict) -> None:
2726
result = await completed_job.result_info()
2827

2928
if not result:
30-
log_context["message"] = f"Job finished, but could not retrieve a job result for job {job_id}."
31-
logger.warning(json.dumps(log_context))
29+
logger.warning(msg=f"Job finished, but could not retrieve a job result for job {job_id}.", extra=log_context)
3230
log_context.pop("message")
3331
else:
3432
log_context = {
@@ -51,23 +49,18 @@ async def log_job(ctx: dict) -> None:
5149
"version": __version__,
5250
"log_type": LogType.worker_job,
5351
"source": Source.worker,
52+
"canonical": True,
5453
},
5554
}
5655

5756
if result is None:
58-
log_context["message"] = "Job result could not be found."
59-
logger.error(json.dumps(log_context))
57+
logger.error(msg="Job result could not be found.", extra=log_context)
6058
elif result.result == "success":
61-
log_context["message"] = "Job completed successfully."
62-
logger.info(json.dumps(log_context))
59+
logger.info(msg="Job completed successfully.", extra=log_context)
6360
elif result.result != "success":
64-
log_context["message"] = "Job completed with handled exception."
65-
logger.warning(json.dumps(log_context))
61+
logger.warning(msg="Job completed with handled exception.", extra=log_context)
6662
else:
67-
log_context["message"] = "Job completed with unhandled exception."
68-
logger.error(json.dumps(log_context))
69-
70-
log_context.pop("message")
63+
logger.error(msg="Job completed with unhandled exception.", extra=log_context)
7164

7265

7366
def log_request(request: Request, response: Response, end: int) -> None:
@@ -79,8 +72,8 @@ def log_request(request: Request, response: Response, end: int) -> None:
7972

8073
save_to_logging_context({"canonical": True})
8174
if response.status_code < 400:
82-
logger.info(msg="Request completed.", extra={**logging_context(), "canonical": True})
75+
logger.info(msg="Request completed.", extra=logging_context())
8376
elif response.status_code < 500:
84-
logger.warning(msg="Request completed.", extra={**logging_context(), "canonical": True})
77+
logger.warning(msg="Request completed.", extra=logging_context())
8578
else:
86-
logger.error(msg="Request completed.", extra={**logging_context(), "canonical": True})
79+
logger.error(msg="Request completed.", extra=logging_context())

0 commit comments

Comments
 (0)