I am using the yii2 framework with the 'queue' extension. This extension may fail, which is saved in the log file, and I would like to view the error in a specific view.
I was able to log errors into a specific .log file, so the real question is, how can I view errors from another log file?
Yii framework use category distinguish log file
configļ¼
return [
'bootstrap' => ['log'],
'timeZone' => 'PRC',
'components' => [
'log' => [
'targets' => [
//default
[
'class' => 'yii\log\DbTarget',
'levels' => ['error', 'warning'],
],
//especially
[
'class' => 'yii\log\DbTarget',
'levels' => ['error'],
'categories' => ['yii\db\*'],//The point
'logFile'=>'log.txt',//you custom file
],
//especially2
[
'class' => 'yii\log\DbTarget',
'levels' => ['error'],
'categories' => ['app\models'],//The point
'logFile'=>'log.txt',//you custom file
],
//especially3
[
'class' => 'yii\log\DbTarget',
'levels' => ['error'],
'categories' => ['abc'],//The point
'logFile'=>'log.txt',//you custom file
],
],
],
],
];
Use it
Yii::trace('db error');
Yii::trace('start calculating average revenue', __METHOD__);
Yii::trace('start calculating average revenue', 'abc');
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
'error' =>
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
'categories' => ['yii\queue\Queue'],
'logFile' => '#runtime/logs/error.log',
//'enabled' => YII_DEBUG,
//'exportInterval' => 1,
],
],
],
Related
I am learning how modules work in Yii2 and now I created the following module: gdpr. I can access the following route: /index.php?r=gdpr/user/index. However, I want to access the route like this: /gdpr/user/index. How can I achieve that?
config.php:
<?php
return [
'components' => [
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
['class' => 'yii\rest\UrlRule', 'controller' => 'modules\gdpr\default'],
['class' => 'yii\rest\UrlRule', 'controller' => 'modules\gdpr\user'],
],
],
],
'params' => [
// list of parameters
],
];
You need to configure controllers in this way:
'components' => [
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
[
'class' => 'yii\rest\UrlRule',
'controller' => ['gdpr/default', 'gdpr/user'],
],
],
],
],
See https://www.yiiframework.com/doc/api/2.0/yii-rest-urlrule#$controller-detail
I install yii2-rbac following this site page: https://github.com/dektrium/yii2-rbac/blob/master/docs/installation.md .
I do it second time. First time I have done, but I wrote in the config/web.php file:
'modules' => [
'user' => [
'class' => 'dektrium\user\Module',
],
//'rbac' => 'dektrium\rbac\RbacWebModule',
'rbac' => 'dektrium\rbac\RbacConsoleModule',
],
I did not know, that 'rbac' => 'dektrium\rbac\RbacConsoleModule' it must write in the console.php (not in web.php).
'authManager' => [
'class' => 'yii\rbac\DbManager',
//'defaultRoles' => ['guest'],
],
`
This code I have wrote in both config files: web.php and console.php, but in the web.php I have wrote 'rbac' => 'dektrium\rbac\RbacConsoleModule' and in console.php I have not wrote it, but all worked: yii2-rbac has been installed succeful. And all transaction have passed succeful. But 'rbac' => 'dektrium\rbac\RbacConsoleModule' in web.php seems to me wrong. It isn't web module, it is console module. Then I have rollbacked transactions (migrate/down) and I have removed rbac at all by removing from composer.json "dektrium/yii2-rbac": "1.0.0-alpha#dev" declaration. All has been removed.
Than I began to install rbac second time. After composer installation I have wrote in the web.php:
'modules' => [
'user' => [
'class' => 'dektrium\user\Module',
],
'rbac' => 'dektrium\rbac\RbacWebModule',
//'rbac' => 'dektrium\rbac\RbacConsoleModule',
],
and in console.php I have wrote:
'modules' => [
'rbac' => 'dektrium\rbac\RbacConsoleModule',
],
The site on yii2 don't work after it!!! I have changed in the web.php "...RbacConsoleModule". Site works. Why it don't work with RbacWebModule? Then I tried to apply transactions, that I have rollbacked before, but raise error: You have wrong authManager configuration
enter image description here
What can I do? Help me. Ecscuse me for my English. I'm from Russia.
my console.php:
$config = [
'id' => 'basic-console',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'controllerNamespace' => 'app\commands',
'components' => [
'cache' => [
'class' => 'yii\caching\FileCache',
],
'log' => [
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'db' => $db,
'authManager' => [
'class' => 'yii\rbac\DbManager',
//'defaultRoles' => ['guest'],
]
],
'modules' => [
'rbac' => 'dektrium\rbac\RbacConsoleModule',
],
//....
my web.php:
//This all in $component
'db' => require(__DIR__ . '/db.php'),
'authManager' => [
'class' => 'yii\rbac\DbManager',
//'defaultRoles' => ['guest'],
],
],
'modules' => [
'user' => [
'class' => 'dektrium\user\Module',
],
//'rbac' => 'dektrium\rbac\RbacWebModule',
'rbac' => 'dektrium\rbac\RbacConsoleModule',
],
That all! The problem has been decided. It must write authManager section to modules, not in components:
'modules' => [
'user' => [
'class' => 'dektrium\user\Module',
],
'authManager' => [
'class' => 'yii\rbac\DbManager',
//'defaultRoles' => ['guest'],
]
//'rbac' => 'dektrium\rbac\RbacWebModule',
'rbac' => 'dektrium\rbac\RbacConsoleModule',
]
you need write that:
'components' => [
'authManager' => [
'class' => 'dektrium\rbac\components\DbManager',
'defaultRoles' => ['users'],
],
...
I want to write info message in app.log file for debugging. For that I am using that code in web.php file:-
log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
[
'class' => 'yii\log\FileTarget',
'levels' => ['info'],
'categories' => ['orders'],
'logFile' => '#app/runtime/logs/requests.log',
'maxFileSize' => 1024 * 2,
'maxLogFiles' => 20,
],
[
'class' => 'yii\log\FileTarget',
'levels' => ['info'],
'categories' => ['pushNotifications'],
'logFile' => '#app/runtime/logs/Orders/notification.log',
'maxFileSize' => 1024 * 2,
'maxLogFiles' => 50,
],
],
]
and in my controller file I am using that code :-
Yii::info($valuerendom, 'orders');
but no message is written in .log file.
Thank you,
i want to put a log in app.log ,My config file
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
'file' => [
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
'logFile' => '#root/console/runtime/logs/app.log',
],
]
]
in controller action
public function actionRankCalculation()
{
$allConest = Contest::find()->where('isActive = 1')->all();
Yii::trace('start calculating average revenue');
$response = [];
/** #var Contest $contest */
foreach ($allConest as $contest) {
$videoQuery = Video::find()->where('contest_id = ' . $contest->id);
$videoQuery->andWhere('isActive = 1');
$videoQuery->orderBy([
'global_likes' => SORT_DESC,
'id' => SORT_ASC,
]);
}
But Yii::trace('start calculating average revenue'); not working
You try this.Use categories. For example like below
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error'],
'categories' => ['test1'],
'logFile' => '#app/Test/test1.log',
],
And use below one in controller action
public function actionIndex(){
Yii::error('Test index action', $category = 'test1'); }
Try to set both flushInterval and exportInterval to 1 in console config:
return [
'bootstrap' => ['log'],
'components' => [
'log' => [
'targets' => [
[
'class' => 'yii\log\FileTarget',
'exportInterval' => 1,
],
],
'flushInterval' => 1,
],
],
];
It makes each log message appearing immediately in logs.
I already use yii2-file-kit. And now I want to resize my thumbnail by glide but I'be got error in log
"NetworkError: 500 Internal Server Error - http://storage.local/cache/1/iApQj79NQCji2TWsLZppiCQ8lbdgAPBz.jpg?w=100"
Here is my config
'components' => [
'cache' => [
'class' => 'yii\caching\DummyCache',
],
'fileStorage' => [
'class' => '\trntv\filekit\Storage',
'baseUrl' => '#storageUrl/source',
'filesystem' => [
'class' => 'common\components\filesystem\LocalFlysystemBuilder',
'path' => '#storage/web/source'
],
'as log' => [
'class' => 'common\behaviors\FileStorageLogBehavior',
'component' => 'fileStorage'
]
],
'glide' => [
'class' => 'trntv\glide\components\Glide',
'sourcePath' => '#storage/web/source',
'cachePath' => '#storage/cache',
'urlManager' => 'urlManagerStorage',
'maxImageSize' => 4000000,
'signKey' => 'pe4AJmRcBFbXfZvsk93VN'
],
And in my view
<?= Html::img(
Yii::$app->glide->createSignedUrl([
'glide/index',
'path' => $model->productAttachments[0]->path,
'w' => 100
], true),
['class' => 'article-thumb img-rounded pull-left']
) ?>
I just look at Starter-Kit config and there is the same config as I see. storage config are the same as yii2-starter-kit
Can you show debug for more information. Or you can output image by
Url::base(true).'/glide?path='.$path.'&w='.$w.'&h='.$h.'&fit=crop';