php 在 CodeIgniter(和 MVC)中调试
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13562950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Debugging in CodeIgniter (and MVC)
提问by Thibaut Delarbre
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?
很多时候我觉得有必要为了调试目的而转储一个变量。在 CodeIgniter 中,我很难做到这一点,因为我不能简单地“回显”一个变量而不先将它传递给视图,我认为这很痛苦。我已经阅读了官方文档,虽然我已经找到了一个很好的解决方案 log_message 功能但无法使其工作,即使我成功地使“logs”文件夹可写并按照建议更改了“阈值”。有什么建议?
回答by Jordan Harper
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.
这不是一个好的做法,但是您可以在执行的任何点(从核心类、库、模型和控制器)向浏览器输出任何内容,只需print_r()在要输出信息的点使用即可。
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.
该die()命令将停止所有执行,只留下您想要输出的信息。
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:
使用log_message()是更好的做法,但如果您说您已遵循以下步骤,则很难理解为什么您在那里没有成功:
- Make sure that your logs folder is set to be writable
- Set
$config['log_threshold']to 2, 3 or 4 - Used the function like so:
log_message('debug','Message you want to log');
- 确保您的日志文件夹设置为可写
- 设置
$config['log_threshold']为 2、3 或 4 - 像这样使用函数:
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:
如果你想使用print_r()很好地格式化日志信息的数组或对象,那么你就需要设置的第二个参数print_r()来TRUE,像这样:
log_message('debug',print_r($array_or_object,TRUE));
回答by SubRed
You need to set your $config['log_threshold'] in file config.php since by default it's 0 (no logs)
您需要在文件 config.php 中设置 $config['log_threshold'] ,因为默认情况下它是 0(无日志)
- 0 = Disables logging, Error logging TURNED OFF
- 1 = Error Messages (including PHP errors)
- 2 = Debug Messages
- 3 = Informational Messages
- 4 = All Messages
- 0 = 禁用记录,错误记录关闭
- 1 = 错误消息(包括 PHP 错误)
- 2 = 调试消息
- 3 = 信息性消息
- 4 = 所有消息
回答by YahyaE
I think the answer is you are looking for is: Codeigniter - Developer's Debug Helper
我认为您正在寻找的答案是: Codeigniter - Developer's Debug Helper
You can add this file to application/helperfolder. Then autoload vayes_helperin application/config/autoload.phpfor generic use. Then all you need to is writing:
您可以将此文件添加到application/helper文件夹。然后自动加载vayes_helper以application/config/autoload.php供通用使用。然后你只需要写:
vdebug($variable); // In Controllers, Models, Libraries, Helpers
<?=vdebug($variable)?> // In View files
回答by Ritesh Kumar
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 :
这会将您的数组很好地打印到 log 中。以下是输出示例:
[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
)

