Showing posts with label logging. Show all posts
Showing posts with label logging. Show all posts

Wednesday, 13 July 2011

Zend Log: Log to a file, formatting output, logging request details, and all error info

Wham this in your Bootstrap.php (replacing YOUR_WEBSITE):
protected function _initLogger() {
$writer = new Zend_Log_Writer_Stream('/var/log/YOUR_WEBSITE/output.log');
$format = '%timestamp% %priorityName%: %message%' . PHP_EOL;
$formatter = new Zend_Log_Formatter_Simple($format);
$writer->setFormatter($formatter);
$logger = new Zend_Log($writer);
$logger->setTimestampFormat("d-M-Y H:i:s");
Zend_Registry::set('logger', $logger);
}
Functions starting with _init will automatically be run during the bootstrap. This will setup the logger to log to a file in your logs directory, with a slightly clearer format than the default one (inc. simplified date format). It then saves the logger instance in the Zend_Registry, which means we can access it from our controllers!

Now in your Controller:
class IndexController extends Zend_Controller_Action {
private $logger;
public function init() {$this->logger = Zend_Registry::get('logger');}
public function indexAction() {$this->logger->info("Hello");}
}

Next, lets setup our logger to log the details of all requests, as this is really useful for debugging. We do this with a Controller Plugin, by implementing the preDispatch function (create application/plugins/Logger.php):
class Application_Plugin_Logger extends Zend_Controller_Plugin_Abstract {
public function preDispatch(Zend_Controller_Request_Abstract $request) {
$uri = $_SERVER['REQUEST_URI'];
if ($uri != "/favicon.ico") {
$logger = Zend_Registry::get('logger');
$logger->info("* REQUESTED URI ".$_SERVER['REQUEST_URI']
    .", CONTROLLER ".$request->getControllerName()
    .", ACTION ".$request->getActionName());
}
}
}
Note: we don't bother logging browsers requests for favicon.ico, as they are not very interesting

Then we need to register the plugin, which we do back in our bootstrap, at the end of _initLogger():
$front = Zend_Controller_Front::getInstance();
$front->registerPlugin(new Application_Plugin_Logger());

Finally, we setup the ErrorController to log all error information by adding this code to errorAction():
$logger = Zend_Registry::get('logger');
$params = $errors->request->getParams();

$paramsString = "array (\n";
foreach($params as $key=>$value) {
$paramsString .= "'".$key."' => '".$value."'\n";
}
$paramsString .= ")";

$logger->crit("ERROR = ".$this->view->message."\n"
    ."MESSAGE = ".$errors->exception->getMessage()."\n"
    ."STACK TRACE = \n".$errors->exception->getTraceAsString()."\n"
    ."REQUEST PARAMS = ".$paramsString);

Tuesday, 22 February 2011

PHP Logging on Ubuntu 10.04 Lucid

PHP CONFIGURATION
Enable PHP logging by setting up the appropriate variables in your php.ini. If you don't know where your php.ini file is, try the command: locate php.ini, mine was here: /etc/php5/apache2/php.ini:
error_reporting = E_ALL & ~E_DEPRECATED
log_errors = On
error_log = /var/log/apache2/php_errors.log
EXTRA: if you want to show PHP errors on screen, set:
display_errors = On
To finish, we restart apache: sudo /etc/init.d/apache2 restart


PERMISSIONS
Then find out what your apache user is called: ps aux | grep apache (mine was www-data), and ensure that user has full permissions on the log file directory (/var/log/apache2/). The best way to do this is ensure the user has group access i.e. either add the user the group currently assigned to the directory, or change the directory's group to one that the user is in! On my system, the directory had the group: adm, and so I added www-data to that group (see /etc/group). You then need to give the directory full permissions for group access: sudo chmod 770 /var/log/apache2/

To register these changes, we need to log out and back in again.


TESTING
An easy way to generate a PHP error is to call an undefined function i.e. put this PHP code into one of your pages:
a_function_that_doesnt_exist();
Now when you visit that page, you will either get a PHP error on screen (if you enabled that), or your browser will just display a 403 error, but importantly, your new log file php_errors.log should have been created and had it's first error logged.


INLINE PHP CONFIGURATION
One other method worth noting is inline configuration. During development, you may decide you want to temporarily enable showing errors on the screen for a particular page. The easiest way to do this, is simply to add this line of PHP to the top of your PHP file:
ini_set('display_errors', TRUE);