Multiple loggers - php

I'd like to create multiple loggers where different areas of my app will log to different files. For example, all the classes associated with getting the users data would log to a user.log, all functionality of making purchases going to a purchase.log. I am using the configuration array method for setting up the logger & appenders. In my index.php:
require_once('log4php/Logger.php');
require_once('classB.php');
require_once('classA.php');
Logger::configure(array(
'rootLogger' => array('appenders' => array('default')),
'classALogger' => array('appenders' => array('classAAppender')),
'appenders' => array(
'default' => array(
'class' => 'LoggerAppenderEcho',
'layout' => array(
'class' => 'LoggerLayoutSimple'
)
),'classAAppender' => array(
'class' => 'LoggerAppenderFile',
'additivity' => false,
'layout' => array(
'class' => 'LoggerLayoutSimple'
),
'params' => array(
'file' => 'log/classA.log',
'append' => true
)
)
),
));
$logger = Logger::getLogger("main");
$logger->info('message from index' . '<br>');
$classA = new ClassA();
$classA->test();
Class A is as follows:
class ClassA
{
public function test()
{
$logger = Logger::getLogger("classALogger");
$logger->error('from ClassA');
}
}
I am able to log to the default or root logger but am not able to log to, in this example, classALogger. Any suggestions?

Related

Zend 2 Rotate logging

I use the Zend Log for logging information to my logfile. I am looking forward for a way to automatically write in monthly logfiles like logfile201601.log, logile201602.log etc.
Is there a way to to achieve that ? Any hints or a tutorial ? Didn't found anything like that on my current research.
My current configuration:
'factories' => array(
'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
'Zend\Log' => function ($sm) {
$logger = new Zend\Log\Logger(array(
'writers' => array(
'stream' => array(
'name' => 'stream',
'options' => array(
'stream' => './data/log/logfile2016.log',
'formatter' => array(
'name' => 'simple',
'options' => array(
'dateTimeFormat' => 'Y-m-d H:i:s'
)
)
)
)
)
));
return $logger;
},
);
Because Zend will automatically create the log file, you can just dynamically create your log file with the date() function:
// ...
'stream' => './data/log/logfile'.date("Ym").'.log',
// ...
Note that the log directory must be writable.

How to save XMLobject in memcahe in Zend Framwork2

Below is my part of code(in ZF2) where I am trying to save an XML object into Memcache, But I am unable to figure it out why its not setting up Complex obejct into Memcache, Any lead ?
$serviceManager = $event->getApplication()->getServiceManager();
$xmlAclConfigCache = $serviceManager->get('memcache')->getItem('xmlConfig');
if (empty($xmlAclConfigCache)) {
echo 'fresh ACL XML';
$xmlAclConfigCache = simplexml_load_file(__DIR__.'/config/acl.xml');
$serviceManager->get('memcache')->setItem('xmlConfig', $xmlAclConfigCache);
var_dump($xmlAclConfigCache);
} else {
echo 'OLD ACL XML';
}
Memcache config in cache.local.php
<?php
return array(
'caches' => array(
'memcache' => array( //can be called directly via SM in the name of 'memcached'
'adapter' => array(
'name' =>'memcache',
'ttl' => 7200,
'options' => array(
'servers' => array(
array(
'127.0.0.1',11211
)
),
'namespace' => 'MYMEMCACHEDNAMESPACE',
)
),
'plugins' => array(
'exception_handler' => array(
'throw_exceptions' => false
),
),
),
),
);

Invalid arguments for console cron route in Zend Framework 2

