Skip to content

Commit f39ace2

Browse files
authored
feat: add "logging_config" for PyCasbin logging (#376)
1 parent 28886db commit f39ace2

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ production-ready | production-ready | beta-test | production-ready
6262
- [Role manager](#role-manager)
6363
- [Async Enforcer](#async-enforcer)
6464
- [Benchmarks](#benchmarks)
65+
- [Logging](#logging)
6566
- [Examples](#examples)
6667
- [Middlewares](#middlewares)
6768
- [Our adopters](#our-adopters)
@@ -291,6 +292,10 @@ asyncio.run(main())
291292

292293
https://casbin.org/docs/benchmark
293294

295+
## Logging
296+
297+
pycasbin leverages the default Python logging mechanism. The pycasbin package makes a call to `logging.getLogger()` to set the logger. No special logging configuration is needed other than initializing the logger in the parent application. If no logging is initialized within the parent application, you will not see any log messages from pycasbin. At the same time, When you enable log in pycasbin, you can specify the logging configuration through the parameter `logging_config`. If no configuration is specified, it will use the [default log configuration](https://github.com/casbin/pycasbin/blob/c33cabfa0ac65cd09cf812a65e71794d64cb5132/casbin/util/log.py#L6C1-L6C1). For other pycasbin extensions, you can refer to the [Django logging docs](https://docs.djangoproject.com/en/4.2/topics/logging/) if you are a Django user. For other Python users, you should refer to the [Python logging docs](https://docs.python.org/3/library/logging.config.html) to configure the logger.
298+
294299
## Examples
295300

296301
Model | Model file | Policy file

casbin/core_enforcer.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class CoreEnforcer:
5555
auto_build_role_links = False
5656
auto_notify_watcher = False
5757

58-
def __init__(self, model=None, adapter=None, enable_log=False):
58+
def __init__(self, model=None, adapter=None, enable_log=False, logging_config: dict = None):
5959
self.logger = logging.getLogger("casbin.enforcer")
6060
if isinstance(model, str):
6161
if isinstance(adapter, str):
@@ -70,7 +70,7 @@ def __init__(self, model=None, adapter=None, enable_log=False):
7070
self.init_with_model_and_adapter(model, adapter)
7171

7272
if enable_log:
73-
configure_logging()
73+
configure_logging(logging_config)
7474
else:
7575
disabled_logging()
7676

casbin/fast_enforcer.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,16 @@
1010
class FastEnforcer(Enforcer):
1111
_cache_key_order: Sequence[int] = None
1212

13-
def __init__(self, model=None, adapter=None, enable_log=False, cache_key_order: Sequence[int] = None):
13+
def __init__(
14+
self,
15+
model=None,
16+
adapter=None,
17+
enable_log=False,
18+
logging_config: dict = None,
19+
cache_key_order: Sequence[int] = None,
20+
):
1421
self._cache_key_order = cache_key_order
15-
super().__init__(model, adapter, enable_log)
22+
super().__init__(model, adapter, enable_log, logging_config)
1623

1724
def new_model(self, path="", text=""):
1825
"""creates a model."""

0 commit comments

Comments
 (0)