use psr3-bridge.
This commit is contained in:
parent
8f97b3c95c
commit
b6ed2b51c3
|
@ -11,7 +11,8 @@
|
||||||
"license": "Apache-2.0",
|
"license": "Apache-2.0",
|
||||||
"require": {
|
"require": {
|
||||||
"laravel/framework": "~5.1|~6.0",
|
"laravel/framework": "~5.1|~6.0",
|
||||||
"casbin/casbin": "~1.0"
|
"casbin/casbin": "~1.0",
|
||||||
|
"casbin/psr3-bridge": "^1.0"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"phpunit/phpunit": "~5.7|~6.0|~7.0|~8.0",
|
"phpunit/phpunit": "~5.7|~6.0|~7.0|~8.0",
|
||||||
|
|
|
@ -39,8 +39,8 @@ return [
|
||||||
// changes whether Lauthz will log messages to the Logger.
|
// changes whether Lauthz will log messages to the Logger.
|
||||||
'enabled' => false,
|
'enabled' => false,
|
||||||
|
|
||||||
// Casbin Logger
|
// Casbin Logger, Supported: \Psr\Log\LoggerInterface|string
|
||||||
'logger' => Lauthz\Logger::class,
|
'logger' => 'log',
|
||||||
],
|
],
|
||||||
|
|
||||||
'cache' => [
|
'cache' => [
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
namespace Lauthz;
|
namespace Lauthz;
|
||||||
|
|
||||||
|
use Casbin\Bridge\Logger\LoggerBridge;
|
||||||
use Casbin\Enforcer;
|
use Casbin\Enforcer;
|
||||||
use Casbin\Model\Model;
|
use Casbin\Model\Model;
|
||||||
use Casbin\Log\Log;
|
use Casbin\Log\Log;
|
||||||
|
@ -77,7 +78,11 @@ class EnforcerManager implements Factory
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($logger = Arr::get($config, 'log.logger')) {
|
if ($logger = Arr::get($config, 'log.logger')) {
|
||||||
Log::setLogger(new $logger($this->app['log']));
|
if (is_string($logger)) {
|
||||||
|
$logger = $this->app->make($logger);
|
||||||
|
}
|
||||||
|
|
||||||
|
Log::setLogger(new LoggerBridge($logger));
|
||||||
}
|
}
|
||||||
|
|
||||||
$model = new Model();
|
$model = new Model();
|
||||||
|
|
|
@ -1,79 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
namespace Lauthz;
|
|
||||||
|
|
||||||
use Casbin\Log\Logger as LoggerContract;
|
|
||||||
use Psr\Log\LoggerInterface;
|
|
||||||
|
|
||||||
class Logger implements LoggerContract
|
|
||||||
{
|
|
||||||
public $enable = false;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var LoggerInterface
|
|
||||||
*/
|
|
||||||
protected $logger;
|
|
||||||
|
|
||||||
public function __construct(LoggerInterface $logger)
|
|
||||||
{
|
|
||||||
$this->logger = $logger;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* controls whether print the message.
|
|
||||||
*
|
|
||||||
* @param bool $enable
|
|
||||||
*/
|
|
||||||
public function enableLog($enable)
|
|
||||||
{
|
|
||||||
$this->enable = $enable;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* returns if logger is enabled.
|
|
||||||
*
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function isEnabled()
|
|
||||||
{
|
|
||||||
return $this->enable;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* formats using the default formats for its operands and logs the message.
|
|
||||||
*
|
|
||||||
* @param mixed ...$v
|
|
||||||
*
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function write(...$v)
|
|
||||||
{
|
|
||||||
if (!$this->enable) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
$content = '';
|
|
||||||
foreach ($v as $value) {
|
|
||||||
if (\is_array($value) || \is_object($value)) {
|
|
||||||
$value = json_encode($value);
|
|
||||||
}
|
|
||||||
$content .= $value;
|
|
||||||
}
|
|
||||||
$this->logger->info($content);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* formats according to a format specifier and logs the message.
|
|
||||||
*
|
|
||||||
* @param $format
|
|
||||||
* @param mixed ...$v
|
|
||||||
*
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function writef($format, ...$v)
|
|
||||||
{
|
|
||||||
if (!$this->enable) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
$this->logger->info(sprintf($format, ...$v));
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,39 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
namespace Lauthz\Tests;
|
|
||||||
|
|
||||||
use Mockery as m;
|
|
||||||
use Monolog\Logger as Monolog;
|
|
||||||
use Lauthz\Logger;
|
|
||||||
|
|
||||||
class LoggerTest extends TestCase
|
|
||||||
{
|
|
||||||
public function testLogger()
|
|
||||||
{
|
|
||||||
if (class_exists(\Illuminate\Log\Logger::class)) {
|
|
||||||
$writer = new \Illuminate\Log\Logger($monolog = m::mock(Monolog::class));
|
|
||||||
} else {
|
|
||||||
$writer = new \Illuminate\Log\Writer($monolog = m::mock(Monolog::class));
|
|
||||||
}
|
|
||||||
|
|
||||||
$logger = new Logger($writer);
|
|
||||||
|
|
||||||
$logger->enableLog(false);
|
|
||||||
$this->assertFalse($logger->isEnabled());
|
|
||||||
|
|
||||||
$logger->enableLog(true);
|
|
||||||
$this->assertTrue($logger->isEnabled());
|
|
||||||
|
|
||||||
$monolog->shouldReceive('info')->once()->with('foo', []);
|
|
||||||
$logger->write('foo');
|
|
||||||
|
|
||||||
$monolog->shouldReceive('info')->once()->with('foo1foo2', []);
|
|
||||||
$logger->write('foo1', 'foo2');
|
|
||||||
|
|
||||||
$monolog->shouldReceive('info')->once()->with(json_encode(['foo1', 'foo2']), []);
|
|
||||||
$logger->write(['foo1', 'foo2']);
|
|
||||||
|
|
||||||
$monolog->shouldReceive('info')->once()->with(sprintf('There are %u million cars in %s.', 2, 'Shanghai'), []);
|
|
||||||
$logger->writef('There are %u million cars in %s.', 2, 'Shanghai');
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue