The Yii2 debugger seems to only work for web requests. How can I debug console commands (CLI)?
Eg. I need to see the SQL statements that were executed during a console command...
Use logger:
'log' => [
'targets' => [[
...
], [
'class' => 'yii\log\FileTarget',
'logFile' => '#runtime/logs/profile.log',
'logVars' => [],
'levels' => ['profile'],
'categories' => ['yii\db\Command::query'],
'prefix' => function($message) {
return '';
}
]]
]
https://github.com/achertovsky/yii2-debug-cli
i wrote extension to existing yii2-debug to achieve the result. Please, use and lmk issues if you find some.
In the Yii2 default application advanced template, you already have the log file target setup/enabled. However, it is only enabled for 'error' and 'warning' conditions. To include for info, add info as below in the console/config/main.php
[
'class' => 'yii\log\FileTarget',
'levels' => ['info', 'error', 'warning'],
],
The log output should be in console/runtime/logs/app.log
Related
I use the Yii component "log" to log some important info. In the targets I have:
[
'class' => FileTarget::class,
'categories' => ['import.category'],
'levels' => ['info', 'warning', 'error'],
'logFile' => '#runtime/logs/import/import.log',
'maxFileSize' => 10240,
'logVars' => []
]
I call the logger in this way:
Yii::warning('some message', 'import.category');
I have a record in logs/import/import.log. That is good. But I have the same record in logs/app.log.
I do not need the record in logs/app.log. Is this by default? Can I turn it off?
By the way, if I use:
Yii::info('some message', 'import.category');
I have a record only in logs/import/import.log, but an error or warning duplicates the record.
The configuration for your app.log target probably looks like this:
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
'logFile' => '#runtime/logs/app.log',
],
Because no categories are specified, all error and warning messages are written there. To exclude category from getting logged in target there is $except property in yii\log\Target which is common parent for log targets. So you want to modify your setting for app.log target like this:
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
'logFile' => '#runtime/logs/app.log',
'except' => ['import.category'],
],
I am using Yii2 to work on a project that needs to be translated into various languages. I am using the advanced template and set up my common/main.php like so
return [
'vendorPath' => dirname(dirname(__DIR__)) . '/vendor',
'language' => 'en',
'sourceLanguage' => 'en',
'components' => [
'i18n' => [
'translations' => [
'app' => [
'class' => 'yii\i18n\DbMessageSource',
'sourceLanguage' => 'en',
],
],
],
'cache' => [
'class' => 'yii\caching\FileCache',
],
],
'as beforeRequest' => [
'class' => 'common\components\CheckLanguage',
],
];
I have added the required database tables source_message and message. Now each text in the website that I need to have translated I use the Yii::t($category,$message) function.
My question is; How can I get a list of all text in my website that needs to be translated into the database? Do I have to manually scan my site for all Yii::t($category,$message) function calls?
Thanks in advance for your time and input
There is console command provided for this scan functionality.
./yii message
See the documentation in the Guide.
Generate configuration file for the scanner:
./yii message/config-template your/path/to/saving/config.php
Adjust the newly created config.php to your needs.
Run the command:
./yii message path/to/config.php
I am currently building a second system with yii2. It will use some tables from a Yii1 database for translation. The Yii1 project was originally translated using files but this has now been moved to the db. All the translations of the Yii1 system use one of three categories app,flash,email of which the vast majority use app.
In the Yii2 project I have the following in the web.php config file.
'i18n' => [
'translations' => [
'*' => [
'class' => 'yii\i18n\DbMessageSource',
'db' => 'cdb',
'sourceMessageTable' => 'translation_source',
'messageTable' => 'translation',
'forceTranslation'=>true,
],
],
],
All of the translations on the system that use app are not translated, however, all other categories are. If I change the above code to
'i18n' => [
'translations' => [
'app*' => [
Then I get an error for other categories but not app and the app strings are translated as expected. The error I get is
Unable to locate message source for category 'flash'.
If however I change my config to the following, this works for all translations.
'i18n' => [
'translations' => [
'*' => [
'class' => 'yii\i18n\DbMessageSource',
'db' => 'cdb',
'sourceMessageTable' => 'translation_source',
'messageTable' => 'translation',
'forceTranslation' => true,
],
'app*' => [
'class' => 'yii\i18n\DbMessageSource',
'db' => 'cdb',
'sourceMessageTable'=>'translation_source',
'messageTable' => 'translation',
'forceTranslation' => true,
],
],
],
It just seems odd that I am having to include effectively the same code block twice. Can anyone tell me how I can achieve this in one array or is this the expected behaviour?
** Update **
I do see some mention of * in the docs Docs. I also see some mention of this in the Forum
Module section config
'user' => [
'class' => 'dektrium\user\Module',
'modelMap' => [
'User' => 'app\models\DL\User',
'registrationForm' => 'app\models\DL\registrationForm',
],
'controllerMap' => [
/*'registration' => 'app\controllers\user\RegistrationController',
'admin' => 'app\controllers\user\AdminController'*/
],
'layout' => '#app/views/layouts/container',
'defaultRoute' => 'profile',
'admins' => ['admin'],
'enableFlashMessages' => false,
'params' => [
'menuItems' => [
'label' => 'Users',
'url' => ['/user/admin']
]
]
],
Yii console application (./yii) showing me error
'Calling unknown method:
app\controllers\user\AdminController::getHelpSummary()'
If I uncomment the controllerMap section, I can't understand why it autoloads in console app if my AdminController extends web controller not console.
This is commands from user module.
Do you really need the user module in console?
Yii2 console and web applications have separated configuration files by default. If you changed this default and use the same config for both of them, you must take care about consistency.
You can check the list of loaded configs in ./yii.
You need to specify a valid defaultRoute for the console application.
With 'defaultRoute' => 'profile', ./yiimay try to load a Controller which requires the user module.
Try adding it in the console configuration.
I am using Yii2 for a project. I have a class for consuming a third party service. This class has two methods sendRequest and processResponse. I would like to maintain separate logs for payload in sendRequest before actually sending it and another log for the raw response data received in processResponse before doing any processing. Additionally I would like log rotation on both logs as the files may grow indefinitely and want both files to be separate from the default app.log. Is this possible? How may I implement this using Yii2 APIs?
I eventually reverted back to using Yii2 logger by adding 2 additional file targets in my #app/config/main.php. The file targets had categories = ['orders'] and ['pushNotifications'] respectively so that in my code I use:
Yii::info($message, 'pushNotifications');
or
Yii::info($message, 'orders');
Here is my log config:
'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/Orders/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,
],
],
],
Since I wasn't quite sure how to configure Yii2 logger to do what I wanted, and googling the subject wasn't much help I decided to go with a third-party logger. The one I chose was Monolog. This functionality was only needed in one class so I create a static getLogger method which returned an instance of Monolog\Logger.
public static function getLogger($name) {
$logger = new \Monolog\Logger($name);
$logger->pushHandler(new \Monolog\Handlers\RotatingFileHandle(Yii::getAlias("#app/runtime/logs/$name.log")), \Monolog\Logger::INFO);
return $logger;
}
Then in sendRequest method I use:
static::getLogger('orders')->info($outgoingXmlPayload.$curlResponseXml);
In the processResponse method I use:
static::getLogger('pushNotifications')->info($notificationXml);
I will be glad to hear(or read) from anyone who has a better solution still. Thanks.
--Ab
Asterisk * may also come handy if we need to collect more subcategories into a cummulative file:
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'flushInterval' => 100, // may prevent from memory exhaustion
'targets' => [
[
'class' => 'yii\log\FileTarget',
'categories' => ['eshop1*'],
'logFile' => '#app/runtime/logs/eshop1.log',
'logVars' => [], // don't log global vars
],
[
'class' => 'yii\log\FileTarget',
'categories' => ['eshop2*'],
'logFile' => '#app/runtime/logs/eshop2.log',
'logVars' => ['GET', 'POST'], // log some globals
'exportInterval' => 100, // may prevent from memory exhaust
],
],
],
This work for me, for dev log
'components' => [
'log' => [
'traceLevel' => YII_DEBUG ? 10 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'logFile' => '#runtime/logs/dev.log',
'categories' => ['dev'],
'levels' => ['trace'],
],
],
],
it's used
Yii::debug('log step 1', 'dev');