error logging in zend - zend-framework

i am using zend for 1st time..i am facing problems in finding errors. when a setter and getter function name mismatch i get a an error...but the error is show sometimes and not all the time...how do i log such errors.is there any separate log for this?

You can create a simple logger and add a logging instance in the setter or getter function. THe attached sample uses firebug for you to immediately see your logs. You may also use file loggers.
$writer = new Zend_Log_Writer_Firebug();
$filter = new Zend_Log_Filter_Priority( Zend_Log::DEBUG );
$writer->addFilter($filter);
$logger = new Zend_Log ( $writer );
Alternatively, you can use the ever so usefil var_dump();exit(); to verify your issue..

Related

neo4jphp: Cannot instantiate abstract class Everyman\Neo4j\Transport

maybe a simple question but for me as starter with Neo4j a hurdle. I installed the neo4jphp with composer in the same directory as my application. Vendor-Subfolder has been created and the everyman/neo4j folder below is available. For a first test I used this code snippet from the examples:
spl_autoload_register(function ($className) {
$libPath = 'vendor\\';
$classFile = $className.'.php';
$classPath = $libPath.$classFile;
if (file_exists($classPath)) {
require($classPath);
}
});
require('vendor/autoload.php');
use everyman\Neo4j\Client,
everyman\Neo4j\Transport;
$client = new Client(new Transport('localhost', 7474));
print_r($client->getServerInfo());
I always stumple upon the error
Fatal error: Cannot instantiate abstract class Everyman\Neo4j\Transport
Googling brought me to a comment from Josh Adell stating
You can't instantiate Everyman\Neo4j\Transport, since it is an abstract class. You must instantiate Everyman\Neo4j\Transport\Curl or Everyman\Neo4j\Transport\Stream depending on your needs
So I thought I just need to alter the use-statements to
use everyman\Neo4j\Client,
everyman\Neo4j\Transport\Curl;
but this doesnt work, debugging shows, that the autoloader only get "Transport.php" instead of "everyman\Neo4j\Transport\Curl.php". For "Client.php" its still working ("vendor\everyman\Neo4j\Client.php") so I am guessing that the use-statement is wrong or the code is not able to handle an additional subfolder-structure.
Using
require('phar://neo4jphp.phar');
works fine but I read that this is deprecated and should be replaced by composer / autoload.
Anyone has a hint what to change or had the same problem?
Thanks for your time,
Balael
Curl is the default transport. You only need to instantiate your own Transport object if you want to use Stream instead of Curl. If you really want to instantiate your own Curl Transport, the easiest change to your existing code is to modify the use statement to be:
use everyman\Neo4j\Client,
everyman\Neo4j\Transport\Curl as Transport;
Also, you don't need to register your own autoload function if you are using the Composer package. vendor/autoload.php does that for you.
Thanks Josh, I was trying but it seems I still stuck somewhere. I am fine with using the default CURL - so I shrinked the code down to
require('vendor/autoload.php');
use everyman\Neo4j\Client;
$client = new Everyman\Neo4j\Client('localhost', 7474);
print_r($client->getServerInfo());`
The folder structure is main (here are the files and the composer.json with the content
{
"require": {
"everyman/Neo4j": "dev-master"
}
}
and in the subfolder "vendor" we have the "autoload.php" and the subfolder everyman with the related content. When I run the file I come out with
Fatal error: Class 'Everyman\Neo4j\Client' not found
which does not happen when I have the autoloadfunction. I guess I made a mistake somewehere - can you give me a hint?
Thanks a lot, B
Hmmm... I was just trying around and it seems the Transport CLASS is not needed in the use-statement and the class instantiation. This seems to work:
require('vendor/autoload.php');
use everyman\Neo4j\Client;
$client = new Client();
print_r($client->getServerInfo());
also valid for having a dedicated server/port:
$client = new Everyman\Neo4j\Client('localhost', 7474);
If you have more input I would be happy to learn more - thanks, all input & thoughts are very appreciated.
Balael

Using context.Database.Log in MVC web app

Using the guide here, I'm trying to log the SQL generated by my MVC web application.
The guide uses the line:
context.Database.Log = Console.Write;
Which obviously doesn't work with a web application. The guide described the Log property:
The DbContext.Database.Log property can be set to a delegate for any
method that takes a string.
And it's this I don't understand, delegates just confuse me. How can I use this EF6 feature?
Using a delegate allows you to do write any function taking a string. As a very simple logging to a file, you could do the following:
context.Database.Log = message => File.AppendText("C:\\mylog.txt").WriteLine(message);
In a web environment, you may wish to use Trace to log this information:
context.Database.Log = message => Trace.WriteLine(message);
You can see more examples of using delegates on the MSDN page on Anonymous Functions.
To write your logs to a file, try this:
using (var context = new MyDbContext())
{
var logFile = new StreamWriter("C:\\temp\\log.txt");
context.Database.Log = logFile.Write;
// Execute your queries here
// ....
logFile.Close();
}
You can print it in Debug window:
dbContext.Database.Log = message => Debug.Write(message);

Zend_Loader include doesn't throw exception

I am finding that when Zend tries to auto load a file which doesn't exist, it throws an error which I cannot catch in a try/catch block. This happens when I use class_exists too. I have fixed the problem by hacking zend:
if ($once) {
if (!#include_once ($filename)) {
throw new Exception("Failed to include $filename");
}
// include_once $filename;
}
else {
if (!#include ($filename)) {
throw new Exception("Failed to include $filename");
}
// include $filename;
}
The commented out lines are zend's originals. Now I can catch the exception thrown when a file cannot be included. Can anybody suggest a cleaner way to do this which doesn't involve hacking zend?
I am on Zend version 1.11.10, and the code in question is Zend_Loader line 146.
Thanks.
instead of using include or include_once try using Zend_Loader::loadClass()
Here is the API: Zend_Loader::loadClass($class, $dirs)
An example:
Zend_Loader::loadClass('Container_Tree',
array(
'/home/production/mylib',
'/home/production/myapp'
)
);
Now the blurb on how it works:
The string specifying the class is converted to a relative path by
substituting underscores with directory separators for your OS, and
appending '.php'. In the example above, *'Container_Tree'* becomes
'Container\Tree.php' on Windows.
If $dirs is a string or an array, *Zend_Loader::loadClass()* searches
the directories in the order supplied. The first matching file is
loaded. If the file does not exist in the specified $dirs, then the
include_path for the PHP environment is searched.
If the file is not found or the class does not exist after the load,
*Zend_Loader::loadClass()* throws a Zend_Exception.
This should allow you to use a try/catch block for any non-existent classes. Zend_Loader::loadFile() also has similar functionality.
Don't try and autoload classes that don't exist. If for some reason the class you're trying to autoload may or may not be there, wrap that part of code with a class_exists() call.
I can't think of any reason why you would want class_exists() to throw an exception on failure since it's sole purpose is to allow you to check for the existence of classes.

Zend Framework - ZFDebug - Log - Log Custom Errors

When using ZFDebug, is it possible to add custom messages to the 'Log' tab?
So you could use something like:
$this->log('Error: Couldn't find the user');
Has anyone managed to achieve this?
I have never used ZFDebug before and wasn't aware of it. Your post piqued my interest, so I installed it and have been trying to achieve what you want to do. I will probably add it to my dev toolbox as I use ZF a lot.
You can achieve what you want by using the mark() method of ZFDebug_Controller_Plugin_Debug_Plugin_Log which takes two arguments. The first is the message you want to send and the second is a boolean which, when set to true (default is false), will send your message to the 'log' tab.
The following code worked for me:-
$debug = Zend_Controller_Front::getInstance()
->getPlugin('ZFDebug_Controller_Plugin_Debug');
$logger = $debug->getPlugin('log');
$logger->mark('Logging a message now', true);
Or to use your example (with the syntax error fixed :) )
$logger->mark("Error: Couldn't find the user", true);
As you can see this produced the desired output:-
Not quite as simple as you wanted, I know, but it's close and you could always wrap it in a function.

Zend Framework unknown module is interpreted as default module

i wanted to support multilingual structure for my work i used the following lines
$controller=Zend_Controller_Front::getInstance();
$router=$controller->getRouter();
$languageRouter=new Zend_Controller_Router_Route(":lang/:module/:controller/:action", array("lang"=>"en","module"=>"default","controller"=>"index","action"=>"index"),
array("lang"=>"[a-zA-Z]{2}"));
$router->addRoute("default",$languageRouter);
it works fine http://localhost/zend/public/en set the lang param to en and call default module
but the problem is that when i use url like this http://localhost/zend/public/en/anything
where anything isn't module it still show the default module how to prevent that???
after the answer of takeshin i added this function to the bootstarp file and now it works as i want it
protected function _initRoutes()
{
$routeLang=new Zend_Controller_Router_Route(':lang',array('lang'=>'en'),array('lang'=>'[a-z]{2}'));
$front = Zend_Controller_Front::getInstance() /*$this->getResource('frontcontroller')*/;
$router = $front->getRouter();
$routeDefault=new Zend_Controller_Router_Route_Module(array(),$front->getDispatcher(),$front->getRequest());
$routeLangDefault=$routeLang->chain($routeDefault);
$router->addRoute('default',$routeLangDefault);
$router->addRoute('lang',$routeLang);
}
It looks like you have overwritten default module defined in Zend Application by your custom one.
You should chain the routes instead.
The settings you are using means module will 'default' to default , if you didn't it would throw a route not found error - which should throw to appropriate error controller
I'm not sure if I unterstood this correctly, but it looks like it works fine, as it should. If you try to call non existing module, Zend Framework automatically "redirects" to the default module.