Skip to content

Commit 2ada6ff

Browse files
committed
feat: Casbin Logger, Supported: \Psr\Log\LoggerInterface
1 parent 98e06ba commit 2ada6ff

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

src/Permission.php

+18-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@
1313

1414
use Casbin\Enforcer;
1515
use Casbin\Exceptions\CasbinException;
16+
use Casbin\Log\Logger\DefaultLogger;
1617
use Casbin\Model\Model;
18+
use Monolog\Handler\StreamHandler;
19+
use Monolog\Logger;
20+
use Psr\Log\LoggerInterface;
1721
use support\Container;
1822
use Casbin\WebmanPermission\Watcher\RedisWatcher;
1923

@@ -67,10 +71,11 @@ class Permission
6771
protected static array $_manager = [];
6872

6973
/**
70-
* @param string|null $driver
74+
* @desc driver
75+
* @param string|null $driver
7176
* @return Enforcer
7277
* @throws CasbinException
73-
* @author Lyt8384
78+
* @author Tinywan(ShaoBo Wan)
7479
*/
7580
public static function driver(?string $driver = null): Enforcer
7681
{
@@ -87,7 +92,15 @@ public static function driver(?string $driver = null): Enforcer
8792
} elseif ('text' == $config['model']['config_type']) {
8893
$model->loadModel($config['model']['config_text']);
8994
}
90-
static::$_manager[$driver] = new Enforcer($model, Container::make($config['adapter'], [$driver]), false);
95+
$logConfig = self::getConfig('log');
96+
$logger = null;
97+
if (true === $logConfig['enabled']) {
98+
/** @var LoggerInterface $casbinLogger 创建一个 Monolog 日志记录器 */
99+
$casbinLogger = new Logger($logConfig['logger']);
100+
$casbinLogger->pushHandler(new StreamHandler($logConfig['path'], Logger::DEBUG));
101+
$logger = new DefaultLogger($casbinLogger);
102+
}
103+
static::$_manager[$driver] = new Enforcer($model, Container::make($config['adapter'], [$driver]), $logger, $logConfig['enabled']);
91104

92105
$watcher = new RedisWatcher(config('redis.default'), $driver);
93106
static::$_manager[$driver]->setWatcher($watcher);
@@ -112,7 +125,7 @@ public static function getAllDriver(): array
112125
* @return mixed
113126
* @author Tinywan(ShaoBo Wan)
114127
*/
115-
public static function getDefaultDriver()
128+
public static function getDefaultDriver(): mixed
116129
{
117130
return self::getConfig('default');
118131
}
@@ -124,7 +137,7 @@ public static function getDefaultDriver()
124137
* @return mixed
125138
* @author Tinywan(ShaoBo Wan)
126139
*/
127-
public static function getConfig(string $name = null, $default = null)
140+
public static function getConfig(string $name = null, $default = null): mixed
128141
{
129142
if (!is_null($name)) {
130143
return config('plugin.casbin.webman-permission.permission.' . $name, $default);

src/config/plugin/casbin/webman-permission/permission.php

+8-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@
66
*/
77
return [
88
'default' => 'basic',
9-
// 默认配置
9+
/** 日志配置 */
10+
'log' => [
11+
'enabled' => true, // changes will log messages to the Logger.
12+
'logger' => 'Casbin', // Casbin Logger, Supported: \Psr\Log\LoggerInterface|string
13+
'path' => runtime_path() . '/logs/casbin.log' // log path
14+
],
15+
/** 默认配置 */
1016
'basic' => [
1117
// 策略模型Model设置
1218
'model' => [
@@ -24,7 +30,7 @@
2430
'rules_name' => null
2531
],
2632
],
27-
// 其他扩展配置,只需要按照基础配置一样,复制一份,指定相关策略模型和适配器即可
33+
/** 其他扩展配置,只需要按照基础配置一样,复制一份,指定相关策略模型和适配器即可 */
2834
'restful' => [
2935
'model' => [
3036
'config_type' => 'file',

0 commit comments

Comments
 (0)