Logging & Debugging
Effective debugging starts with understanding where and how the application logs information. CodeIgniter 4 provides a powerful logging service.
1. Log Locations
All application logs are stored in the writable directory:
/writable/logs/log-YYYY-MM-DD.logEach day generates a new log file. Errors, warnings, and custom log messages are appended here.
2. System Activity Logs (Audit Trail)
For auditing user actions (Create, Update, Delete), the system uses a database-driven Activity Log System.
Viewing Logs
Logs are visible in the Admin Panel at /admin/activity-logs. A widget is also available on the Dashboard.
Database Table
Logs are stored in the activity_logs table:
user_id: Who performed the actionaction:create,update,delete,login, etc.entity_type: The resource modified (e.g.,user,role)old_values/new_values: JSON snapshots for diff views
Exporting Logs
Logs can be exported in various formats for offline analysis or reporting. Click the Export dropdown in the top-right corner of the Activity Logs page.
- CSV: Best for spreadsheet software and raw data analysis.
- Excel: Formatted spreadsheet with headers.
- PDF: Printable report layout.
Note: The export respects all currently applied filters (User, Date Range, etc.).
Implementing Logging
Use the global Event system to trigger logs from any Controller or Service:
use CodeIgniter\Events\Events;
// Basic Log
Events::trigger('activity.log', [
'action' => 'create',
'entity_type' => 'user',
'entity_id' => $user->id,
'description' => "Created user {$user->name}",
'new_values' => $user->toArray()
]);
// Update Log (with diff)
Events::trigger('activity.log', [
'action' => 'update',
'entity_type' => 'user',
'entity_id' => $user->id,
'description' => "Updated user profile",
'old_values' => $originalUser->toArray(),
'new_values' => $updatedUser->toArray()
]);3. Writing File Logs
Use the global log_message() function to record events.
// Log an error (Critical issues)
log_message('error', 'Payment failed for Order #123: {message}', ['message' => $e->getMessage()]);
// Log a warning (Potential issues)
log_message('warning', 'API rate limit approaching');
// Log info (General events)
log_message('info', 'User logged in: ' . $userId);
// Log debug (Development details)
log_message('debug', 'SQL Query: ' . $builder->getCompiledSelect());3. Checking Logs
Via Command Line (Tail)
You can monitor logs in real-time using `tail` inside the container:
docker-compose exec php tail -f writable/logs/log-$(date +%Y-%m-%d).phpCommon Errors
| Error | Likely Cause | Solution |
|---|---|---|
ClassNotFoundException | Namespace mismatch or missing file | Check `namespace` and file path |
AccessDeniedException | Permissions or RBAC issue | Check user roles or file permissions (chmod) |
DatabaseException | SQL syntax or connection error | Check `.env` credentials and query syntax |
4. Debug Toolbar
In the development environment, the CI4 Debug Toolbar appears at the bottom of the screen. It provides instant access to:
- Executed SQL Queries
- Route Information
- Session Data
- Performance Timings
Click the Fire Icon in the bottom right to expand it.