I'm trying to make a cron app in my zf2 project but it gave me always the following message :
Reason for failure: Invalid arguments or no arguments provided
My code is this:
module.config.php
'controllers' => array(
'invokables' => array(
'Sync\Controller\Cron' => 'Sync\Controller\CronController',
'Sync\Controller\Index' => 'Sync\Controller\IndexController'
),
),
'console' => array(
'router' => array(
'routes' => array(
'user-reset-password' => array(
'options' => array(
'route' => 'user resetpassword [--verbose|-v] <userEmail>',
'defaults' => array(
'controller' => 'Sync\Controller\Index',
'action' => 'password'
)
)
),
'cron' => array(
'options' => array(
'route' => 'cron [full|center]',
'defaults' => array(
'controller' => 'Sync\Controller\Cron',
'action' => 'full'
)
)
)
)
)
)
CronController.php
class CronController extends AbstractActionController
{
public function fullAction()
{
$request = $this->getRequest();
if (!$request instanceof ConsoleRequest) {
throw new \RuntimeException('You can only use this action from a console!');
}
return("hi");
}
public function centerAction()
{
}
}
Given that a matching php binary is in the path you can then call from your application folder:
php public/index.php cron full
or just
php public/index.php cron (since the route defaults to the fullAction)

Yii returns exception.CHttpException.404 error

