v1.0
Docs / Logging & Debugging

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.log

Each 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:

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.

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).php

Common Errors

ErrorLikely CauseSolution
ClassNotFoundExceptionNamespace mismatch or missing fileCheck `namespace` and file path
AccessDeniedExceptionPermissions or RBAC issueCheck user roles or file permissions (chmod)
DatabaseExceptionSQL syntax or connection errorCheck `.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:

Click the Fire Icon in the bottom right to expand it.

ESC

Start typing to search the documentation