diff --git a/README.md b/README.md index fccb6c52..8645ea7c 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,7 @@ production-ready | production-ready | beta-test | production-ready - [Role manager](#role-manager) - [Async Enforcer](#async-enforcer) - [Benchmarks](#benchmarks) +- [Logging](#logging) - [Examples](#examples) - [Middlewares](#middlewares) - [Our adopters](#our-adopters) @@ -291,6 +292,10 @@ asyncio.run(main()) https://casbin.org/docs/benchmark +## Logging + +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. + ## Examples Model | Model file | Policy file diff --git a/casbin/core_enforcer.py b/casbin/core_enforcer.py index 21e97646..0800fb22 100644 --- a/casbin/core_enforcer.py +++ b/casbin/core_enforcer.py @@ -55,7 +55,7 @@ class CoreEnforcer: auto_build_role_links = False auto_notify_watcher = False - def __init__(self, model=None, adapter=None, enable_log=False): + def __init__(self, model=None, adapter=None, enable_log=False, logging_config: dict = None): self.logger = logging.getLogger("casbin.enforcer") if isinstance(model, str): if isinstance(adapter, str): @@ -70,7 +70,7 @@ def __init__(self, model=None, adapter=None, enable_log=False): self.init_with_model_and_adapter(model, adapter) if enable_log: - configure_logging() + configure_logging(logging_config) else: disabled_logging() diff --git a/casbin/fast_enforcer.py b/casbin/fast_enforcer.py index fd000ddb..000fcaf6 100644 --- a/casbin/fast_enforcer.py +++ b/casbin/fast_enforcer.py @@ -10,9 +10,16 @@ class FastEnforcer(Enforcer): _cache_key_order: Sequence[int] = None - def __init__(self, model=None, adapter=None, enable_log=False, cache_key_order: Sequence[int] = None): + def __init__( + self, + model=None, + adapter=None, + enable_log=False, + logging_config: dict = None, + cache_key_order: Sequence[int] = None, + ): self._cache_key_order = cache_key_order - super().__init__(model, adapter, enable_log) + super().__init__(model, adapter, enable_log, logging_config) def new_model(self, path="", text=""): """creates a model."""