How customise log file on Yii Framework? - php

I am trying to handle errors on my website. I logs each error in my log files, but I would like to know how to customize the message that is recorded in the log file.
For now, I get this message:
2013/09/30 10:08:59 [error] [exception.CException] exception
'CException' with message 'Error test' in
myDomain.com\protected\controllers\SiteController.php:234
Can you help me?

For manual customization log messages you need to create your own LogRoute class. In your situation you need inherit class from CFileLogRoute and override method formatLogMessage (such in example):
class MyFileLogRoute extends CFileLogRoute{
protected function formatLogMessage($message,$level,$category,$time)
{
//enter code here your custom message format
return #date('Y/m/d H:i:s',$time)." [$level] [$category] $message\n";
}
}
Than configure your configuration file:
'log' => array(
'class' => 'CLogRouter',
'routes' => array(
array(
'class' => 'MyFileLogRoute',
'levels' => 'error, warning, info',
'categories' => 'application.*',
'logPath' => dirname(__FILE__).'/../../../../../logs/',
),
...
And yes, #ragingprodigy is right: you can set define('YII_DEBUG', 0) or define('YII_TRACE_LEVEL', 0) in index.php to remove stack trace from log messages

Related

yii2 extract messages to db

I'm using trntv/Yii2-starter-kit. How can I extract messages to DB?
My config:
'*'=> [
'class' => 'yii\i18n\DbMessageSource',
'sourceMessageTable'=>'{{%i18n_source_message}}',
'messageTable'=>'{{%i18n_message}}',
'enableCaching' => YII_ENV_DEV,
'cachingDuration' => 3600,
'on missingTranslation' => ['\backend\modules\i18n\Module', 'missingTranslation']
]
My I18N file:
'sourcePath'=>Yii::getAlias('#base'),
'languages' => ['uz','ru'],
'translator' => 'Yii::t',
'sort' => false,
'removeUnused' => true,
'only' => [
'*.php',
],
'ignoreCategories' => ['yii'],
I tryed:
php yii message #common/config/messages/_base.php
And
php yii message
But always it writes all messages to files: vendor/yiisoft/yii2/messages. How can I export messages to DB? Has anyone help?
You need to use the following as per CONSOLE-DOCS there is a ExtendedMessageControler class. This controller extends default MessageController to provide some useful actions:
To migrate messages between different message sources run the common like below
php console/yii message/migrate #common/config/messages/php.php #common/config/messages/db.php
Which means you should have a file inside the #common/confiog/messages/ folder with the name db.php that will be used to create the message and source_message tables the contents of the file should be
<?php
return \yii\helpers\ArrayHelper::merge(
require(__DIR__ . '/_base.php'),
[
// 'db' output format is for saving messages to database.
'format' => 'db',
// Connection component to use. Optional.
'db' => 'db',
// Custom source message table. Optional.
'sourceMessageTable' => '{{%i18n_source_message}}',
// Custom name for translation message table. Optional.
'messageTable' => '{{%i18n_message}}',
]
);
and the messages source directory will be determined by the php.php file inside the #common/config/messages directory that contains the following
<?php
return \yii\helpers\ArrayHelper::merge(
require(__DIR__ . '/_base.php'),
[
// 'php' output format is for saving messages to php files.
'format' => 'php',
// Root directory containing message translations.
'messagePath' => Yii::getAlias('#common/messages'),
// boolean, whether the message file should be overwritten with the merged messages
'overwrite' => true,
]
);
You just need to run the migration command and the tables will be created and the messages will be exported to the respective tables.
for more details see the SOURCE for the actionMigrate().

How call components in Yii 1.1

I have some component I want to make it separate currently my components looks like
-protected/components
-GeneralFunction.php
-CustomFunction.php
and my config I have called:
'components' => array(
'general' => array('class' => 'GeneralFunction'),
'custom' => array('class' => 'CustomFunction'),
),
Above code is working fine But I want to separate frontend and backend of my components like:
-protected/components
-frontend
-GeneralFunction.php
-CustomFunction.php
-backend
-GeneralFunction.php
-CustomFunction.php
and my config I am calling:
'components' => array(
'general2' => array('class' => 'frontend.GeneralFunction'),
),
TestController.php
function actionTestComponent(){
echo Yii::app()->general2->test(); exit;
}
I am getting this error message:
2017/12/19 11:17:54 [error] [exception.CException] CException: Alias "frontend.GeneralFunction" is invalid. Make sure it points to an existing directory or file. in C:\xampp\htdocs\yii\framework\YiiBase.php:348
Please help me..
After lots of research I found this:
'general2' => array('class' => 'application.components.frontend.GeneralFunction'),

Log Sql errors Cakephp

I am using a Plugin to log the errors and warnings in a Cakephp-Application, and it is working as it supposed, but I was thinknig ,if the Connection with my DB is not working well, then still I need to log these errors , but this time in an external file,
The Idea should be easy, I call CaeLog::write() and in these Method(wich has been reimplemented by the Plugin) I would put "try/Catch", which would help me in the case an Exception is thrown, to write the message to a logfile.
My bootstrap.php looks like this :`
App::uses('CakeLog', 'Log');
CakeLog::config('debug', array(
'engine' => 'File',
'types' => array('notice', 'info', 'debug'),
'file' => 'debug',
));
CakeLog::config('fileLog', array(
'engine' => 'File',
'types' => array('fileLog'),
'file' => 'fileLog',
'scopes' => array('fileLog')
));
CakeLog::config('default', array(
'engine' => 'DatabaseLogger.DatabaseLog',
'types' => array('warning', 'error', 'critical', 'alert', 'emergency')`
In the write methode i have this Code
function write($type, $message){
try {
//Save In Db
} catch (Exception $exc) {
//If the save in DB fails call log in fileLog(which has been defined in bootstrap.php)
CakeLog::write('fileLog', "***************************\n".$exc."\n***************************\n");
}
}
The problem now : I am getting a loop, cause of the call in the catch block, which is trying again to save in db, how could i solve this without a loop?

Yii don't understand how to log a message

I followed this tutorial but still I go to the webpage refresh, then go to the file and still I can't find the log message I sent
I wrote the following line in a controller:
Yii::log("Index Checkout",CLogger::LEVEL_ERROR);
And My configurations:
'log' => array(
'class' => 'CLogRouter',
'routes' => array(
array(
'logFile'=>'trace.log',
'class' => 'CFileLogRoute',
'levels' => 'error,info, warning',
),
// uncomment the following to show log messages on web pages
/*
array(
'class'=>'CWebLogRoute',
),
*/
),
Right way to write to log is:
Yii::log($message, $level, $category);
But point is that $category should not be empty.
Example above works becouse message is written in category then category is not empty, but it writes empty message. It writes category so it looks like message.. but it isn't.
I was in a similar trouble with YII logger. Strange but I was kind of messing with parameter order.
This works for me:
<?php
Yii::log('', CLogger::LEVEL_ERROR, 'Message Here...');

Fatal Error when loading Catalog-settings in backend

On a fresh 1.5.0.1 Magento install when choosing Catalog from the settings->settings menu I get the following error:
Fatal error: Undefined class constant 'RANGE_CALCULATION_AUTO' in
/my-install-dir/app/code/core/Mage/Adminhtml/Model/System/Config/Source/Price/Step.php on line 33
Checked Step.php and it does not look damaged and contains the following:
class Mage_Adminhtml_Model_System_Config_Source_Price_Step
{
public function toOptionArray()
{
return array(
array(
'value' => Mage_Catalog_Model_Layer_Filter_Price::RANGE_CALCULATION_AUTO,
'label' => Mage::helper('adminhtml')->__('Automatic')
),
array(
'value' => Mage_Catalog_Model_Layer_Filter_Price::RANGE_CALCULATION_MANUAL,
'label' => Mage::helper('adminhtml')->__('Manual')
),
);
}
}`
Anyone know this error or how to fix it?
PHP is complaining that it can't find the constant on RANGE_CALCULATION_AUTO defined on the class Mage_Catalog_Model_Layer_Filter_Price
Based on your comments above, it sounds like you already checked the file at
app/code/core/Mage/Catalog/Model/Layer/Filter/Price.php
to ensure is had the correct constant defined.
const RANGE_CALCULATION_AUTO = 'auto';
Based on that, my guess would be there's a different Price.php being loaded for this class. This can happen if
Someone's placed a different version in community or local
Someone's monkied with the include path beyond Magento's normal monkey business
Check for files at
app/community/core/Mage/Catalog/Model/Layer/Filter/Price.php
app/local/core/Mage/Catalog/Model/Layer/Filter/Price.php
If that doesn't work, add some temporary debugging code to
app/code/core/Mage/Adminhtml/Model/System/Config/Source/Price/Step.php
that uses reflection to figure out what file PHP is loading the class from
class Mage_Adminhtml_Model_System_Config_Source_Price_Step
{
public function toOptionArray()
{
//NEW LINES HERE
$r = new ReflectionClass('Mage_Catalog_Model_Layer_Filter_Price');
var_dump($r->getFileName());
//echo $r->getFileName(); // if too long for var_dump
exit("Bailing at line ".__LINE__." in ".__FILE__);
//END NEW LINES
return array(
array(
'value' => Mage_Catalog_Model_Layer_Filter_Price::RANGE_CALCULATION_AUTO,
'label' => Mage::helper('adminhtml')->__('Automatic')
),
array(
'value' => Mage_Catalog_Model_Layer_Filter_Price::RANGE_CALCULATION_MANUAL,
'label' => Mage::helper('adminhtml')->__('Manual')
),
);
}
}`
This will dump out a file path that points to the exact place PHP is loading the class from, which should get you where you need to go.

Categories