Skip to content

Commit

Permalink
feat: support Casbin Logger interface
Browse files Browse the repository at this point in the history
  • Loading branch information
basakest committed Mar 29, 2021
1 parent 83893e6 commit 25c5883
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 115 deletions.
3 changes: 1 addition & 2 deletions composer.json
Expand Up @@ -13,8 +13,7 @@
"ext-swoole": ">=4.5",
"casbin/casbin": "^2.2",
"easyswoole/orm": "^1.4",
"easyswoole/easyswoole": "~3.3|~3.4",
"casbin/psr3-bridge": "^1.1"
"easyswoole/easyswoole": "~3.3|~3.4"
},
"require-dev": {
"easyswoole/easyswoole": "~3.3|~3.4",
Expand Down
3 changes: 0 additions & 3 deletions src/Casbin.php
Expand Up @@ -8,8 +8,6 @@
use Casbin\Model\Model;
use Casbin\Persist\Adapter;
use EasySwoole\Permission\Adapters\DatabaseAdapter;
use Casbin\Bridge\Logger\LoggerBridge;
use Casbin\Log\Log;

/**
* Class Casbin
Expand Down Expand Up @@ -122,7 +120,6 @@ public function __construct(?Config $config = null)
}

$logger = Logger::getInstance();
Log::setLogger(new LoggerBridge($logger));

$this->model = new Model();

Expand Down
147 changes: 37 additions & 110 deletions src/Logger.php
Expand Up @@ -2,147 +2,74 @@

namespace EasySwoole\Permission;

use Psr\Log\LoggerInterface;
use Casbin\Log\Logger as CasbinLogger;
use EasySwoole\Log\Logger as ESLogger;
use EasySwoole\Component\Singleton;

class Logger implements LoggerInterface
class Logger extends ESLogger implements CasbinLogger
{
use Singleton;

public function __construct()
{
$this->logger = new ESLogger();
}

/**
* System is unusable.
*
* @param string $message
* @param mixed[] $context
*
* @return void
*/
public function emergency($message, array $context = array())
{

}

/**
* Action must be taken immediately.
*
* Example: Entire website down, database unavailable, etc. This should
* trigger the SMS alerts and wake you up.
*
* @param string $message
* @param mixed[] $context
* DefaultLogger is the implementation for a Logger using golang log.
*
* @return void
* @var bool
*/
public function alert($message, array $context = array())
{

}
public $enable = false;

/**
* Critical conditions.
*
* Example: Application component unavailable, unexpected exception.
* controls whether print the message.
*
* @param string $message
* @param mixed[] $context
*
* @return void
* @param bool $enable
*/
public function critical($message, array $context = array())
public function enableLog(bool $enable): void
{

$this->enable = $enable;
}

/**
* Runtime errors that do not require immediate action but should typically
* be logged and monitored.
*
* @param string $message
* @param mixed[] $context
* returns if logger is enabled.
*
* @return void
* @return bool
*/
public function error($message, array $context = array())
public function isEnabled(): bool
{

return $this->enable;
}

/**
* Exceptional occurrences that are not errors.
*
* Example: Use of deprecated APIs, poor use of an API, undesirable things
* that are not necessarily wrong.
*
* @param string $message
* @param mixed[] $context
* formats using the default formats for its operands and logs the message.
*
* @return void
* @param mixed ...$v
*/
public function warning($message, array $context = array())
public function write(...$v): void
{

if ($this->enable) {
$content = '';
foreach ($v as $value) {
if (\is_array($value)) {
$value = json_encode($value);
} elseif (\is_object($value)) {
$value = json_encode($value);
}
$content .= $value;
}
$content .= PHP_EOL;
$this->log($content);
}
}

/**
* Normal but significant events.
*
* @param string $message
* @param mixed[] $context
*
* @return void
*/
public function notice($message, array $context = array())
{

}

/**
* Interesting events.
*
* Example: User logs in, SQL logs.
*
* @param string $message
* @param mixed[] $context
*
* @return void
*/
public function info($message, array $context = array())
{

}

/**
* Detailed debug information.
*
* @param string $message
* @param mixed[] $context
*
* @return void
*/
public function debug($message, array $context = array())
{

}

/**
* Logs with an arbitrary level.
*
* @param mixed $level
* @param string $message
* @param mixed[] $context
*
* @return void
* formats according to a format specifier and logs the message.
*
* @throws \Psr\Log\InvalidArgumentException
* @param string $format
* @param mixed ...$v
*/
public function log($level, $message, array $context = array())
public function writef(string $format, ...$v): void
{
return $this->logger->log($message, $level, $context['category'] = 'debug');
$content = '';
$content .= sprintf($format, ...$v);
$content .= PHP_EOL;
$this->log($content);
}
}

0 comments on commit 25c5883

Please sign in to comment.