Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
fix: celery loggers being used outside Celery (#16330)
Mikaile91211de0254
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yamlindex 90b0d5ab0c..131a22eb78 100644--- a/.pre-commit-config.yaml+++ b/.pre-commit-config.yaml@@ -46,9 +46,6 @@ repos:- --error# Do not check for version updates, it slows down the execution.- --disable-version-check- # Do not append directory names to the rules, otherwise- # it may misbehave.- - --no-rewrite-rule-ids# Quiet mode in order to not pollute the pre-commit outputs.# Replace --quiet with --verbose to debug issues.- --quietdiff --git a/.semgrep/Makefile b/.semgrep/Makefileindex 81b5a80c0d..cfa702ebc6 100644--- a/.semgrep/Makefile+++ b/.semgrep/Makefile@@ -3,9 +3,8 @@ test:scan:semgrep scan \- --metrics=off \ # Disable Telemetry+ --metrics=off \--disable-version-check \- --no-rewrite-rule-ids \ # Don't put directory name in the rule IDs- --error \ # Exit with error code 1 if there are finding- -c rules/ \+ --error \+ -c . \../diff --git a/.semgrep/correctness/celery/task-logger-without-suffix.py b/.semgrep/correctness/celery/task-logger-without-suffix.pynew file mode 100644index 0000000000..23be5dc69f--- /dev/null+++ b/.semgrep/correctness/celery/task-logger-without-suffix.py@@ -0,0 +1,39 @@+import logging+from celery.utils.log import get_task_logger++def same_name_using___name__():+ # Using the same name (__name__) should match.+ # ruleid: task-logger-without-suffix+ logger = logging.getLogger(__name__)+ task_logger = get_task_logger(__name__)++def same_name_with_celery_first():+ # Using the same name (__name__) should match,+ # even if get_task_logger() is called before logging.getLogger().+ # ruleid: task-logger-without-suffix+ task_logger = get_task_logger(__name__)+ logger = logging.getLogger(__name__)++def different_name_get_task_logger():+ # Using a different name in `get_task_logger()` shouldn't match.+ # ok: task-logger-without-suffix+ task_logger = get_task_logger(f"{__name__}.celery")++def different_name_logging_getlogger():+ # Using a different name in logging.getLogger shouldn't match.+ # ok: task-logger-without-suffix+ logger = logging.getLogger("foo")+ task_logger = get_task_logger(__name__)++def same_name_hardcoded():+ # Using the same name as a string literal should match.+ # ruleid: task-logger-without-suffix+ logger = logging.getLogger("foo")+ task_logger = get_task_logger("foo")++def same_name_without_variables():+ # Using the same name without creating variables should match.+ # ruleid: task-logger-without-suffix+ logging.getLogger("foo")+ get_task_logger("foo")+diff --git a/.semgrep/correctness/celery/task-logger-without-suffix.yaml b/.semgrep/correctness/celery/task-logger-without-suffix.yamlnew file mode 100644index 0000000000..feaf56c113--- /dev/null+++ b/.semgrep/correctness/celery/task-logger-without-suffix.yaml@@ -0,0 +1,22 @@+rules:+ - id: task-logger-without-suffix+ message: >-+ Wrapping the module's logger with Celery can lead to unexpected behaviors when called outside Celery, such as crashes or missing custom logging formatters (e.g., HTTP request log formatter).+ Because of that, the task logger should be using its own specific namespace, such as f"{__name__}.celery".+ metadata:+ category: correctness+ technology:+ - celery+ languages:+ - python+ severity: ERROR+ pattern-either:+ - pattern: |+ logging.getLogger($X, ...)+ ...+ celery.utils.log.get_task_logger($X, ...)+ - pattern: |+ celery.utils.log.get_task_logger($X, ...)+ ...+ logging.getLogger($X, ...)+ focus-metavariable: $Xdiff --git a/.semgrep/no-use-requests-lib.fixed.py b/.semgrep/security/requests/no-use-requests-lib.fixed.pysimilarity index 100%rename from .semgrep/no-use-requests-lib.fixed.pyrename to .semgrep/security/requests/no-use-requests-lib.fixed.pydiff --git a/.semgrep/no-use-requests-lib.py b/.semgrep/security/requests/no-use-requests-lib.pysimilarity index 100%rename from .semgrep/no-use-requests-lib.pyrename to .semgrep/security/requests/no-use-requests-lib.pydiff --git a/.semgrep/no-use-requests-lib.yaml b/.semgrep/security/requests/no-use-requests-lib.yamlsimilarity index 100%rename from .semgrep/no-use-requests-lib.yamlrename to .semgrep/security/requests/no-use-requests-lib.yamldiff --git a/saleor/app/installation_utils.py b/saleor/app/installation_utils.pyindex e60cd9489a..9085b2f8bb 100644--- a/saleor/app/installation_utils.py+++ b/saleor/app/installation_utils.py@@ -33,7 +33,7 @@ from .types import AppExtensionTarget, AppTypeMAX_ICON_FILE_SIZE = 1024 * 1024 * 10 # 10MBlogger = logging.getLogger(__name__)-task_logger = get_task_logger(__name__)+task_logger = get_task_logger(f"{__name__}.celery")class AppInstallationError(HTTPError):diff --git a/saleor/product/tasks.py b/saleor/product/tasks.pyindex 6513a82d2c..42f38dd0c2 100644--- a/saleor/product/tasks.py+++ b/saleor/product/tasks.py@@ -30,7 +30,7 @@ from .utils.variants import ()logger = logging.getLogger(__name__)-task_logger = get_task_logger(__name__)+task_logger = get_task_logger(f"{__name__}.celery")PRODUCTS_BATCH_SIZE = 300diff --git a/saleor/webhook/transport/asynchronous/transport.py b/saleor/webhook/transport/asynchronous/transport.pyindex 16c793720a..680a0c11e1 100644--- a/saleor/webhook/transport/asynchronous/transport.py+++ b/saleor/webhook/transport/asynchronous/transport.py@@ -42,7 +42,7 @@ if TYPE_CHECKING:logger = logging.getLogger(__name__)-task_logger = get_task_logger(__name__)+task_logger = get_task_logger(f"{__name__}.celery")OBSERVABILITY_QUEUE_NAME = "observability"diff --git a/saleor/webhook/transport/synchronous/transport.py b/saleor/webhook/transport/synchronous/transport.pyindex 0623e65b70..ca0293d5c7 100644--- a/saleor/webhook/transport/synchronous/transport.py+++ b/saleor/webhook/transport/synchronous/transport.py@@ -4,7 +4,6 @@ from json import JSONDecodeErrorfrom typing import TYPE_CHECKING, Any, Callable, Optional, TypeVarfrom urllib.parse import urlparse-from celery.utils.log import get_task_loggerfrom django.conf import settingsfrom django.core.cache import cache@@ -49,7 +48,6 @@ R = TypeVar("R")logger = logging.getLogger(__name__)-task_logger = get_task_logger(__name__)@app.task(diff --git a/saleor/webhook/transport/utils.py b/saleor/webhook/transport/utils.pyindex fec27c33ae..27f4dc7d7d 100644--- a/saleor/webhook/transport/utils.py+++ b/saleor/webhook/transport/utils.py@@ -53,7 +53,7 @@ from ..models import Webhookfrom . import signature_for_payloadlogger = logging.getLogger(__name__)-task_logger = get_task_logger(__name__)+task_logger = get_task_logger(f"{__name__}.celery")DEFAULT_TAX_CODE = "UNMAPPED"