You can log information to the local log files by using the Show <?php if ($some_var === '') { log_message('error', 'Some variable did not contain a value.'); } There are eight different log levels, matching to the RFC 5424 levels, and they are as follows:
The logging system does not provide ways to alert sysadmins or webmasters about these events, they solely log the information. For many of the more critical event levels, the logging happens automatically by the Error Handler, described above. ConfigurationYou 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 <?php namespace Config; use CodeIgniter\Config\BaseConfig; class Logger extends BaseConfig { public $threshold = 5; // ... } A complete list of levels and their corresponding threshold value is in the configuration file for your reference. You can pick and choose the specific levels that you would like logged by assigning an array of log level numbers to the threshold value: <?php namespace Config; use CodeIgniter\Config\BaseConfig; class Logger extends BaseConfig { // Log only debug and info type messages public $threshold = [5, 8]; // ... } Using Multiple Log HandlersThe 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:
The handlers are configured in the main configuration file, in the <?php namespace Config; use CodeIgniter\Config\BaseConfig; class Logger extends BaseConfig { public $handlers = [ // File Handler 'CodeIgniter\Log\Handlers\FileHandler' => [ 'handles' => ['critical', 'alert', 'emergency', 'debug', 'error', 'info', 'notice', 'warning'], ], ]; // ... } Modifying the Message With ContextYou will often want to modify the details of your message based on the context of the event being logged. You might need to log a user id, an IP address, the current POST variables, etc. You can do this by use placeholders in your message. Each placeholder must be wrapped in curly braces. In the third parameter, you must provide an array of placeholder names (without the braces) and their values. These will be inserted into the message string: <?php // Generates a message like: User 123 logged into the system from 127.0.0.1 $info = [ 'id' => $user->id, 'ip_address' => $this->request->getIPAddress(), ]; log_message('info', 'User {id} logged into the system from {ip_address}', $info); If you want to log an Exception or an Error, you can use the key of ‘exception’, and the value being the Exception or Error itself. A string will be generated from that object containing the error message, the file name and line number. You must still provide the exception placeholder in the message: <?php try { // Something throws error here } catch (\Exception $e) { log_message('error', '[ERROR] {exception}', ['exception' => $e]); } Several core placeholders exist that will be automatically expanded for you based on the current page request:
Using Third-Party LoggersYou can use any other logger that you might like as long as it extends from either You must ensure that the third-party logger can be found by the system, by adding it to either the app/Config/Autoload.php configuration file, or through another autoloader, like Composer. Next, you should modify app/Config/Services.php to point the Now, any call that is done through the |