Skip to content

Commit 28dc2d2

Browse files
authored
Merge pull request #10 from basakest/psr3-bridge
feat: support Casbin Logger interface
2 parents 2c70393 + bc6c1ef commit 28dc2d2

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

src/Casbin.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Casbin\Model\Model;
99
use Casbin\Persist\Adapter;
1010
use EasySwoole\Permission\Adapters\DatabaseAdapter;
11+
use Casbin\Log\Log;
1112

1213
/**
1314
* Class Casbin
@@ -119,6 +120,13 @@ public function __construct(?Config $config = null)
119120
$this->adapter = new DatabaseAdapter();
120121
}
121122

123+
if ($logger = $this->config->getLoggerClass() ?? 'EasySwoole\Permission\Logger') {
124+
if (class_exists($logger)) {
125+
$logger = $logger::getInstance();
126+
}
127+
Log::setLogger($logger);
128+
}
129+
122130
$this->model = new Model();
123131

124132
if (Config::CONFIG_TYPE_FILE === $config->getModelConfigType()) {

src/Config.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ class Config
3737
*/
3838
protected $log_enable = false;
3939

40+
/**
41+
* @var string
42+
*/
43+
protected $logger_class = 'EasySwoole\Permission\Logger';
44+
4045
/**
4146
* @return string
4247
*/
@@ -116,4 +121,14 @@ public function setLogEnable(bool $log_enable): void
116121
{
117122
$this->log_enable = $log_enable;
118123
}
124+
125+
/**
126+
* Get the logger class
127+
*
128+
* @return string
129+
*/
130+
public function getLoggerClass()
131+
{
132+
return $this->logger_class;
133+
}
119134
}

src/Logger.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
namespace EasySwoole\Permission;
4+
5+
use Casbin\Log\Logger as CasbinLogger;
6+
use EasySwoole\Log\Logger as ESLogger;
7+
use EasySwoole\Component\Singleton;
8+
9+
class Logger extends ESLogger implements CasbinLogger
10+
{
11+
use Singleton;
12+
13+
/**
14+
* DefaultLogger is the implementation for a Logger using golang log.
15+
*
16+
* @var bool
17+
*/
18+
public $enable = false;
19+
20+
/**
21+
* controls whether print the message.
22+
*
23+
* @param bool $enable
24+
*/
25+
public function enableLog(bool $enable): void
26+
{
27+
$this->enable = $enable;
28+
}
29+
30+
/**
31+
* returns if logger is enabled.
32+
*
33+
* @return bool
34+
*/
35+
public function isEnabled(): bool
36+
{
37+
return $this->enable;
38+
}
39+
40+
/**
41+
* formats using the default formats for its operands and logs the message.
42+
*
43+
* @param mixed ...$v
44+
*/
45+
public function write(...$v): void
46+
{
47+
if ($this->enable) {
48+
$content = '';
49+
foreach ($v as $value) {
50+
if (\is_array($value)) {
51+
$value = json_encode($value);
52+
} elseif (\is_object($value)) {
53+
$value = json_encode($value);
54+
}
55+
$content .= $value;
56+
}
57+
$content .= PHP_EOL;
58+
$this->log($content);
59+
}
60+
}
61+
62+
/**
63+
* formats according to a format specifier and logs the message.
64+
*
65+
* @param string $format
66+
* @param mixed ...$v
67+
*/
68+
public function writef(string $format, ...$v): void
69+
{
70+
$content = '';
71+
$content .= sprintf($format, ...$v);
72+
$content .= PHP_EOL;
73+
$this->log($content);
74+
}
75+
}

0 commit comments

Comments
 (0)