Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions config/http-logger.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
<?php

return [
'enabled' => env('LOG_HTTP_ENABLED', true),
'enabled' => env('HTTP_LOG_ENABLED', true),

'connection' => env('DB_CONNECTION'),

'table' => 'http_logs',

'hide' => [
'authorization',
'token',
'access_token',
'password',
'password_confirmation',
'enabled' => env('HTTP_LOG_HIDE_ENABLED', true),

'keys' => [
'authorization',
'token',
'access_token',
'password',
'password_confirmation',
],
],
];
13 changes: 10 additions & 3 deletions src/Casts/Hide.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ public function get($model, string $key, $value, array $attributes): array

public function set($model, string $key, $value, array $attributes): string
{
$value = $this->process((array) $value);
if ($this->enabled()) {
$value = $this->process((array) $value);
}

return json_encode($value);
return json_encode((array) $value, JSON_NUMERIC_CHECK);
}

protected function process(array $values): array
Expand Down Expand Up @@ -53,8 +55,13 @@ protected function hide(mixed $value): string
return str_pad('', $length, $this->mask);
}

protected function enabled(): bool
{
return (bool) config('http-logger.hide.enabled', true);
}

protected function hides(): array
{
return config('http-logger.hide', []);
return config('http-logger.hide.keys', []);
}
}
67 changes: 66 additions & 1 deletion tests/HideTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class HideTest extends TestCase
{
public function testLogging(): void
public function testEnabled(): void
{
$method = 'POST';
$name = 'api.pages.create';
Expand Down Expand Up @@ -70,4 +70,69 @@ public function testLogging(): void
'content-type' => ['application/x-www-form-urlencoded'],
], $log->headers);
}

public function testDisabled(): void
{
config(['http-logger.hide.enabled' => false]);

$method = 'POST';
$name = 'api.pages.create';
$path = 'api/pages';

$uri = $path . '?' . http_build_query([
'foo' => 'Foo',
'token' => 123,
'access_token' => 456,
]);

$this->assertDatabaseLogsCount(0);

$response = $this->post($uri, [
'bar' => 'Bar',

'password' => 'q123456',
'password_confirmation' => 'q123456',
], [
'Authorization' => 'Bearer QwErTy',
]);

$response->assertNoContent();

$this->assertDatabaseLogsCount(1);
$this->assertDatabaseHasRecord($method, $name, $path);

$log = HttpLog::where(compact('method', 'name'))->first();

$this->assertSame($method, $log->method);
$this->assertSame($name, $log->name);
$this->assertSame($path, $log->path);

$this->assertSame('http', $log->scheme);
$this->assertSame('localhost', $log->host);
$this->assertSame(80, $log->port);
$this->assertSame('127.0.0.1', $log->ip);

$this->assertSame([
'foo' => 'Foo',
'token' => 123,
'access_token' => 456,
], $log->query);

$this->assertSame([
'bar' => 'Bar',

'password' => 'q123456',
'password_confirmation' => 'q123456',
], $log->payload);

$this->assertSame([
'host' => ['localhost'],
'user-agent' => ['Symfony'],
'accept' => ['text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'],
'accept-language' => ['en-us,en;q=0.5'],
'accept-charset' => ['ISO-8859-1,utf-8;q=0.7,*;q=0.7'],
'authorization' => ['Bearer QwErTy'],
'content-type' => ['application/x-www-form-urlencoded'],
], $log->headers);
}
}