Codeigniter 4 Tutorials
- Codeigniter 4
- Application Run
- Application Structure
- Configuration files
- Errors
- Controller
- View
- Model
- How to remove index.php from URL
- Route
- CodeIgniter URLs
- Helper
- Session
- Global Functions and Constants
- Logging Information in CI4
- Error Handling
- Signin / SignUp
- Login / Register
- CRUD
- Crud 1
- Form Validation
- Data Table
- Pagination
- File Upload
- Show Data Using Ajax
- Select2 AJAX Autocomplete Search
- Curl Post
- Rest API
- Weblink
What is Logging in Codeigniter 4?
You can log information to the local log files by using the log_message() method. You must supply the “level” of the error in the first parameter. The second parameter is the message.
File name : index.php
if ($var_x == '')
{
log_message('error', 'This variable did not contain a value.');
}
There are eight different log levels,
File name : index.php
Level :- Description
debug :- Detailed debug information.
info :- Interesting events in your application, like a user logging in, logging SQL queries, etc.
notice :- Normal, but significant events in your application.
warning :-Exceptional occurrences that are not errors, like the use of deprecated APIs,
poor use of an API, or other undesirable things that are not necessarily wrong.
error :- Runtime errors that do not require immediate action but should typically be logged and monitored.
critical :- Critical conditions, like an application component not available, or an unexpected exception.
alert :- Action must be taken immediately, like when an entire website is down, the database unavailable, etc.
emergency :- The system is unusable.
Configuration
You can modify which levels are actually logged, as well as assign different Loggers to handle different levels, within the /app/Config/Logger.php configuration file.
The threshold value of the config file determines which levels are logged across your application. If any levels are requested to be logged by the application, but the threshold doesn’t allow them to log currently, they will be ignored. The simplest method to use is to set this value to the minimum level that you want to have logged. For example, if you want to log warning messages, and not information messages, you would set the threshold to 5.
File name : index.php
public $threshold = 5;
public $threshold = [5, 8];
Using Multiple Log Handlers
The logging system can support multiple methods of handling logging running at the same time. Each handler can be set to handle specific levels and ignore the rest. Currently, three handlers come with a default install:
File Handler is the default handler and will create a single file for every day locally. This is the recommended method of logging.
ChromeLogger Handler If you have the ChromeLogger extension installed in the Chrome web browser, you can use this handler to display the log information in Chrome’s console window.
Errorlog Handler This handler will take advantage of PHP’s native error_log() function and write the logs there. Currently, only the 0 and 4 message types of error_log() are supported.
public $handlers = [
//--------------------------------------------------------------------
// File Handler
//--------------------------------------------------------------------
'CodeIgniter\Log\Handlers\FileHandler' => [
'handles' => ['critical', 'alert', 'emergency', 'debug', 'error', 'info', 'notice', 'warning'],
]
];
Modifying the Message With Context
File name : index.php
// Generates a message like: User 123 logged into the system from 127.0.0.1
$info = [
'id' => $user->id,
'ip_address' => $this->request->ip_address()
];
log_message('info', 'User {id} logged into the system from {ip_address}', $info);
File name : index.php
File name : index.php
File name : index.php
File name : index.php