Yii2 how to specify the action action, record the log
For example, the following two classes
SiteController.php
namespace admin\controllers;
use admin\models\Store_set;
use yii;
class SiteController extends UserController {
public function actionUpBoot() {
}
}
OrderController.php
namespace admin\controllers;
use admin\models\Store_set;
use yii;
class OrderController extends UserController {
public function actionOrder() {
}
public function actionTest() {
}
}
Just want to log in the OrderController.php class
Or just want to log in the actionTest method,what should I do?
| Q | A
| ---------------- | ---
| Yii version | 2.0.15
| PHP version | 7.*
@larryli @bio @ouhman @marsuboss
@samdark
this is my config
return [
'id' => 'admin',
'defaultRoute'=>'index',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'controllerNamespace' => 'admin\controllers',
'components' => [
'request' => [
'csrfParam' => '_csrf',
'enableCookieValidation' => true,
],
'session' => [
// this is the name of the session cookie used for login on the control
'name' => 'create-session',
'timeout' => 3600,
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
//[
// 'class' => 'yiilog\FileTarget',
// 'levels' => ['error', 'warning','info'],
// 'logVars' => ['']
//],
[
'class' => 'yiilog\DbTarget',
'levels' => ['error', 'warning','info'],
'logVars' => ['']
],
],
],
'errorHandler' => [
'errorAction' => 'common/error',
],
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'enableStrictParsing' => false,
'suffix' => '',
'rules' => [
]
]
],
'params' => $params,
];
what should I do?
Just call Yii::info() (or debug/error/warning) inside your action. Depending on how you configured the log component, the message gets logged to the file.
@xingchunzhe111
You may attach custom behavior to your controllers. Something like this:
// app/behaviors/ActionsLogBehavior.php
namespace app\behaviors;
use app\base\Controller;
use Yii;
use yii\base\ActionEvent;
use yii\base\Behavior;
class ActionsLogBehavior extends Behavior
{
public function events()
{
return [
Controller::EVENT_AFTER_ACTION => 'afterAction',
];
}
/**
* @param ActionEvent $event
*/
public function afterAction($event)
{
if (Yii::$app->request->method !== 'GET') {
$actionUid = $event->action->getUniqueId();
Yii::info($actionUid, __METHOD__);
}
}
}
Then attach it to your controllers' base class:
class UserController extends Controller {
public function behaviors()
{
return [
'actionsLog' => 'app\behaviors\ActionsLogBehavior',
];
}
// rest of your code
}
Otherwise you may attach it from your config:
'as actionsLog' => 'app\behaviors\ActionsLogBehavior',
Thank you for your question.
In order for this issue tracker to be effective, it should only contain bug reports and feature requests.
We advise you to use our community driven resources:
If you are confident that there is a bug in the framework, feel free to provide information on how to reproduce it. This issue will be closed for now.
_This is an automated comment, triggered by adding the label question._
@ xingchunzhe111
您可以将自定义行为附加到控制器。像这样的东西:// app / behavior / ActionsLogBehavior.php 命名空间 app \ behavior ; 使用 app \ base \ Controller ; 使用 Yii ; 使用 yii \ base \ ActionEvent ; 使用 yii \ base \ Behavior ; class ActionsLogBehavior extends Behavior { public function events() { return [ Controller :: EVENT_AFTER_ACTION => ' afterAction ', ]; } / ** * @param ActionEvent $ event * / public function afterAction( $ event) { if( Yii :: $ app - > request - > method !== ' GET '){ $ actionUid = $ event - > action - > getUniqueId(); Yii :: info( $ actionUid, __ METHOD__); } }}然后将它附加到控制器的基类:
class UserController extends Controller { public function behavior() { return [ ' actionsLog ' => ' app \ behavior \ ActionsLogBehavior ', ]; } //其余的代码 }否则你可以从你的配置中附加它:
' as actionsLog ' => ' app \ behavior \ ActionsLogBehavior ',
I just want to record sql, yii::info how to get the executed sql and record it
只需
Yii::info()在您的操作中调用(或调试/错误/警告)。根据您配置log组件的方式,消息将记录到文件中。
I just want to record sql, yii::info how to get the executed sql and record it
@xingchunzhe111 Please google your question. https://stackoverflow.com/a/36741997