I have some problems with my Yii system. All modules are working fine in the system, but there is an error with the activity module. It returns the following error:
2013/10/22 10:21:17 [error] [exception.CHttpException.404] exception 'CHttpException' with message '"activity/default/list" isteği çözümlenemedi.' in /var/www/yii/framework/web/CWebApplication.php:286
Stack trace:
#0 /var/www/yii/framework/web/CWebApplication.php(141): CWebApplication->runController('activity/defaul...')
#1 /var/www/yii/framework/base/CApplication.php(180): CWebApplication->processRequest()
#2 /var/www/hello/index.php(13): CApplication->run()
#3 {main}
REQUEST_URI=/etkinlikler/liste
Here are my main and defaultController files for activity module.
main.php
<?php
date_default_timezone_set('Asia/Istanbul');
return array(
'basePath' => dirname(__FILE__) . DIRECTORY_SEPARATOR . '..',
'name' => 'Kendim Panel',
'language' => 'tr',
'preload' => array('log'),
'import' => array(
'application.models.*',
'application.modules.*',
'application.components.*',
'application.helpers.*'
),
'modules' => array(
'gii' => array(
'class' => 'system.gii.GiiModule',
'password' => '121212',
'ipFilters' => array('127.0.0.1, 192.168.1.27', '::1'),
),
'wlapi' => array(),
'panel' => array(),
'ileti' => array(),
'anket' => array(),
'hastag' => array(),
'category' => array(),
'product' => array(),
'menu' => array(),
'siparis' => array(),
'kisisel' => array(),
'istatistik' => array()
),
'components' => array(
'CString' => array('class'=>'CString'),
'myFunc' => array('class'=>'myFunc'),
'user' => array(
'allowAutoLogin' => true,
),
'db' => array(
'connectionString' => 'mysql:host=localhost;dbname=kendim_db',
'emulatePrepare' => true,
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
)
, 'urlManager' =>array(
'urlFormat' => 'path',
'showScriptName' => false,
'rules' => array(
'/' => 'panel/default',
'wl-api' => 'wlapi',
'etkinlikler' => 'activity',
'etkinlikler/ekle' => 'activity/default/create',
'etkinlikler/duzenle' => 'activity/default/update',
'etkinlikler/duzenle/id/<id:\d+>' => 'activity/default/update',
'etkinlikler/sil' => 'activity/default/delete',
'etkinlikler/sil/id/<id:\d+>' => 'activity/default/delete',
'etkinlikler/liste' => 'activity/default/list',
'kategoriler' => 'category',
'kategoriler/ekle' => 'category/default/create',
'kategoriler/duzenle' => 'cateogry/default/update',
'kategoriler/duzenle/id/<id:\d+>' => 'category/default/update',
'kategoriler/sil' => 'category/default/delete',
'kategoriler/sil/id/<id:\d+>' => 'category/default/delete',
'kategoriler/liste' => 'category/default/list',
'urunler' => 'product',
'urunler/ekle' => 'product/default/create',
'urunler/duzenle' => 'product/default/update',
'urunler/duzenle/id/<id:\d+>' => 'product/default/update',
'urunler/sil' => 'product/default/delete',
'urunler/sil/id/<id:\d+>' => 'product/default/delete',
'urunler/liste' => 'product/default/list',
),
),
'errorHandler' => array(
'errorAction' => 'panel/default/error',
),
'log' => array(
'class' => 'CLogRouter',
'routes' => array(
array(
'class' => 'CFileLogRoute',
'levels' => 'error, warning',
),
),
),
'image'=>array(
'class'=>'application.extensions.image.CImageComponent',
//GD or ImageMagick
'driver'=>'GD',
'params'=>array('directory'=>'/opt/local/bin',
'product'=>array(
'size'=>array(
'detail'=>array(
'width' => 418,
'height' => 314
),
'thumbnail'=>array(
'width' => 90,
'height' => 68
),
'org'=>array(
'width' => 800,
'height' => 600
),
),
),
),
),
),
);
?>
DefaultController.php
<?php
class DefaultController extends ActivityController
{
public $layout = 'activity';
private $actionStatus;
private $defaultDetailImageWidth;
private $defaultDetailImageHeight;
private $defaultListImageWidth;
private $defaultListImageHeight;
private $defaultOrgImageWidth;
private $defaultOrgImageHeight;
public function actionCreate()
{
$this->pageTitle = "Etkinlik Yönetimi > Etkinlik Ekleme";
$this->render("create", array('model'=>$newActivityModel, 'actionStatus'=>$this->actionStatus, 'categoryGridList'=>$categoryGridList));
}
public function actionList()
{
$this->pageTitle = "Etkinlik Yönetimi > Etkinlik Listeleme";
$this->render("list");
}
public function actionDelete()
{
$this->pageTitle = "Ürün Yönetimi > Ürün Silme";
$this->render("delete", array('actionStatus' => $this->actionStatus));
}
public function actionUpdate()
{
$this->pageTitle = "Ürün Yönetimi > Ürün Güncelleme";
$this->render("update", array('model'=>$product, 'actionStatus' => $this->actionStatus, 'categoryList' => $categoryList));
}
public function actionIndex()
{
$this->render("index");
}
public function actionError()
{
$this->render("error");
}
}
?>
ActivityController.php
<?php
/**
* Controller is the customized base controller class.
* All controller classes for this application should extend from this base class.
*/
class ActivityController extends CController {
/**
* #var string the default layout for the controller view. Defaults to '//layouts/column1',
* meaning using a single column layout. See 'protected/views/layouts/column1.php'.
*/
public $layout = '/layouts/column1';
/**
* #var array context menu items. This property will be assigned to {#link CMenu::items}.
*/
public $constants = array();
public $menu = array(
array('label'=>'Etkinlik Ekle', 'url'=>'/etkinlikler/ekle'),
array('label'=>'Etkinlik Liste', 'url'=>'/etkinlikler/liste'),
);
/**
* #var array the breadcrumbs of the current page. The value of this property will
* be assigned to {#link CBreadcrumbs::links}. Please refer to {#link CBreadcrumbs::links}
* for more details on how to specify this property.
*/
public $breadcrumbs = array();
}
I've set permissions for all files to 777 and I still get the same error.
Also I checked that system is not able to go to controllers. I get the error in main.php
What is the problem?
Ok I found the problem. I haven't defined the activity module in main.php After adding
'activity' => array();
the problem was solved.
Change
array('label'=>'Etkinlik Ekle', 'url'=>'/etkinlikler/ekle'),
array('label'=>'Etkinlik Liste', 'url'=>'/etkinlikler/liste'),
to
array('label' => 'Etkinlik Ekle', 'url' => array('activity/default/create')),
array('label' => 'Etkinlik Liste', 'url' => array('activity/default/list)),

Yii framework new column in database not showing in output of $model->getAttributes() method call?

Yii version 1.1.8
I have added a column to a table in a mysql database,
but the new column is not showing in output of $model->getAttributes() method call
I deleted all files in protected/runtime folder, but still no column
config: 'schemaCachingDuration' => 0, // in seconds. <1 means off
I can add data to the new column directly in the database.
Are there any other things that I can do to debug this?
index.php
<?php
// change the following paths if necessary
$yii=dirname(__FILE__).'/../../framework/yii.php';
$config=dirname(__FILE__).'/protected/config/main.php';
// remove the following line when in production mode
//debug
defined('YII_DEBUG') or define('YII_DEBUG',true );
//show profiler
defined('YII_DEBUG_SHOW_PROFILER') or define('YII_DEBUG_SHOW_PROFILER',true);
//enable profiling
defined('YII_DEBUG_PROFILING') or define('YII_DEBUG_PROFILING',true);
//trace level
defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',0);
//execution time
defined('YII_DEBUG_DISPLAY_TIME') or define('YII_DEBUG_DISPLAY_TIME',false);
require_once($yii);
Yii::createWebApplication($config)->run();
main.php
<?php
return CMap::mergeArray(array(
'timeZone'=>'UTC',
'basePath' => dirname(__FILE__) . DIRECTORY_SEPARATOR . '..',
'catchAllRequest' => null, // null if online, array('site/offline') if offline,
'sourceLanguage' => 'en_ca',
'theme' => 'td',
'charset' => 'UTF-8',
'preload' => array('log'),
'import' => array(
'application.models.*',
'application.components.*',
'application.extensions.*'
),
'modules' => array(
),
// application components
'components' => array(
'format' => array(
),
'user' => array(
// enable cookie-based authentication
'allowAutoLogin' => true,
'autoRenewCookie' => true,
),
'widgetFactory' => array(
'enableSkin' => true,
),
'urlManager' => array(
),
),
'db' => array(
'class' => 'CDbConnection',
'connectionString' => 'mysql:host=localhost;dbname=mydb',
'emulatePrepare' => true,
'initSQLs'=>array("set time_zone='+00:00'"),
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'tablePrefix' => 'tbl_',
'enableParamLogging' => true, //show parameter values in log
// 'schemaCachingDuration' => 0, // in seconds. <1 means off
'enableProfiling' => YII_DEBUG_PROFILING, //show sql profile info in log
'nullConversion' => true,
//'initSQLs'=>array('set time_zone="+00:00"')
),
'errorHandler' => array(
),
'log' => array(
'class' => 'CLogRouter',
'routes' => array(
array(
'class' => 'CFileLogRoute',
'levels' => 'error, warning',
'filter' => 'CLogFilter',
'enabled' => !YII_DEBUG
),
array(
'class' => 'CPhpMailerLogRoute',
'levels' => 'error',
'emails' => 'neilmcguigan+tderror#gmail.com',
'filter' => 'CLogFilter',
'enabled' => false,
),
array(
'class' => 'CWebLogRoute', // show log messages on web pages
'enabled' => YII_DEBUG,
'filter' => 'CLogFilter',
//'showInFireBug' => false
),
array(
'class' => 'CProfileLogRoute',
'enabled' => YII_DEBUG,
'showInFireBug' => false
)
),
),
'request' => array(
),
'securityManager'=>array(
)
),
// application-level parameters that can be accessed
// using Yii::app()->params['paramName']
'params' => array(
),
), require(dirname( __FILE__ ) . '/override.php'));
Having just went through this problem -- I found out the new field names need to be in the attributeLabels function in the model
public function attributeLabels()
{
return array(
'id' => 'ID',
'newfield' => 'New Field',
...
Be careful with using the "better still update overwrite your whole model" suggestion if you are like me and have a lot of custom code in your models. As Sankalp Singha suggests, Just copy the "diff" (green part) from using gii and add the part from attributeLabels to your code.
After you have added a new column in your table then just go to Gii and then click on model generator. In that put the name of your table again and then click preview. There would be an option of diff click on that and then copy the highlighted green code and copy paste it in your original modal or better still update overwrite your whole modal. Then check if you are able to get the values inside and are able to get the attributes. This should work.

Categories