Skip to content

Commit

Permalink
Add user_id to the request object, so it can be logged
Browse files Browse the repository at this point in the history
  • Loading branch information
ilijastuden committed Feb 20, 2021
1 parent fb77e27 commit e8795a2
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 32 deletions.
10 changes: 7 additions & 3 deletions src/AppRequest/AppRequestInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@

namespace ActiveCollab\Logger\AppRequest;

/**
* @package Angie\AppRequest
*/
interface AppRequestInterface
{
/**
Expand All @@ -30,6 +27,13 @@ public function getSessionId();
*/
public function getRequestId();

/**
* Return user ID.
*
* @return string
*/
public function getUserId();

/**
* Return summary arguments.
*
Expand Down
11 changes: 8 additions & 3 deletions src/AppRequest/CliRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@

use RuntimeException;

/**
* @package Angie\AppRequest
*/
class CliRequest implements AppRequestInterface
{
/**
Expand Down Expand Up @@ -93,6 +90,14 @@ public function getRequestId()
return "$this->command/$this->timestamp";
}

/**
* {@inheritdoc}
*/
public function getUserId()
{
return '';
}

/**
* {@inheritdoc}
*/
Expand Down
20 changes: 16 additions & 4 deletions src/AppRequest/HttpRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@

use Psr\Http\Message\ServerRequestInterface;

/**
* @package Angie\AppRequest
*/
class HttpRequest implements AppRequestInterface
{
/**
Expand Down Expand Up @@ -43,6 +40,11 @@ class HttpRequest implements AppRequestInterface
*/
private $request_id;

/**
* @var string
*/
private $user_id;

/**
* @param ServerRequestInterface $request
*/
Expand All @@ -54,6 +56,7 @@ public function __construct(ServerRequestInterface $request)
$this->method = $request->getMethod();
$this->session_id = (string) $request->getAttribute('session_id');
$this->request_id = (string) $request->getAttribute('request_id');
$this->user_id = (string) $request->getAttribute('user_id');
}

/**
Expand Down Expand Up @@ -91,12 +94,21 @@ public function getRequestId()
return $this->request_id;
}

public function getUserId()
{
return $this->user_id;
}

/**
* {@inheritdoc}
*/
public function getSummaryArguments()
{
return ['uri' => $this->uri_path, 'method' => $this->method, 'query_string' => $this->query_string];
return [
'uri' => $this->uri_path,
'method' => $this->method,
'query_string' => $this->query_string
];
}

/**
Expand Down
3 changes: 0 additions & 3 deletions src/AppResponse/AppResponseInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@

namespace ActiveCollab\Logger\AppResponse;

/**
* @package Angie\AppResponse
*/
interface AppResponseInterface
{
/**
Expand Down
3 changes: 0 additions & 3 deletions src/AppResponse/HttpResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@

use Psr\Http\Message\ResponseInterface;

/**
* @package Angie\AppResponse
*/
class HttpResponse implements AppResponseInterface
{
/**
Expand Down
4 changes: 4 additions & 0 deletions src/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ public function &setAppRequest(AppRequestInterface $request)
$this->request_arguments['request_id'] = $request->getRequestId();
}

if (!empty($request->getUserId())) {
$this->request_arguments['user_id'] = $request->getUserId();
}

$this->flushBuffer();

return $this;
Expand Down
11 changes: 1 addition & 10 deletions test/src/LoggerShutdownTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,16 @@

namespace ActiveCollab\Logger\Test;

use ActiveCollab\Logger\AppRequest\CliRequest;
use ActiveCollab\Logger\AppRequest\HttpRequest;
use ActiveCollab\Logger\AppResponse\HttpResponse;
use ActiveCollab\Logger\Factory\Factory;
use ActiveCollab\Logger\Factory\FactoryInterface;
use ActiveCollab\Logger\LoggerInterface;
use ActiveCollab\Logger\Test\Base\TestCase;
use Laminas\Diactoros\Response;
use Laminas\Diactoros\ServerRequest;

/**
* @package angie.tests
*/
class LoggerShutdownTest extends TestCase
{
public function testLogicExceptionOnMultiShutdownCalls()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage("Buffer flush function should be registered only once");
$this->expectExceptionMessage('Buffer flush function should be registered only once');

$logger = (new Factory())->create('Active Collab', '1.0.0', 'development', LoggerInterface::LOG_FOR_DEBUG, LoggerInterface::BLACKHOLE);

Expand Down
6 changes: 0 additions & 6 deletions test/src/LoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,13 @@
use Laminas\Diactoros\Response;
use Laminas\Diactoros\ServerRequest;

/**
* @package angie.tests
*/
class LoggerTest extends TestCase
{
/**
* @var LoggerInterface
*/
private $logger;

/**
* {@inheritdoc}
*/
public function setUp(): void
{
parent::setUp();
Expand Down

0 comments on commit e8795a2

Please sign in to comment.