26 February 2024

In this snippet we're using log_message() to log messages of different severity levels (error, debug, info). Depending on the condition, we log different messages. Ensure that logging is enabled and properly configured in your CodeIgniter 4 application.

Configuration

In CodeIgniter 4, the Logger class provides several threshold levels to filter log messages based on their severity. These threshold levels are used to determine which log messages should be recorded. Here are the threshold levels available in CodeIgniter 4:
  1. emergency: System is unusable.
  2. alert: Action must be taken immediately.
  3. critical: Critical conditions.
  4. error: Error conditions.
  5. warning: Warning conditions.
  6. notice: Normal but significant condition.
  7. info: Informational messages.
  8. debug: Debug-level messages.
You can set the threshold level in your application's configuration file, typically located at app/Config/Logger.php. Within this file, you'll find a configuration option called $threshold, where you can specify the desired threshold level. For example:
Source code viewer
  1. // This will log messages of severity "error" and lower.
  2. public $threshold = 3;
Programming Language: PHP
You can adjust the threshold level according to your application's requirements. Any log message with a severity level equal to or lower than the specified threshold will be recorded in the log file.

Log function

Source code viewer
  1. log_message('info', 'Log an info message.');
Programming Language: PHP