Reputation: 201
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
Upvotes: 20
Views: 89057
Reputation: 75
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
)
Upvotes: 4
Reputation: 1057
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
Upvotes: 8
Reputation: 301
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:
$config['log_threshold']
to 2, 3 or 4log_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));
Upvotes: 28
Reputation: 3187
You need to set your $config['log_threshold'] in file config.php since by default it's 0 (no logs)
Upvotes: 16