Write info message in app.log file using yii2 - yii2

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,

Related

yii2 how to view the logs of another .log file

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,
],
],
],

Codeception Object configuration must be an array

I am trying to run basic codeception test but what I get is the error bellow:
Backend\tests.functional Tests (1) -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
E LoginCest: Login user (0.01s)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1) LoginCest: Login user
Test tests/functional/LoginCest.php:loginUser
[yii\base\InvalidConfigException] Object configuration must be an array containing a "class" element.
Scenario Steps:
1. $I->amOnPage("/admin/user/login") at tests/functional/LoginCest.php:18
#1 /home/projects/toma/toma-coreshop/vendor/yiisoft/yii2/BaseYii.php:353
#2 /home/projects/toma/toma-coreshop/vendor/yiisoft/yii2/base/Module.php:427
#3 /home/projects/toma/toma-coreshop/vendor/yiisoft/yii2/base/Module.php:586
#4 /home/projects/toma/toma-coreshop/vendor/yiisoft/yii2/base/Module.php:522
#5 /home/projects/toma/toma-coreshop/vendor/yiisoft/yii2/web/Application.php:103
#6 /home/projects/toma/toma-coreshop/vendor/symfony/browser-kit/Client.php:351
#7 Codeception\Lib\InnerBrowser->amOnPage
#8 /home/projects/toma/toma-coreshop/backend/tests/_support/_generated/FunctionalTesterActions.php:526
#9 /home/projects/toma/toma-coreshop/backend/tests/functional/LoginCest.php:18
#10 backend\tests\functional\LoginCest->loginUser
Time: 251 ms, Memory: 16.00MB
There was 1 error:
---------
ERRORS!
Tests: 1, Assertions: 0, Errors: 1.
The errors flows one by one on every step whit those tests. My configuration file looks like (backend\codeception.yml):
namespace: backend\tests
actor: Tester
paths:
tests: tests
log: tests/_output
data: tests/_data
helpers: tests/_support
settings:
bootstrap: _bootstrap.php
colors: true
memory_limit: 1024M
modules:
config:
Yii2:
configFile: 'config/test.php'
In my test.php I have:
<?php
$config = yii\helpers\ArrayHelper::merge(
require(__DIR__ . '/main.php'),
require(__DIR__ . '/main-local.php'),
[
'id' => 'app-backend-test',
'components' => [
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=coreshop',
'username' => '*****',
'password' => '*****',
'charset' => 'utf8',
//'enableSchemaCache' => true,
],
]
]
);
return $config;
And finally my LoginCest.php:
/**
* Class LoginCest
*/
class LoginCest
{
/**
* #param FunctionalTester $I
*/
public function loginUser(FunctionalTester $I)
{
$I->amOnPage('/admin/user/login');
$I->fillField('Username', '***');
$I->fillField('Password', '***');
$I->click('Вход');
}
}
I followed the instruction given on the codeception site for the Yii2-advanced template. I am trying to test on old project so it is not newly installed Yii2-advanced if that matters in this case. Thank you!
EDIT
Main.php:
$params = array_merge(
require(__DIR__ . '/../../common/config/params.php'),
require(__DIR__ . '/../../common/config/params-local.php'),
require(__DIR__ . '/params.php'),
require(__DIR__ . '/params-local.php')
);
return [
'id' => 'app-backend',
'name' => 'Online Shop Admin',
'homeUrl' => '/admin',
'basePath' => dirname(__DIR__),
'controllerNamespace' => 'backend\controllers',
'language' => 'bg-BG',
'bootstrap' => [
[
'class' => 'app\components\LanguageSelector',
'supportedLanguages' => [],
],
'log',
'devicedetect'
],
'modules' => [
'user' => [
'controllerMap' => [
'security' => 'backend\controllers\user\SecurityController',
'admin' => 'backend\controllers\user\AdminController',
],
'modelMap' => [
'User' => 'backend\models\User',
'SettingsForm' => 'backend\models\SettingsForm',
'Profile' => 'backend\models\Profile',
/*'UserSearch' => 'backend\models\UserSearch',*/
],
'rememberFor' => 1209600,
],
'statistics' => [
'class' => 'backend\modules\statistics\Module',
],
'catalogue' => [
'class' => 'backend\modules\catalogue\Module',
],
'news' => [
'class' => 'backend\modules\news\Module',
],
'sales' => [
'class' => 'backend\modules\sales\Module',
],
'wasteManagment' => [
'class' => 'backend\modules\wasteManagment\WasteManagment',
],
'help' => [
'class' => 'backend\modules\help\HelpInfo',
],
'marketing' => [
'class' => 'backend\modules\marketing\Module',
],
'translation' => [
'class' => 'backend\modules\translation\Module',
],
'gii' => [
'class' => 'yii\gii\Module',
'allowedIPs' => ['127.0.0.1', '::1', '192.168.33.1'], // adjust this to your needs
'generators' => [
'crud' => [
'class' => 'backend\templates\crud_v1\Generator',
'templates' => ['mycrud' => '#backend/templates/crud_v1/default']
],
'model' => [
'class' => 'backend\templates\model_v1\Generator',
'templates' => ['mymodel' => '#backend/templates/model_v1/default']
],
/*'crud' => [
'class' => 'backend\templates\crud_module_v1\Generator',
'templates' => ['mycrudmodule' => '#backend/templates/crud_module_v1/default']
],
'model' => [
'class' => 'backend\templates\model_module_v1\Generator',
'templates' => ['mymodelmodule' => '#backend/templates/model_module_v1/default']
],*/
],
],
],
'components' => [
'assetManager' => [
'bundles' => [
'yii\bootstrap\BootstrapAsset' => [
'css' => [],
],
'yii\bootstrap\BootstrapPluginAsset' => [
'js' => []
],
'yii\web\JqueryAsset' => [
'js' => []
],
'wbraganca\dynamicform\DynamicFormAsset' => [
'js' => [
'/admin/dist/js/yii2-dynamic-form.js'
]
],
],
],
'urlManagerBackend' => [
'class' => 'yii\web\urlManager',
'baseUrl' => (isset($_SERVER['HTTPS']) ? "https" : 'http') . "://toma-coreshop.webbuild.com/admin",
'enablePrettyUrl' => true,
'showScriptName' => false,
],
'request' => [
'baseUrl' => '/admin',
'cookieValidationKey' => 'sew4789vb7834tvnv78',
'csrfParam' => '_backendCSRF',
],
'user' => [
'identityClass' => 'backend\models\User',
'enableAutoLogin' => true,
'loginUrl' => [ 'user/login'],
'identityCookie' => [
'name' => '_backendUser', // unique for backend
]
],
/*'session' => [
'name' => '_backendSessionId',
'savePath' => __DIR__ . '/../runtime/sessions',
],*/
'view' => [
'theme' => [
'pathMap' => [
'#dektrium/user/views' => '#backend/views/user',
'#dektrium/rbac/views' => '#backend/views/rbac',
],
],
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
],
'translate' => [
'class' => 'backend\components\Translate'
],
'tabSettings' => [
'class' => 'backend\components\TabSettings'
],
'system' => [
'class' => 'backend\components\System'
],
'moneyConvertor' => [
'class' => 'backend\components\MoneyConvertor'
],
'moneyWords' => [
'class' => 'backend\components\MoneyWords'
],
'OutData' => [
'class' => 'backend\components\OutData'
],
'MakeURL' => [
'class' => 'backend\components\MakeURL'
],
'makeUrl' => [
'class' => 'frontend\components\MakeURL'
],
'image' => [
'class' => 'backend\components\Image'
],
'formatter' => [
'class' => 'yii\i18n\Formatter',
'nullDisplay' => '',
],
'devicedetect' => [
'class' => 'alexandernst\devicedetect\DeviceDetect'
],
'errorHandler' => [
'errorAction' => 'site/error',
],
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'enableStrictParsing' => false,
'rules' => [
'profile' => 'user/settings/profile',
],
],
'i18n' => [
'translations' => [
'app*' => [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '#backend/messages',
'sourceLanguage' => 'bg',
'fileMap' => [
'app' => 'app.php',
],
],
'help*' => [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '#backend/modules/help/messages',
'sourceLanguage' => 'bg',
'fileMap' => [
'help' => 'help.php',
],
],
'sales*' => [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '#backend/modules/sales/messages',
'sourceLanguage' => 'bg',
'fileMap' => [
'catalogue' => 'sales.php',
],
],
'statistics*' => [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '#backend/modules/statistics/messages',
'sourceLanguage' => 'bg',
'fileMap' => [
'catalogue' => 'statistics.php',
],
],
'catalogue*' => [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '#backend/modules/catalogue/messages',
'sourceLanguage' => 'bg',
'fileMap' => [
'catalogue' => 'catalogue.php',
],
],
'marketing*' => [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '#backend/modules/marketing/messages',
'sourceLanguage' => 'bg',
'fileMap' => [
'catalogue' => 'news.php',
],
],
'news*' => [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '#backend/modules/news/messages',
'sourceLanguage' => 'bg',
'fileMap' => [
'catalogue' => 'news.php',
],
],
'message*' => [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '#backend/modules/translation/messages',
'sourceLanguage' => 'bg',
'fileMap' => [
'message' => 'message.php',
],
],
'kvgrid*' => [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '#backend/messages/kvgrid',
'sourceLanguage' => 'bg',
'fileMap' => [
'kvgrid' => 'kvgrid.php',
],
],
],
],
],
'params' => $params,
];
Main-local.php:
$config = [
'components' => [
'request' => [
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
'cookieValidationKey' => 'ud_6581BcmdaOC-RoK4LznBhsD7W4pyU',
],
],
'container' => [
'definitions' => [
kartik\grid\GridView::class => [
'krajeeDialogSettings' => ['overrideYiiConfirm' => false],
'responsiveWrap' => false,
'bordered' => false,
'tableOptions' => [
'class' => 'table full-color-table full-dark-table hover-table',
],
'resizableColumns' => true,
'hover' => true,
'layout' => "{items}\n{summary}\n{pager}",
'rowOptions' => function ($model) {
if(!empty($model->id))
return ['data-id' => $model->id];
else
return;
},
],
],
],
];
if (YII_ENV_DEV) {
// configuration adjustments for 'dev' environment
$config['bootstrap'][] = 'debug';
$config['modules']['debug'] = [
'class' => 'yii\debug\Module',
];
$config['modules']['debug']['allowedIPs'] = ['*'];
$config['bootstrap'][] = 'gii';
$config['modules']['gii'] = [
'class' => 'yii\gii\Module',
];
$config['modules']['gii']['allowedIPs'] = ['*'];
}
return $config;

installing yii2-rbac error You have wrong authManager configuration

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'],
],
...

Log not working in yii2

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.

How can I use Yii2 Glide

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';