Skip to content

Commit feccbc2

Browse files
authored
feat: avoid expensive str() calls in enforce_ex when logging is disabled (#421)
1 parent feccd90 commit feccbc2

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

casbin/core_enforcer.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -508,17 +508,18 @@ def enforce_ex(self, *rvals):
508508
result = effect_to_bool(final_effect)
509509

510510
# Log request.
511-
512-
req_str = "Request: "
513-
req_str = req_str + ", ".join([str(v) for v in rvals])
514-
515-
req_str = req_str + " ---> %s" % result
516-
if result:
517-
self.logger.info(req_str)
518-
else:
519-
# leaving this in warning for now, if it's very noise this can be changed to info or debug,
520-
# or change the log level
521-
self.logger.warning(req_str)
511+
if (result and self.logger.isEnabledFor(logging.INFO)) or (
512+
not result and self.logger.isEnabledFor(logging.WARNING)
513+
):
514+
req_str = "Request: "
515+
req_str = req_str + ", ".join([str(v) for v in rvals])
516+
req_str = req_str + " ---> %s" % result
517+
if result:
518+
self.logger.info(req_str)
519+
else:
520+
# leaving this in warning for now, if it's very noise this can be changed to info or debug,
521+
# or change the log level
522+
self.logger.warning(req_str)
522523

523524
explain_rule = []
524525
if explain_index != -1 and explain_index < policy_len:

0 commit comments

Comments
 (0)