Debugging in CodeIgniter (and MVC) Debugging in CodeIgniter (and MVC) codeigniter codeigniter

Debugging in CodeIgniter (and MVC)


It's not good practice, but you can output anything to the browser at any point in execution (from core classes, libraries, models and controllers) by simply using print_r() at the point you want to output the info.

Of course, this will sometimes not display as there may be redirects/routes/etc that take the user to the next step in the controller execution, but if you want to suppress this (again, for debug purposes only), you can use:

print_r($string_or_variable_to_debug);die();

The die() command will stop all execution and leave you with just the info you want to output.

Using log_message() is better practice, but it's difficult to understand why you're not having success there if you say you've followed the following steps:

  1. Make sure that your logs folder is set to be writable
  2. Set $config['log_threshold'] to 2, 3 or 4
  3. Used the function like so: log_message('debug','Message you want to log');

If you want to use print_r() to nicely format an array or object inside the log message, then you'll need to set the second parameter of print_r() to TRUE, like so:

log_message('debug',print_r($array_or_object,TRUE));


You need to set your $config['log_threshold'] in file config.php since by default it's 0 (no logs)

  • 0 = Disables logging, Error logging TURNED OFF
  • 1 = Error Messages (including PHP errors)
  • 2 = Debug Messages
  • 3 = Informational Messages
  • 4 = All Messages


I think the answer is you are looking for is:Codeigniter - Developer's Debug Helper

You can add this file to application/helper folder. Then autoload vayes_helper in application/config/autoload.php for generic use. Then all you need to is writing:

vdebug($variable); // In Controllers, Models, Libraries, Helpers<?=vdebug($variable)?> // In View files