Logging the requests,response and errors in Yii 1.1.15 - php

How to log all the requests and their corresponding responses in Yii, I tried to log the requests in afterAction, but how to get the responses in afterAction. I also need to log all the errors or exceptions.

You can use afterRender function to get the rendered output. The $output parameter gives you the rendered output. But I think this works only when you have used render function in your controller actions.
Add following to your components configuration in your config file to log errors and warnings
'log'=>array(
'class'=>'CLogRouter',
'routes'=>array(
array(
'class'=>'CFileLogRoute',
'levels'=>'error, warning',
),
),

Related

Yii: How do I find out which views, controllers, models and styles are being used in a page?

I'm very new to Yii, and I've been asked to take a look at a project, and see if I can add anything.
So to start off, I wanted to find out what files are in play for a certain page, i.e., what Controller, what View, what Model, etc. A friend, who dabbles in Yii told me it can usually be found via the URL itself, like this:
Sample: localhost/project/index.php?r=site/index
Site is the Controller, index is the Action
However, the project I saw returns URLs like so:
localhost/cdforum/web/index.php/forum/view/id/1
To which my friend said "the htaccess must've been modified". We assumed the Controller is forum, and the Action is view
We're not exactly sure if that's accurate. And given a project directory like so:
I'm not exactly sure what to look for. So I'd like to ask, for the URL above, is there a way to tell which files are responsible for the output?
You can usually get that from url but not necessarily because routing is defined in your config/main.php file in this part of it:
array(
......
'components'=>array(
......
'urlManager'=>array(
'urlFormat'=>'path',
'rules'=>array(
'pattern1'=>'route1',
'pattern2'=>'route2',
'pattern3'=>'route3',
),
),
),
);
Check the rules key of this array is the pattern the url is going to have so your pattern will look like
'forum/view/...' => 'the/real/url'
then stuff before first backslash is the controller and the second is the action. In that action you will be able to find what models are used.
Hope it helps
I suggest you to familiarize with this wiki page
http://www.yiiframework.com/wiki/249/understanding-the-view-rendering-flow/
Then, you can display trace log on your pages:
config/main.php
'log'=>array(
(...)
'routes'=>array(
(...)
array(
'class'=>'CWebLogRoute',
'levels'=>'trace',
),
),
),

Trying to get property on a non object Yii 1.1.15

I try to change Css file for CGridView widget, in my config/main.php:
'components' => array(
'widgetFactory' => array(
'widgets' => array(
'CGridView' => array(
'cssFile' => Yii::app()->request->baseUrl . '/css/gridview.css',
),
),
),
...
And I get warning:
Trying to get property on a non object /path_to_project/protected/config/main.php on line 79
How I can suppress this warning, and why I getting it, in when I using it in view files it all works.
P.S. Yes I can set display_errors ini set to false and message will dissapearm but I want get clearly with it. Thanks!
The reason for the warning is that the CHttpRequest object Yii::app()->request has not been instantiated yet.
From the API page for CApplication:
CApplication will undergo the following lifecycles when processing a
user request:
load application configuration;
set up error handling;
load static application components;
onBeginRequest: preprocess the user request;
processRequest: process the user request;
onEndRequest: postprocess the user request;
Starting from lifecycle 3, if a PHP error or an uncaught exception occurs, the application will switch to its error handling logic and jump to step 6 afterwards.
Your error is happening at the first step. As such, Yii's error handling has not been setup yet. The only option is to suppress this warning using the # operator:
'cssFile' => #Yii::app()->request->baseUrl . '/css/gridview.css',
HOWEVER
This is a terrible idea, since you are essentially hiding the error instead of fixing it.
If your views are being displayed correctly (no css errors), you can omit the Yii::app()->request->baseUrl and just use:
'cssFile' => '/css/gridview.css'
If you are experiencing errors, you can create a class in your components folder that extends CWidgetFactory and set any variables that depend on other components here e.g
class MyWidgetFactory extends CWidgetFactory {
public function init() {
parent::init();
$this->widgets['CGridView']['cssFile'] = Yii::app()->request->baseUrl.'css/gridview.css';
}
}
You will need to adjust your components to use this file:
'components' => array(
'widgetFactory' => array(
'class' => 'MyWidgetFactory'
...

Yii not showing any error

Can't make Yii to show error to webbrowser. Configured custom error handler in config:
'errorHandler' => array(
// use 'site/error' action to display errors
'errorAction' => 'site/error',
),
But all errors only got written to application.log and then Apache servers empty page with error 500. How to make Yii print errors to the screen?
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);
You should a little bit modify your config/main.php. There is an item errorHandler. It should look like this:
'errorHandler' => [
// use 'site/error' action to display errors
'errorAction' => YII_DEBUG ? null : 'site/error',
],
This happened due to increased priority of CErrorHandler::$errorAction. Here is discussion: https://github.com/yiisoft/yii/issues/3760
It might be more of an Apache setting. What you have is correct.
Mine also has this one.
defined('YII_DEBUG_SHOW_PROFILER') or define('YII_DEBUG_SHOW_PROFILER',true);
You can also do this.
error_reporting(E_ALL);
ini_set("display_startup_errors","1");
ini_set("display_errors","1");
read runtime/logs/app.log
you can see any action and error that happens in you yii2 app.

How to display standard yii stacktrace?

'errorHandler' => array(
'class' => 'ErrorHandler',
'errorAction' => 'page/find',
),
http://shot.qip.ru/008pAk-4IA4wMhU6/
I have standard error handling with beautiful error page. But for develop environment I need standard stacktrace on it below.
Examlpe: http://shot.qip.ru/008pAk-4IA4wMhU7/
If I comment 'errorAction' I can see just standart stacktrace, in other case I cant display this stacktrace.
'log'=>array(
'class'=>'CLogRouter',
'routes'=>array(
array(
'class' => 'CWebLogRoute',
'categories' => 'application, exception.*',
'levels'=>'error, warning, trace, profile, info',
'showInFireBug' => true,
'enabled' => YII_DEBUG,
),
array(
'class'=>'ext.yii-debug-toolbar.YiiDebugToolbarRoute',
'ipFilters'=>array('127.0.0.1','192.168.0.100'),
),
array(
'class'=>'CProfileLogRoute',
'report'=>'summary',
// Shows the execution time of each labeled with a code block.
// The value of "report" can also be specified as a "callstack".
),
),
),
Error handler by default uses two types of views for
Production named as error.php;
Development named as named as exception.php;
Based on your routing and error handler code. I see you have defined a custom error action
You will have to place your custom Errors views in either of the following folders, in the format specified in the link below and use the standard error action.
themes/ThemeName/views/system: when a theme is active.
protected/views/system
See this Documentation for detailed explanation
Reference: http://www.yiiframework.com/doc/api/1.1/CErrorHandler
Try this extenstion http://www.yiiframework.com/extension/yii-debug-toolbar/
The Yii Debug Toolbar is a configurable set of panels that display various debug information about the current request/response and when clicked, display more details about the panel's content.
It is a ported to PHP famous Django Debug Toolbar.

Yii Db Profiler Not Working - 'enableprofiling'=>True

i have enabled profiling to be displayed in yii as below shown.
'db'=>array(
'connectionString'=>'pgsql:host=localhost;port=5432;dbname=ijob_css',
'username'=>'postgres',
'password'=>'allion123',
'enableProfiling'=>true,
'schemaCachingDuration'=>604800
),
but still i am not getting anything displayed under the pages of website and its empty.
before it worked and recently i swiotched off by
'enableProfiling'=>false
why doesn't it work ? i must be missing something.
Adding this to main config resolved the issue. 'class'=>'CProfileLogRoute',
'components'=>array(
'log'=>array(
'class'=>'CLogRouter',
'routes'=>array(
array(
'class'=>'CFileLogRoute',
'levels'=>'error, warning',
),
array(
'class'=>'CProfileLogRoute',
),

Categories