Showing generic error pages to users in Yii 2 - php

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

Related

How i can get log message with yii2

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

Yii2 Yii::trace never found in logs despite config values

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.

Styling ForbiddenHttpException error in yii2

When I get a ForbiddenHttpException its just ugly black on white text. It's not using the standard pretty Yii2 error from the 'views/site/error.php'
An Error occurred while handling another error: exception
'yii\web\ForbiddenHttpException' with message 'You are not allowed to
perform this action.' in
/vendor/yiisoft/yii2/filters/AccessControl.php:151
Config has:
'errorHandler' => [
'errorAction' => 'site/error',
],
Is it possible to style ALL Yii2 errors to look the same?
First of all check for errorHandler in you main config
'errorHandler' => [
'errorAction' => 'site/error',
],
If it is not there it will show u a plain text.
And for Access Control Custom Error handling you can set a denyCallback in access behaviour
'denyCallback' => function($rule, $action){
// Your Code Goes Here
}

Yii profiler get methods names

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

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.

Categories