My goal is know all steps what my app execute. Example:
Method1() -> Method2() -> Method3()
How can I achieve this ? I logging everythin: my log looks like:
'routes' => [
[
'class' => 'CFileLogRoute',
'levels' => 'error, warning, trace, info, profile',
'categories' => 'system.*',
],
Bet all still not have methods names in log.
To improve profiling exist issue to increse trace level to:
define('YII_TRACE_LEVEL',3);
That will allow to get file line numbers where method call. In logs you will some thins like:
UserController.php 78 line 'SELECT * FROM `users`'
Related
i have a little problem.
I'm developing a php application using Yii2 framework and i want to save a log messagges into db table.
I'm coding my own component witch extends DbTarget, in this component i rewrite export() function to save data into my table. It's works fine, but i can't get the log message.
For example, when i call Yii:log('message log'), all my data are saved in my db except 'message log' because i don't know how to get this value in my component.
Any solutions?
thanks
P.s. I'm newbee with yii2 and i have read the official documentation, but i didn't find any solution.
It seems you should specify level of the message
Yii:log('message log', Logger::LEVEL_TRACE);
or use shortcut methods
Yii::info('message log');
Yii::trace('message log');
Yii::error('message log');
Check your config for another targets. May be your message goes to level or category of another target. Notice that default category is 'application'.
To be shure you can make this configuration
'components' => [
'log' => [
'targets' => [
[
'class' => 'YourDbTarget',
'levels' => ['info'],
'categories' => ['application'],
],
],
],
],
And try to log info message
Yii::info('message log'); // target = info, category = application
I'm tightening up the logging for our Yii2 application and are struggling with getting the Logger to actually write messages to a log file when dealing with specific categories.
Our project consists of the following Yii2 'Applications' : console, common, frontend, backend and the logging for each of these components work fine for Exceptions generated by Yii and general PHP Exceptions. However when I add some info messages for a specific category in the console part (which I execute by running commands via SHH) the directory that the file should be in is created but the file itself is not. I see that the newly specified log messages are inside the correct 'FileTarget' when I do a var_dump of it.
I've set the flushInterval of the 'log' component to 1 and the exportInterval of the logTarget to 1 to make sure that the messages are to be written to the targets directly. I'm aware that this should be set to a larger value at some point, but for now I want to make sure that the logs are actually being saved. Changing the interval values doesn't seem to have any effect.
A workaround I came up with is to manually call target->export() for the specific FileTarget. This is how I currently make sure logs are being written:
In the Controller where I want to log message I do
Yii::info('Report created succesfully.', 'reports-create');
UtilitiesController::forceLogExport('reports-create');
The forceLogExport method does this:
public function forceLogExport($categoryName = ''){
$logger = \Yii::getLogger();
foreach($logger->dispatcher->targets as $target){
if(!empty($categoryName)){
foreach($target->categories as $category){
if($category == $categoryName){
$target->export();
}
}
}
}
$logger->flush(true);
}
This does actually write the logs to the .log file, however there seem to be duplicate entries in the log now and it feels just plain wrong to call an export after every message.
As I read in the docs the actual writing of logs only happens when either these intervals are met OR when the application ends. I could imagine the application not ending properly so I tried forcing this by calling Yii:$app->end() directly after logging the message, but the file stays empty.
Since the problem is neither the application not ending (I can see that it does end) nor the interval being met (I see at least one message in the target and the interval is set to 1) I'm kind of at my wit's end why the logs are not exported. If anyone could perhaps elaborate any further on when/where Yii calls the ->export() method itself that would be a big help.
EDIT: added the console/config/main.php settings. Slightly changed my story, initially I wrote that 'the log file was being created but the messages were not being written in it.' but in fact only the directory that should contain the files was created, not the log file itself.
<?php
return [
'id' => 'console',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'controllerNamespace' => 'console\controllers',
'components' => [
'user' => [
'class' => 'yii\web\User',
'identityClass' => 'app\models\User',
//'enableAutoLogin' => true,
],
'session' => [ // for use session in console application
'class' => 'yii\web\Session'
],
'log' => [
'flushInterval' => 1,
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error','info', 'warning'],
'categories' => ['reports-schedule'],
'exportInterval' => 1,
'logVars' => [null],
'logFile' => '#app/../logs/console/'. date('Y') . '/' . date('m') . '/reports-schedule/' . 'reports-schedule_' . date('d-m-Y') . '.log'
],
[
'class' => 'yii\log\FileTarget',
'levels' => ['error','info', 'warning'],
'categories' => ['reports-create'],
'exportInterval' => 1,
'logVars' => [null],
'logFile' => '#app/../logs/console/'. date('Y') . '/' . date('m') . '/reports-create/' . 'reports-create_' . date('d-m-Y') . '.log'
],
// and 6 other similarly structured targets
[...],
[...],
[...],
[...],
[...],
[...],
],
]
];
UPDATE: it seems like I'll have to stick to manually calling ->export() on the desired log target. The problem I had with duplicate log entries (same message, same timestamp) being written was due to the fact that I had set the exportInterval property to 1 for the target (which I initially did to make sure the messages were exported at all). I suppose the logger stores messages until the value of exportInterval is met and then expect those messages to be written to the targets. I fixed duplicate entries from being written by removed the exportInterval property so Yii takes the default value (1000). For my case this would be sufficient since I wouldn't run into those numbers but for anyone reading this please consider your use case and check if you would run into anything close to that in a single cycle.
I had a similar issue at some point and I think this should be sufficient enough for your case.
Yii::$app->log->targets['reports-schedule']->logFile = $logPath;
Yii::info($message, $category);
Yii::getLogger()->flush();
This way you can specify a dynamic log path inside your schedule target and after flush the logger continues as it should.
I have the following config for logs:
'log' => [
'traceLevel' => getenv('YII_DEBUG') ? getenv('YII_TRACELEVEL') : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'except' => ['yii\db*'],
'levels' => ['profile', 'trace', 'info', 'warning', 'error'],
'logFile' => '#app/log/app.log',
'logVars' => [],
'maxFileSize' => 1024 * 20,
],
],
],
but when I call all the different level logs I can't seem to get traces to output to my app.log.
here's where we enter my code just as an example of how I'm calling the logs:
public function actionCreate() {
\Yii::trace("trace");
\Yii::info("info");
\Yii::warning("warning");
\Yii::error("error");
return;
and the output I find in app.log:
2016-10-24 19:15:58 [127.0.0.1][-][-][info][application] info
2016-10-24 19:15:58 [127.0.0.1][-][-][warning][application] warning
2016-10-24 19:15:58 [127.0.0.1][-][-][error][application] error
I've played around with adding/removing the levels from the level list, as well as outputting to codemix\streamlog\Targets php://stdout and php://stderr, and everything but trace level logs appear to work as you'd expect from the config. It looks like I'm doing it right according to the documentation found in the Definitive Yii2 guide.
Maybe I'm missing something in my target? I saw someone had a similar problem here but they were simply missing the trace in their targets levels list and haven't responded on whether they solved their problem or not. I'd really appreciate any advice you can offer.
Do you have YII_DEBUG defined in yourApp/web/index.php?
defined('YII_DEBUG') or define('YII_DEBUG', true);
Your code
'traceLevel' => getenv('YII_DEBUG') ? getenv('YII_TRACELEVEL') : 0,
is saying, that if YII_DEBUG is not defined, traceLevel would be 0. According to documentation, traceLevel public property is defined like this:
How much call stack information (file name and line number) should be
logged for each message. If it is greater than 0, at most that number
of call stacks will be logged. Note that only application call stacks
are counted.
Either set YII_DEBUG in yourApp/web/index.php, or change traceLevel property.
I'm using Yii 2 and I'm wondering if Yii has anything built in to handle generic error pages to show to users.
Like for example you may want to show them a general error page because their logout failed for some reason or a range of other reasons. Stuff that you don't want to have to create a view for every situation.
Is there something like this available and if so, how do you use it?
Both the basic and the advance apps come with it by default:
'components' => [
..................
'errorHandler' => [
'errorAction' => 'site/error',
],
..............
class SiteController extends Controller
............
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
You just have to throw an error now and you will see the page.
make sure YII_DEBUG is false.
if YII_DEBUG is true then error stack is displayed.
You can define YII_DEBUG By
1) index.php
//remove the following lines when in production mode
defined('YII_DEBUG') or define('YII_DEBUG', true);
//specify how many levels of call stack should be shown in each log message
defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',3);
OR
2).envFramework
YII_DEBUG = true
YII_ENV = dev
I use the module user
http://yiiframework.com/extension/yii-user/
then open localhost/testdrive and get an error - trying to get property of non-object
on line - array('url'=>Yii::app()->getModule('user')->loginUrl,
did everything according to instructions from the link
Maybe you should add 'class' parameter to 'user' to configuration in main.php, ie:
'components'=>array(
'user' => array(
'class' => 'application.components.WebUser',
'allowAutoLogin' => true,
'loginUrl' => array('/ua/user/login'),
)...
I figured out why this was happening to me, too. I had put the block at the end of main.php. If you put it at the beginning, the problem goes away.