php 用于调试的 YII 日志记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17872005/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:36:37  来源:igfitidea点击:

YII logging for debugging

phpcakephpyii

提问by madi

In many cases, Xdebugis not suitable for debugging as it involves clicks to run to a particular line of codes. I want to use something that is similar to cakePHPdebug function for developers to output the value of a particular property of a class to the browser.

在许多情况下,Xdebug不适合调试,因为它涉及点击以运行到特定的代码行。我想使用类似于cakePHP调试功能的东西让开发人员将类的特定属性的值输出到浏览器。

I am using Yii frameworkand this is my configuration for the yii login the main.php:

我使用的Yii framework,这是我的配置yii logmain.php

'log'=>array(
    'class'=>'CLogRouter',
        'routes'=>array(
        array(
            'class'=>'CFileLogRoute',
            'levels'=>'trace, info, error, warning, vardump',
        ),
        array(
            'class'=>'CWebLogRoute',
                                'enabled' => YII_DEBUG,
            'levels'=>'error, warning, trace, log, vardump',
            'showInFireBug'=>true,
        ),
    ),
), 

In one of my defined controller i put this code to test:

在我定义的控制器之一中,我将此代码用于测试:

Yii::log("CallFromUserController",'info', 'application');

However i don't see this being printed in the firebug. I used Chris's example:

但是我没有看到这个被打印在萤火虫中。我用克里斯的例子:

http://chris-backhouse.com/advanced-logging-in-yii/775

http://chris-backhouse.com/advanced-logging-in-yii/775

回答by madi

I finally managed to find out a solution:

我终于设法找到了解决方案:

In my main.php I did this:

在我的 main.php 我这样做了:

'log' => array(
    'class' => 'CLogRouter',
    'routes' => array(
        array(
            'class' => 'CFileLogRoute',
            'levels' => 'trace, info, error, warning, vardump',
        ),
        // uncomment the following to show log messages on web pages
        array(
            'class' => 'CWebLogRoute',
            'enabled' => YII_DEBUG,
            'levels' => 'error, warning, trace, notice',
            'categories' => 'application',
            'showInFireBug' => false,
        ),
    ),
),

In my controller I used this code:

在我的控制器中,我使用了以下代码:

$a = new array(1,2,3);
Yii::trace(CVarDumper::dumpAsString($a));

The Application Log is shown below in every page.

应用程序日志显示在每个页面的下方。

回答by Habeeb Perwad

In Yii2

在 Yii2

Yii::trace(VarDumper::dumpAsString($array));

An example of customized log class is given below.

下面给出了自定义日志类的示例。

<?php

namespace app\helpers;

use Yii;
use yii\helpers\VarDumper;

class Log {

    const LOG_CATEGORY_NAME = 'myLog';

    public static function e($msg, $data) {

        if (isset($data)) {
            $msg .= " " . VarDumper::dumpAsString($data);
        }   
        Yii::error($msg, self::LOG_CATEGORY_NAME);
    }   
}

Example:

例子:

 Log.e("Copying the Estimate data failed. Model::getError(): ", 
        $model->getErrors());

回答by Shaig Khaligli

You should add following lines to your protected/main.php file for debug:true mode. Trust work like a charm !

您应该将以下几行添加到您的 protected/main.php 文件以用于 debug:true 模式。信任工作就像一种魅力!

return array(
    'preload' => array(
        'debug',
    ),
    'components' => array(
        'debug' => array(
            'class' => 'ext.yii2-debug.Yii2Debug',


         ),
            'db' => array(
                'enableProfiling' => true,
                'enableParamLogging' => true,
            ),
        ),
    );