feat(logging): Implement database logging with app_log table
- Created migration to set up app_log table for storing application logs. - Added AppLog entity and repository for ORM handling of logs. - Developed DbLogger service to persist logs of level WARNING and above to the database while maintaining existing logging behavior. - Implemented tests for admin log retrieval and DbLogger functionality to ensure proper logging behavior. - Enhanced logging context sanitization for better error tracking.
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace App\Tests\Admin;
|
||||
|
||||
use App\Tests\ApiTestCase;
|
||||
|
||||
/**
|
||||
* GET /api/v1/admin/logs — admin-only, paginated, filterable by level.
|
||||
*/
|
||||
class AdminLogsTest extends ApiTestCase
|
||||
{
|
||||
private function seedLog(string $level, string $message): void
|
||||
{
|
||||
$this->em->getConnection()->insert('app_log', [
|
||||
'level' => $level,
|
||||
'message' => $message,
|
||||
'channel' => 'app',
|
||||
'path' => '/test',
|
||||
'created_at' => time(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function testNonAdminForbidden(): void
|
||||
{
|
||||
$user = $this->createUser(['ROLE_USER']);
|
||||
$this->authJson('GET', '/api/v1/admin/logs', $user);
|
||||
self::assertSame(403, $this->responseCode());
|
||||
}
|
||||
|
||||
public function testAdminSeesLogsFilteredByLevel(): void
|
||||
{
|
||||
$marker = 'ADMINLOGTEST_' . bin2hex(random_bytes(5));
|
||||
$this->seedLog('error', $marker . '_err');
|
||||
$this->seedLog('warning', $marker . '_warn');
|
||||
|
||||
$admin = $this->createUser(['ROLE_ADMIN']);
|
||||
$body = $this->authJson('GET', '/api/v1/admin/logs?level=error&search=' . $marker, $admin);
|
||||
|
||||
self::assertSame(200, $this->responseCode());
|
||||
self::assertTrue($body['success']);
|
||||
self::assertCount(1, $body['data']);
|
||||
self::assertSame('error', $body['data'][0]['level']);
|
||||
self::assertSame($marker . '_err', $body['data'][0]['message']);
|
||||
self::assertArrayHasKey('totalRecords', $body['meta']);
|
||||
|
||||
$this->em->getConnection()->executeStatement('DELETE FROM app_log WHERE message LIKE ?', [$marker . '%']);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user