Logging: Renamed EngelsystemLogger to Logger and added UserAwareLogger
parent
36da807ca2
commit
c519be276a
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -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