如何打印字符串到控制台 Yii2

How to print string to console Yii2

我是 Yii2 的新手,我正在尝试将一个简单的字符串打印到控制台。但是,无论我做什么,我都无法真正让它发挥作用。我不确定我的设置是否有误,或者我是否没有按预期使用这些功能。

我的 TeamController.php 是由 Gii 生成的,我想简单地检查我的代码是否运行的函数如下所示:

<?php

namespace app\controllers;

use Yii;
use app\models\Team;
use yii\data\ActiveDataProvider;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\log\Logger;
use yii\helpers\VarDumper;

    /**
     * Creates a new Team model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        VarDumper::dump('Hello world'); //<-- Does not print to console.
        Yii::debug('start calculating average revenue'); //<-- Does not print to console.
        $model = new Team();
        if ($this->request->isPost) {
            if ($model->load($this->request->post()) && $model->save()) {
                return $this->redirect(['view', 'idteam' => $model->id]);
            }
        } else {
            $model->loadDefaultValues();
        }

        return $this->render('create', [
            'model' => $model,
        ]);
    } 

更新

更改了 config 目录中 web.php 中的一些配置。:

'log' => [
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning', 'trace', 'info'],
                    'logVars' => [],
                    'logFile' => '@runtime/webapp/logs/myfile.log',
                ],
            ],
        ], 

是我忘记了什么还是其他配置有误?

您可以尝试设置flushIntervalexportInterval

'log' => [
    'flushInterval' => 1, // <-- here
    'targets' => [
        [
            'class' => 'yii\log\FileTarget',
            'levels' => ['error', 'warning', 'trace', 'info'],
            'logVars' => [],
            'logFile' => '@runtime/webapp/logs/myfile.log',
            'exportInterval' => 1, // <-- and here
        ],
    ],
]

来源:https://yii2-cookbook-test.readthedocs.io/logging-problems-and-solutions/