Installation:
composer require bbit/auditlog-bundle:0.1
Ensure BBIT\AuditLogBundle\BBITAuditLogBundle() is registered in AppKernel.php.
Database Migration:
The bundle requires a audit_log table. Manually create it or use a migration:
// src/Migrations/YYYY_MM_DD_create_audit_log_table.php
Schema::create('audit_log', function (Blueprint $table) {
$table->increments('id');
$table->string('type');
$table->string('channel');
$table->text('message');
$table->timestamps();
});
First Log Entry: Inject the service in a controller or command:
use BBIT\AuditLogBundle\Service\AuditLogService;
class ExampleController extends Controller
{
public function __construct(AuditLogService $logger)
{
$this->logger = $logger;
}
public function index()
{
$this->logger->log('security', 'auth', 'User logged in');
}
}
Logging Events:
Use log($type, $channel, $message) for structured entries. Example:
$this->logger->log('admin', 'dashboard', 'User deleted record #123');
Integration with Symfony Events: Attach the logger to event listeners:
// src/EventListener/AuditListener.php
class AuditListener implements EventSubscriberInterface
{
public function __construct(AuditLogService $logger) {}
public function onKernelRequest(GetResponseEvent $event)
{
$this->logger->log('system', 'request', 'Incoming request: ' . $event->getRequest()->getPathInfo());
}
}
Batch Logging: For performance, batch logs in commands:
// src/Command/ProcessLogsCommand.php
class ProcessLogsCommand extends Command
{
protected function execute(InputInterface $input, OutputInterface $output)
{
$logs = [];
foreach ($items as $item) {
$logs[] = ['type' => 'data', 'channel' => 'import', 'message' => $item->logMessage()];
}
// Bulk insert (requires custom DB logic; bundle lacks native support)
}
}
Dynamic Channels: Use closures for dynamic channel/message generation:
$this->logger->log('user', function ($log) {
$log->setChannel('user_' . $user->id);
$log->setMessage('Profile updated: ' . $user->email);
});
Conditional Logging: Wrap logs in checks to avoid clutter:
if ($this->isDebug()) {
$this->logger->log('debug', 'controller', 'Rendered view: ' . $viewName);
}
No Native Query Builder: The bundle lacks a fluent query interface. Use raw queries or a custom repository:
$logs = DB::table('audit_log')
->where('type', 'security')
->where('created_at', '>', now()->subDays(7))
->get();
Missing Configuration:
The bundle has no config.yml support. Hardcode defaults or extend the service:
# No config file exists; override via DI if needed.
No Soft Deletes:
The audit_log table lacks a deleted_at column. Implement manually if needed.
Performance: Avoid logging in loops without batching. For high-volume logs, consider:
DB::beginTransaction();
foreach ($items as $item) {
DB::table('audit_log')->insert([...]);
}
DB::commit();
Check Table Structure:
Verify the audit_log table matches the expected schema (e.g., type, channel, message columns).
Log Service Availability: Ensure the service is autowired correctly. Test with:
$this->logger->log('test', 'init', 'Service test');
Query Logs: Enable Doctrine logging to debug queries:
// config/packages/dev/doctrine.yaml
doctrine:
dbal:
logging: true
profiling: true
Custom Log Model:
Extend the bundle’s AuditLog entity or create a proxy:
class CustomAuditLog extends \BBIT\AuditLogBundle\Entity\AuditLog
{
public function setAdditionalData(array $data) { ... }
}
Event Dispatching: Trigger Symfony events after logging:
// Override the service to dispatch events
$event = new AuditLoggedEvent($log);
$this->dispatcher->dispatch($event, AuditLoggedEvent::NAME);
Async Logging: Use a queue (e.g., Symfony Messenger) for non-critical logs:
$message = new AuditLogMessage('type', 'channel', 'message');
$this->messageBus->dispatch($message);
How can I help you explore Laravel packages today?