|
16 | 16 | """ |
17 | 17 |
|
18 | 18 | import logging |
| 19 | +from copy import deepcopy |
19 | 20 | from uuid import uuid4 |
20 | 21 |
|
21 | 22 | from casbin import SyncedEnforcer |
| 23 | +from casbin.util.log import DEFAULT_LOGGING, configure_logging |
22 | 24 | from casbin_adapter.enforcer import initialize_enforcer |
23 | 25 | from django.conf import settings |
24 | 26 |
|
@@ -154,6 +156,19 @@ def configure_enforcer_auto_save_and_load(cls): |
154 | 156 |
|
155 | 157 | cls.configure_enforcer_auto_save(auto_save_policy) |
156 | 158 |
|
| 159 | + @classmethod |
| 160 | + def _configure_logging(cls) -> None: |
| 161 | + """Configure logging levels for Casbin's internal loggers. |
| 162 | +
|
| 163 | + This controls the verbosity of Casbin's policy evaluation and role management |
| 164 | + logging. The log level defaults to WARNING if CASBIN_LOG_LEVEL is not set. |
| 165 | + """ |
| 166 | + log_level = getattr(settings, "CASBIN_LOG_LEVEL", "WARNING") |
| 167 | + casbin_logging = deepcopy(DEFAULT_LOGGING) |
| 168 | + for logger_name in casbin_logging["loggers"]: |
| 169 | + casbin_logging["loggers"][logger_name]["level"] = log_level |
| 170 | + configure_logging(casbin_logging) |
| 171 | + |
157 | 172 | @classmethod |
158 | 173 | def load_policy_if_needed(cls): |
159 | 174 | """Load policy if the last load version indicates it's needed. |
@@ -246,6 +261,10 @@ def _initialize_enforcer(cls) -> SyncedEnforcer: |
246 | 261 | # Avoid circular import |
247 | 262 | from openedx_authz.engine.matcher import is_admin_or_superuser_check # pylint: disable=import-outside-toplevel |
248 | 263 |
|
| 264 | + # Configure logging BEFORE initialize_enforcer() because it creates a ProxyEnforcer |
| 265 | + # that doesn't pass logging config and would use Casbin's defaults |
| 266 | + cls._configure_logging() |
| 267 | + |
249 | 268 | db_alias = getattr(settings, "CASBIN_DB_ALIAS", "default") |
250 | 269 |
|
251 | 270 | try: |
|
0 commit comments