Skip to content

Commit

Permalink
Merge pull request #54 from cego/lejo/use-logger-facade
Browse files Browse the repository at this point in the history
Use logger facade
  • Loading branch information
LauJosefsen authored May 16, 2024
2 parents 6c5e28c + db523a3 commit 30f71dc
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 40 deletions.
6 changes: 3 additions & 3 deletions src/RequestLog/Data/RequestLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Cego\RequestLog\Data;

use Throwable;
use Psr\Log\LoggerInterface;
use Illuminate\Support\Facades\Log;

class RequestLog
{
Expand All @@ -26,7 +26,7 @@ public function __construct(
) {
}

public function log(LoggerInterface $logger)
public function log()
{
$context = [
'http' => [
Expand Down Expand Up @@ -73,7 +73,7 @@ public function log(LoggerInterface $logger)
];
}

$logger->debug(
Log::debug(
sprintf('Timing for %s', $this->url),
$context
);
Expand Down
2 changes: 1 addition & 1 deletion src/RequestLog/Middleware/LogRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ private function logRequest(Request $request, Response $response): void
responseBody: $this->truncate($response->getContent() ?: '{}', $truncateBodyLength),
responseException: $response->exception ?? null,
executionTimeNs: $executionTimeNs
))->log(Log::getLogger());
))->log();

} catch (Throwable $throwable) {
Log::error($throwable);
Expand Down
72 changes: 36 additions & 36 deletions tests/Unit/LogRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Tests\Unit;

use Monolog\Logger;
use Tests\TestCase;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
Expand Down Expand Up @@ -35,12 +34,11 @@ public function test_request_body_is_always_empty_when_not_json()
$methods = ['get', 'post', 'put', 'patch', 'delete'];

foreach ($methods as $method) {
$loggerMock = $this->createMock(Logger::class);

Log::partialMock()->shouldReceive('getLogger')->once()->withAnyArgs()->andReturn($loggerMock);
$loggerMock = Log::partialMock();
Log::setApplication($this->app);

// Assert debug was called on loggerMock once with {} request body
$loggerMock->expects($this->once())->method('debug')->with($this->stringStartsWith('Timing for'))->willReturnCallback(function ($message, $context) {
$loggerMock->shouldReceive('debug')->once()->andReturnUsing(function ($message, $context) {
$this->assertEquals('{}', $context['http']['request']['body']['content']);
});

Expand All @@ -51,11 +49,11 @@ public function test_request_body_is_always_empty_when_not_json()
public function test_it_masks_request_headers()
{
// Arrange
$loggerMock = $this->createMock(Logger::class);
Log::partialMock()->shouldReceive('getLogger')->once()->withAnyArgs()->andReturn($loggerMock);
$loggerMock = Log::partialMock();
Log::setApplication($this->app);

// Assert debug was called on loggerMock once with {} request body
$loggerMock->expects($this->once())->method('debug')->with($this->stringStartsWith('Timing for'))->willReturnCallback(function ($message, $context) {
$loggerMock->shouldReceive('debug')->once()->andReturnUsing(function ($message, $context) {
$loggedHeaders = $context['http']['request']['headers.raw'];
$loggedHeaders = json_decode($loggedHeaders, true);

Expand All @@ -76,11 +74,11 @@ public function test_it_masks_request_headers()
public function test_it_masks_duplicate_request_headers()
{
// Arrange
$loggerMock = $this->createMock(Logger::class);
Log::partialMock()->shouldReceive('getLogger')->once()->withAnyArgs()->andReturn($loggerMock);
$loggerMock = Log::partialMock();
Log::setApplication($this->app);

// Assert debug was called on loggerMock once with {} request body
$loggerMock->expects($this->once())->method('debug')->with($this->stringStartsWith('Timing for'))->willReturnCallback(function ($message, $context) {
$loggerMock->shouldReceive('debug')->once()->andReturnUsing(function ($message, $context) {
$loggedHeaders = $context['http']['request']['headers.raw'];
$loggedHeaders = json_decode($loggedHeaders, true);
$this->assertEquals('[ MASKED ]', $loggedHeaders['x-encrypt-this-header'][0]);
Expand All @@ -101,11 +99,11 @@ public function test_it_masks_duplicate_request_headers()
public function test_it_masks_request_body()
{
// Arrange
$loggerMock = $this->createMock(Logger::class);
Log::partialMock()->shouldReceive('getLogger')->once()->withAnyArgs()->andReturn($loggerMock);
$loggerMock = Log::partialMock();
Log::setApplication($this->app);

// Assert debug was called on loggerMock once with {} request body
$loggerMock->expects($this->once())->method('debug')->with($this->stringStartsWith('Timing for'))->willReturnCallback(function ($message, $context) {
$loggerMock->shouldReceive('debug')->once()->andReturnUsing(function ($message, $context) {
$loggedBody = json_decode($context['http']['request']['body']['content'], true);
$this->assertEquals([
'password' => '[ MASKED ]',
Expand Down Expand Up @@ -155,11 +153,11 @@ public function test_it_masks_request_body()
public function test_it_tests()
{
// Arrange
$loggerMock = $this->createMock(Logger::class);
Log::partialMock()->shouldReceive('getLogger')->once()->withAnyArgs()->andReturn($loggerMock);
$loggerMock = Log::partialMock();
Log::setApplication($this->app);

// Assert debug was called on loggerMock once with {} request body
$loggerMock->expects($this->once())->method('debug')->with($this->stringStartsWith('Timing for'))->willReturnCallback(function ($message, $context) {
$loggerMock->shouldReceive('debug')->once()->andReturnUsing(function ($message, $context) {
$loggedHeaders = $context['http']['request']['headers.raw'];
$loggedHeaders = json_decode($loggedHeaders, true);
$this->assertEquals('{"token":"[ MASKED ]","cake":"not-secret"}', $context['http']['request']['query_string']);
Expand All @@ -174,11 +172,10 @@ public function test_it_tests()
public function test_it_masks_request_cookies()
{
// Arrange
$loggerMock = $this->createMock(Logger::class);
Log::partialMock()->shouldReceive('getLogger')->once()->withAnyArgs()->andReturn($loggerMock);
$loggerMock = Log::partialMock();
Log::setApplication($this->app);

// Assert debug was called on loggerMock once with {} request body
$loggerMock->expects($this->once())->method('debug')->with($this->stringStartsWith('Timing for'))->willReturnCallback(function ($message, $context) {
$loggerMock->shouldReceive('debug')->once()->andReturnUsing(function ($message, $context) {
$loggedCookies = $context['http']['request']['cookies.raw'];
$loggedCookies = json_decode($loggedCookies, true);
$this->assertEquals('[ MASKED ]', $loggedCookies['SECRET_COOKIE']);
Expand All @@ -196,11 +193,11 @@ public function test_it_masks_request_cookies()
public function test_it_masks_response_cookies(): void
{
// Arrange
$loggerMock = $this->createMock(Logger::class);
Log::partialMock()->shouldReceive('getLogger')->once()->withAnyArgs()->andReturn($loggerMock);
$loggerMock = Log::partialMock();
Log::setApplication($this->app);

// Assert debug was called on loggerMock once with {} request body
$loggerMock->expects($this->once())->method('debug')->with($this->stringStartsWith('Timing for'))->willReturnCallback(function ($message, $context) {
$loggerMock->shouldReceive('debug')->once()->andReturnUsing(function ($message, $context) {
$loggedCookies = $context['http']['response']['cookies.raw'];
$loggedCookies = json_decode($loggedCookies, true);
$this->assertEquals('[ MASKED ]', $loggedCookies['SECRET_COOKIE']['value']);
Expand All @@ -220,9 +217,11 @@ public function test_it_masks_response_cookies(): void

public function test_it_doesnt_crash_if_exception_on_response_doesnt_exist(): void
{
$loggerMock = $this->createMock(Logger::class);
Log::partialMock()->shouldReceive('getLogger')->once()->withAnyArgs()->andReturn($loggerMock);
Log::partialMock()->shouldNotReceive('error');
$loggerMock = Log::partialMock();
Log::setApplication($this->app);

// Assert debug was called on loggerMock once with {} request body
$loggerMock->shouldNotReceive('error');

$middleware = new LogRequest();

Expand All @@ -238,10 +237,10 @@ public function test_it_truncates_very_long_json_bodies(): void
// Set config request-log.truncateBodyLength to 100
Config::set('request-log.truncateBodyLength', 100);

$loggerMock = $this->createMock(Logger::class);
Log::partialMock()->shouldReceive('getLogger')->once()->withAnyArgs()->andReturn($loggerMock);
$loggerMock = Log::partialMock();
Log::setApplication($this->app);

$loggerMock->expects($this->once())->method('debug')->with($this->stringStartsWith('Timing for'))->willReturnCallback(function ($message, $context) {
$loggerMock->shouldReceive('debug')->once()->andReturnUsing(function ($message, $context) {
$this->assertEquals(100, strlen($context['http']['request']['body']['content']));
$this->assertEquals(100, strlen($context['http']['response']['body']['content']));
});
Expand All @@ -264,10 +263,11 @@ public function test_it_doesnt_truncate_very_long_json_bodies_if_disabled(): voi
// Set config request-log.truncateBodyLength to 100
Config::set('request-log.truncateBodyLength', -1);

$loggerMock = $this->createMock(Logger::class);
Log::partialMock()->shouldReceive('getLogger')->once()->withAnyArgs()->andReturn($loggerMock);
$loggerMock = Log::partialMock();
Log::setApplication($this->app);

$loggerMock->expects($this->once())->method('debug')->with($this->stringStartsWith('Timing for'))->willReturnCallback(function ($message, $context) {
// Assert debug was called on loggerMock once with {} request body
$loggerMock->shouldReceive('debug')->once()->andReturnUsing(function ($message, $context) {
$this->assertEquals(48897, strlen($context['http']['request']['body']['content']));
$this->assertEquals(48897, strlen($context['http']['response']['body']['content']));
});
Expand All @@ -290,10 +290,10 @@ public function test_it_doesnt_truncate_bodies_shorter_than_truncate_limit(): vo
// Set config request-log.truncateBodyLength to 100
Config::set('request-log.truncateBodyLength', 100);

$loggerMock = $this->createMock(Logger::class);
Log::partialMock()->shouldReceive('getLogger')->once()->withAnyArgs()->andReturn($loggerMock);
$loggerMock = Log::partialMock();
Log::setApplication($this->app);

$loggerMock->expects($this->once())->method('debug')->with($this->stringStartsWith('Timing for'))->willReturnCallback(function ($message, $context) {
$loggerMock->shouldReceive('debug')->once()->andReturnUsing(function ($message, $context) {
$this->assertEquals(3, strlen($context['http']['request']['body']['content']));
$this->assertEquals(3, strlen($context['http']['response']['body']['content']));
});
Expand Down

0 comments on commit 30f71dc

Please sign in to comment.