Logging: Renamed EngelsystemLogger to Logger and added UserAwareLogger
This commit is contained in:
parent
36da807ca2
commit
c519be276a
|
@ -554,7 +554,7 @@ class ImportSchedule extends BaseController
|
|||
SELECT
|
||||
s.SID AS id,
|
||||
s.title,
|
||||
s.start,
|
||||
s.start,
|
||||
s.end,
|
||||
s.shifttype_id AS shift_type_id,
|
||||
s.RID AS room_id,
|
||||
|
@ -606,9 +606,6 @@ class ImportSchedule extends BaseController
|
|||
*/
|
||||
protected function log(string $message, array $context = []): void
|
||||
{
|
||||
$user = auth()->user();
|
||||
$message = sprintf('%s (%u): %s', $user->name, $user->id, $message);
|
||||
|
||||
$this->log->info($message, $context);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,14 +12,7 @@ use Psr\Log\LogLevel;
|
|||
*/
|
||||
function engelsystem_log($message, $level = LogLevel::INFO)
|
||||
{
|
||||
$nick = "Guest";
|
||||
/** @var LoggerInterface $logger */
|
||||
$logger = app('logger');
|
||||
$user = auth()->user();
|
||||
|
||||
if ($user) {
|
||||
$nick = User_Nick_render($user, true);
|
||||
}
|
||||
|
||||
$logger->log($level, '{nick}: {message}', ['nick' => $nick, 'message' => $message]);
|
||||
$logger->log($level, $message);
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@ use Psr\Log\AbstractLogger;
|
|||
use Psr\Log\InvalidArgumentException;
|
||||
use Psr\Log\LogLevel;
|
||||
|
||||
class EngelsystemLogger extends AbstractLogger
|
||||
class Logger extends AbstractLogger
|
||||
{
|
||||
protected $allowedLevels = [
|
||||
LogLevel::ALERT,
|
||||
|
@ -24,8 +24,6 @@ class EngelsystemLogger extends AbstractLogger
|
|||
protected $log;
|
||||
|
||||
/**
|
||||
* EngelsystemLogger constructor.
|
||||
*
|
||||
* @param LogEntry $log
|
||||
*/
|
||||
public function __construct(LogEntry $log)
|
|
@ -3,16 +3,28 @@
|
|||
namespace Engelsystem\Logger;
|
||||
|
||||
use Engelsystem\Container\ServiceProvider;
|
||||
use Engelsystem\Helpers\Authenticator;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class LoggerServiceProvider extends ServiceProvider
|
||||
{
|
||||
public function register()
|
||||
{
|
||||
$logger = $this->app->make(EngelsystemLogger::class);
|
||||
$logger = $this->app->make(UserAwareLogger::class);
|
||||
$this->app->instance('logger', $logger);
|
||||
|
||||
$this->app->bind(LoggerInterface::class, 'logger');
|
||||
$this->app->bind(EngelsystemLogger::class, 'logger');
|
||||
$this->app->bind(Logger::class, 'logger');
|
||||
$this->app->bind(UserAwareLogger::class, 'logger');
|
||||
}
|
||||
|
||||
public function boot()
|
||||
{
|
||||
/** @var UserAwareLogger $logger */
|
||||
$logger = $this->app->get(UserAwareLogger::class);
|
||||
/** @var Authenticator $auth */
|
||||
$auth = $this->app->get(Authenticator::class);
|
||||
|
||||
$logger->setAuth($auth);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,38 @@
|
|||
<?php
|
||||
|
||||
namespace Engelsystem\Logger;
|
||||
|
||||
use Engelsystem\Helpers\Authenticator;
|
||||
use Psr\Log\InvalidArgumentException;
|
||||
|
||||
class UserAwareLogger extends Logger
|
||||
{
|
||||
/** @var Authenticator */
|
||||
protected $auth;
|
||||
|
||||
/**
|
||||
* Logs with an arbitrary level and prepends the user
|
||||
*
|
||||
* @param mixed $level
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
*
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function log($level, $message, array $context = [])
|
||||
{
|
||||
if ($this->auth && ($user = $this->auth->user())) {
|
||||
$message = sprintf('%s (%u): %s', $user->name, $user->id, $message);
|
||||
}
|
||||
|
||||
parent::log($level, $message, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Authenticator $auth
|
||||
*/
|
||||
public function setAuth(Authenticator $auth)
|
||||
{
|
||||
$this->auth = $auth;
|
||||
}
|
||||
}
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace Engelsystem\Test\Feature\Logger;
|
||||
|
||||
use Engelsystem\Logger\EngelsystemLogger;
|
||||
use Engelsystem\Logger\Logger;
|
||||
use Engelsystem\Models\LogEntry;
|
||||
use Engelsystem\Test\Feature\ApplicationFeatureTest;
|
||||
use Psr\Log\InvalidArgumentException;
|
||||
|
@ -10,20 +10,20 @@ use Psr\Log\LoggerInterface;
|
|||
use Psr\Log\LogLevel;
|
||||
use stdClass;
|
||||
|
||||
class EngelsystemLoggerTest extends ApplicationFeatureTest
|
||||
class LoggerTest extends ApplicationFeatureTest
|
||||
{
|
||||
/**
|
||||
* @covers \Engelsystem\Logger\EngelsystemLogger::__construct
|
||||
* @covers \Engelsystem\Logger\Logger::__construct
|
||||
* @return LoggerInterface
|
||||
*/
|
||||
public function getLogger()
|
||||
{
|
||||
$logEntry = new LogEntry();
|
||||
return new EngelsystemLogger($logEntry);
|
||||
return new Logger($logEntry);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \Engelsystem\Logger\EngelsystemLogger::__construct
|
||||
* @covers \Engelsystem\Logger\Logger::__construct
|
||||
*/
|
||||
public function testImplements()
|
||||
{
|
||||
|
@ -48,7 +48,7 @@ class EngelsystemLoggerTest extends ApplicationFeatureTest
|
|||
}
|
||||
|
||||
/**
|
||||
* @covers \Engelsystem\Logger\EngelsystemLogger::log
|
||||
* @covers \Engelsystem\Logger\Logger::log
|
||||
* @dataProvider provideLogLevels
|
||||
* @param string $level
|
||||
*/
|
||||
|
@ -65,7 +65,7 @@ class EngelsystemLoggerTest extends ApplicationFeatureTest
|
|||
}
|
||||
|
||||
/**
|
||||
* @covers \Engelsystem\Logger\EngelsystemLogger::log
|
||||
* @covers \Engelsystem\Logger\Logger::log
|
||||
*/
|
||||
public function testContextReplacement()
|
||||
{
|
||||
|
@ -93,8 +93,8 @@ class EngelsystemLoggerTest extends ApplicationFeatureTest
|
|||
}
|
||||
|
||||
/**
|
||||
* @covers \Engelsystem\Logger\EngelsystemLogger::interpolate
|
||||
* @covers \Engelsystem\Logger\EngelsystemLogger::log
|
||||
* @covers \Engelsystem\Logger\Logger::interpolate
|
||||
* @covers \Engelsystem\Logger\Logger::log
|
||||
* @dataProvider provideContextReplaceValues
|
||||
*
|
||||
* @param string $message
|
||||
|
@ -111,7 +111,7 @@ class EngelsystemLoggerTest extends ApplicationFeatureTest
|
|||
}
|
||||
|
||||
/**
|
||||
* @covers \Engelsystem\Logger\EngelsystemLogger::log
|
||||
* @covers \Engelsystem\Logger\Logger::log
|
||||
*/
|
||||
public function testContextToString()
|
||||
{
|
||||
|
@ -133,8 +133,8 @@ class EngelsystemLoggerTest extends ApplicationFeatureTest
|
|||
}
|
||||
|
||||
/**
|
||||
* @covers \Engelsystem\Logger\EngelsystemLogger::checkLevel
|
||||
* @covers \Engelsystem\Logger\EngelsystemLogger::log
|
||||
* @covers \Engelsystem\Logger\Logger::checkLevel
|
||||
* @covers \Engelsystem\Logger\Logger::log
|
||||
*/
|
||||
public function testThrowExceptionOnInvalidLevel()
|
||||
{
|
|
@ -2,7 +2,9 @@
|
|||
|
||||
namespace Engelsystem\Test\Unit\Logger;
|
||||
|
||||
use Engelsystem\Logger\EngelsystemLogger;
|
||||
use Engelsystem\Helpers\Authenticator;
|
||||
use Engelsystem\Logger\Logger;
|
||||
use Engelsystem\Logger\UserAwareLogger;
|
||||
use Engelsystem\Logger\LoggerServiceProvider;
|
||||
use Engelsystem\Test\Unit\ServiceProviderTest;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
|
@ -11,28 +13,41 @@ use Psr\Log\LoggerInterface;
|
|||
class LoggerServiceProviderTest extends ServiceProviderTest
|
||||
{
|
||||
/**
|
||||
* @covers \Engelsystem\Logger\LoggerServiceProvider::register()
|
||||
* @covers \Engelsystem\Logger\LoggerServiceProvider::register
|
||||
*/
|
||||
public function testRegister()
|
||||
{
|
||||
/** @var EngelsystemLogger|MockObject $logger */
|
||||
$logger = $this->getMockBuilder(EngelsystemLogger::class)
|
||||
$serviceProvider = new LoggerServiceProvider($this->app);
|
||||
$serviceProvider->register();
|
||||
|
||||
$this->assertInstanceOf(UserAwareLogger::class, $this->app->get('logger'));
|
||||
$this->assertInstanceOf(UserAwareLogger::class, $this->app->get(LoggerInterface::class));
|
||||
$this->assertInstanceOf(UserAwareLogger::class, $this->app->get(Logger::class));
|
||||
$this->assertInstanceOf(UserAwareLogger::class, $this->app->get(UserAwareLogger::class));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \Engelsystem\Logger\LoggerServiceProvider::boot
|
||||
*/
|
||||
public function testBoot()
|
||||
{
|
||||
/** @var Authenticator|MockObject $auth */
|
||||
$auth = $this->getMockBuilder(Authenticator::class)
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
/** @var UserAwareLogger|MockObject $log */
|
||||
$log = $this->getMockBuilder(UserAwareLogger::class)
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
|
||||
$app = $this->getApp(['make', 'instance', 'bind']);
|
||||
$this->app->instance(Authenticator::class, $auth);
|
||||
$this->app->instance(UserAwareLogger::class, $log);
|
||||
|
||||
$this->setExpects($app, 'make', [EngelsystemLogger::class], $logger);
|
||||
$this->setExpects($app, 'instance', ['logger', $logger]);
|
||||
$log->expects($this->once())
|
||||
->method('setAuth')
|
||||
->with($auth);
|
||||
|
||||
$app->expects($this->atLeastOnce())
|
||||
->method('bind')
|
||||
->withConsecutive(
|
||||
[LoggerInterface::class, 'logger'],
|
||||
[EngelsystemLogger::class, 'logger']
|
||||
);
|
||||
|
||||
$serviceProvider = new LoggerServiceProvider($app);
|
||||
$serviceProvider->register();
|
||||
$serviceProvider = new LoggerServiceProvider($this->app);
|
||||
$serviceProvider->boot();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,18 +2,18 @@
|
|||
|
||||
namespace Engelsystem\Test\Unit\Logger;
|
||||
|
||||
use Engelsystem\Logger\EngelsystemLogger;
|
||||
use Engelsystem\Logger\Logger;
|
||||
use Engelsystem\Models\LogEntry;
|
||||
use Engelsystem\Test\Unit\ServiceProviderTest;
|
||||
use InvalidArgumentException;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use Psr\Log\LogLevel;
|
||||
|
||||
class EngelsystemLoggerTest extends ServiceProviderTest
|
||||
class LoggerTest extends ServiceProviderTest
|
||||
{
|
||||
/**
|
||||
* @covers \Engelsystem\Logger\EngelsystemLogger::__construct
|
||||
* @covers \Engelsystem\Logger\EngelsystemLogger::log
|
||||
* @covers \Engelsystem\Logger\Logger::__construct
|
||||
* @covers \Engelsystem\Logger\Logger::log
|
||||
*/
|
||||
public function testLog()
|
||||
{
|
||||
|
@ -25,27 +25,27 @@ class EngelsystemLoggerTest extends ServiceProviderTest
|
|||
->method('create')
|
||||
->with(['level' => LogLevel::INFO, 'message' => 'I\'m an information!']);
|
||||
|
||||
$logger = new EngelsystemLogger($logEntry);
|
||||
$logger = new Logger($logEntry);
|
||||
|
||||
$logger->log(LogLevel::INFO, 'I\'m an information!');
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \Engelsystem\Logger\EngelsystemLogger::log
|
||||
* @covers \Engelsystem\Logger\EngelsystemLogger::checkLevel
|
||||
* @covers \Engelsystem\Logger\Logger::log
|
||||
* @covers \Engelsystem\Logger\Logger::checkLevel
|
||||
*/
|
||||
public function testCheckLevel()
|
||||
{
|
||||
/** @var LogEntry|MockObject $logEntry */
|
||||
$logEntry = $this->createMock(LogEntry::class);
|
||||
$logger = new EngelsystemLogger($logEntry);
|
||||
$logger = new Logger($logEntry);
|
||||
|
||||
$this->expectException(InvalidArgumentException::class);
|
||||
$logger->log('FooBar', 'Random Stuff');
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \Engelsystem\Logger\EngelsystemLogger::interpolate
|
||||
* @covers \Engelsystem\Logger\Logger::interpolate
|
||||
*/
|
||||
public function testInterpolate()
|
||||
{
|
||||
|
@ -61,7 +61,7 @@ class EngelsystemLoggerTest extends ServiceProviderTest
|
|||
[['level' => LogLevel::NOTICE, 'message' => 'User: Bar']]
|
||||
);
|
||||
|
||||
$logger = new EngelsystemLogger($logEntry);
|
||||
$logger = new Logger($logEntry);
|
||||
|
||||
$logger->log(LogLevel::DEBUG, 'User: {user}', ['user' => 'Foo']);
|
||||
$logger->log(LogLevel::NOTICE, 'User: {user}', ['user' => ['name' => 'Lorem']]);
|
|
@ -0,0 +1,49 @@
|
|||
<?php
|
||||
|
||||
namespace Engelsystem\Test\Unit\Logger;
|
||||
|
||||
use Engelsystem\Helpers\Authenticator;
|
||||
use Engelsystem\Logger\UserAwareLogger;
|
||||
use Engelsystem\Models\LogEntry;
|
||||
use Engelsystem\Models\User\User;
|
||||
use Engelsystem\Test\Unit\ServiceProviderTest;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use Psr\Log\LogLevel;
|
||||
|
||||
class UserAwareLoggerTest extends ServiceProviderTest
|
||||
{
|
||||
/**
|
||||
* @covers \Engelsystem\Logger\UserAwareLogger::log
|
||||
* @covers \Engelsystem\Logger\UserAwareLogger::setAuth
|
||||
*/
|
||||
public function testLog()
|
||||
{
|
||||
$user = (new User())->forceFill(['id' => 1, 'name' => 'admin']);
|
||||
|
||||
/** @var LogEntry|MockObject $logEntry */
|
||||
$logEntry = $this->getMockBuilder(LogEntry::class)
|
||||
->addMethods(['create'])
|
||||
->getMock();
|
||||
$logEntry->expects($this->exactly(2))
|
||||
->method('create')
|
||||
->withConsecutive(
|
||||
[['level' => LogLevel::INFO, 'message' => 'Some more informational foo']],
|
||||
[['level' => LogLevel::INFO, 'message' => 'admin (1): Some even more informational bar']]
|
||||
);
|
||||
|
||||
/** @var Authenticator|MockObject $auth */
|
||||
$auth = $this->createMock(Authenticator::class);
|
||||
$auth->expects($this->exactly(2))
|
||||
->method('user')
|
||||
->willReturnOnConsecutiveCalls(
|
||||
null,
|
||||
$user
|
||||
);
|
||||
|
||||
$logger = new UserAwareLogger($logEntry);
|
||||
$logger->setAuth($auth);
|
||||
|
||||
$logger->log(LogLevel::INFO, 'Some more informational foo');
|
||||
$logger->log(LogLevel::INFO, 'Some even more informational bar');
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue