Skip to content

Commit

Permalink
feat: Casbin Logger, Supported: \Psr\Log\LoggerInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
Tinywan committed Nov 1, 2024
1 parent 98e06ba commit 2ada6ff
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
23 changes: 18 additions & 5 deletions src/Permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@

use Casbin\Enforcer;
use Casbin\Exceptions\CasbinException;
use Casbin\Log\Logger\DefaultLogger;
use Casbin\Model\Model;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Psr\Log\LoggerInterface;
use support\Container;
use Casbin\WebmanPermission\Watcher\RedisWatcher;

Expand Down Expand Up @@ -67,10 +71,11 @@ class Permission
protected static array $_manager = [];

/**
* @param string|null $driver
* @desc driver
* @param string|null $driver
* @return Enforcer
* @throws CasbinException
* @author Lyt8384
* @author Tinywan(ShaoBo Wan)
*/
public static function driver(?string $driver = null): Enforcer
{
Expand All @@ -87,7 +92,15 @@ public static function driver(?string $driver = null): Enforcer
} elseif ('text' == $config['model']['config_type']) {
$model->loadModel($config['model']['config_text']);
}
static::$_manager[$driver] = new Enforcer($model, Container::make($config['adapter'], [$driver]), false);
$logConfig = self::getConfig('log');
$logger = null;
if (true === $logConfig['enabled']) {
/** @var LoggerInterface $casbinLogger 创建一个 Monolog 日志记录器 */
$casbinLogger = new Logger($logConfig['logger']);
$casbinLogger->pushHandler(new StreamHandler($logConfig['path'], Logger::DEBUG));
$logger = new DefaultLogger($casbinLogger);
}
static::$_manager[$driver] = new Enforcer($model, Container::make($config['adapter'], [$driver]), $logger, $logConfig['enabled']);

$watcher = new RedisWatcher(config('redis.default'), $driver);
static::$_manager[$driver]->setWatcher($watcher);
Expand All @@ -112,7 +125,7 @@ public static function getAllDriver(): array
* @return mixed
* @author Tinywan(ShaoBo Wan)
*/
public static function getDefaultDriver()
public static function getDefaultDriver(): mixed
{
return self::getConfig('default');
}
Expand All @@ -124,7 +137,7 @@ public static function getDefaultDriver()
* @return mixed
* @author Tinywan(ShaoBo Wan)
*/
public static function getConfig(string $name = null, $default = null)
public static function getConfig(string $name = null, $default = null): mixed
{
if (!is_null($name)) {
return config('plugin.casbin.webman-permission.permission.' . $name, $default);
Expand Down
10 changes: 8 additions & 2 deletions src/config/plugin/casbin/webman-permission/permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
*/
return [
'default' => 'basic',
// 默认配置
/** 日志配置 */
'log' => [
'enabled' => true, // changes will log messages to the Logger.
'logger' => 'Casbin', // Casbin Logger, Supported: \Psr\Log\LoggerInterface|string
'path' => runtime_path() . '/logs/casbin.log' // log path
],
/** 默认配置 */
'basic' => [
// 策略模型Model设置
'model' => [
Expand All @@ -24,7 +30,7 @@
'rules_name' => null
],
],
// 其他扩展配置,只需要按照基础配置一样,复制一份,指定相关策略模型和适配器即可
/** 其他扩展配置,只需要按照基础配置一样,复制一份,指定相关策略模型和适配器即可 */
'restful' => [
'model' => [
'config_type' => 'file',
Expand Down

0 comments on commit 2ada6ff

Please sign in to comment.