Debugging in CodeIgniter (and MVC)

76,248

Solution 1

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));

Solution 2

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

Solution 3

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

Solution 4

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

This will print your array nicely into log . Following is the output example :

[1] => Array
    (
        [uid] => 10049082
        [phone_id] => 2
        [friend_status] => 0
        [friend_event_hide_status] => 
    )

[2] => Array
    (
        [uid] => 10042768
        [phone_id] => 4
        [friend_status] => 3
    )

[3] => Array
    (
        [uid] => 10078255
        [phone_id] => 6
        [friend_status] => 2
    )

[4] => Array
    (
        [uid] => 10078255
        [phone_id] => -1
        [friend_status] => 1
        [name] => Rajesh 
    )

[5] => Array
    (
        [uid] => 10078255
        [phone_id] => -1
        [friend_status] => 1
        [name] => ritesh kumar
    )

[6] => Array
    (
        [uid] => 10078255
        [phone_id] => -1
        [friend_status] => 1
        [name] => Le
    )
Share:
76,248

Related videos on Youtube

Thibaut Delarbre
Author by

Thibaut Delarbre

Updated on July 05, 2022

Comments

  • Thibaut Delarbre
    Thibaut Delarbre almost 2 years

    Often times I feel the need to dump a variable for debugging purpose. In CodeIgniter I am struggling to do that since I cannot simply "echo" a variable without passing it to the view first, which I think is pain-staking. I have read the official documentation, though I had found a good solution with the log_message function but cannot make it work, even though I successfully made the "logs" folder writable and changed the "threshold" as advised. Any suggestions?

    https://www.codeigniter.com/user_guide/general/errors.html

    • Jacob Kranz
      Jacob Kranz over 11 years
      I know this has never been a good practice but I always just print_r() or echo stuff before I load the views. It'll appear before your header on top of the page. Again, bad practice but it suffices depending on how much you need to do. Also, in your index.php, set ini_set to ini_set(E_ALL).
    • SubRed
      SubRed over 11 years
      You don't have to always echo/print/dump variables/values to the view first if you want to do debug actually. You can do it anywhere on your code like on your controller even your model. It depends on how your code works.
    • Pankaj Khairnar
      Pankaj Khairnar over 11 years
      its not like that you can print_r from controller method. I have used it 100 of times
    • ankit suthar
      ankit suthar over 7 years
      I am currently working on the same issue. I have found that CI logger system will give you the much-needed info if you want. And for adding something to debug or in info or in error you can simply use print_r.
    • ankit suthar
      ankit suthar over 7 years
      You can log message from controller or model you don't need to go to the view for a log message.
  • SubRed
    SubRed over 11 years
    Check also on your index.php file and make sure that the error_reporting set to E_ALL (or what you need).
  • Silviu G
    Silviu G over 11 years
    After setting the log_treshold to 4, nothing happens after doing something like this: log_message('info',print_r($my_var, TRUE)) ?
  • SubRed
    SubRed over 11 years
    have you tried to use it like this log_message('info', $my_var)?
  • Thibaut Delarbre
    Thibaut Delarbre over 11 years
    I believe I messed up when making the "application/logs" folder writable, it works fine now. Sorry and thanks!
  • SubRed
    SubRed over 11 years
    It's ok and you're welcome. I didn't think about this because you said that you have successfully made it writable before. However glad to see you can resolve your problem.
  • bladerz
    bladerz almost 9 years
    That's a very nice helper. Thank you. It should be <?vdebug($variable)?> // In View files
  • ankit suthar
    ankit suthar over 7 years
    I also do it same way.
  • ankit suthar
    ankit suthar over 7 years
    And any error handling technique for database errors? @SubRed
  • YahyaE
    YahyaE over 7 years
    What is you want to debug in active records? @ankitsuthar
  • ankit suthar
    ankit suthar over 7 years
    is this possible to add try, catch & exception for Active records?? @YahyaE
  • RedDragonWebDesign
    RedDragonWebDesign over 6 years
    You may want to try var_export. It prints more information than print_r.
  • FilT
    FilT over 4 years
    Still useful in '20