How to log errors and exceptions #692
-
Hi,
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Exceptions in Pages are exposed as events. They can easily be intercepted through event subscribers. Based on the type of exception different event handlers could do different things, for example:
What exceptions does Pages captureIf Pages is activate all errors and exceptions will be captured, exceptions can be triggered anywhere. Depending on your Joomla exception and error handling settings Pages will also capture exceptions in Joomla extensions and plugins that are triggered in the request/response cycle. Pages does that by registering itself as a Joomla exception callback handler through: See: event/subscriber/exception.php#L24 Basic exception event subscriberThis is an example of a custom exception handler you can add to your own site. Add it to: class ExtPagesEventSubscriberException extends ComPagesEventSubscriberAbstract
{
public function onException(KEvent $event)
{
$exception = $event->exception;
if($exception->getCode() == 500)
{
//Handle exception here
}
}
} Exception logger using MonologThis is an example of a Monolog exception logger you can add to your own site. Add it to: Object properties:
<?php
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Monolog\Processor\WebProcessor;
use Monolog\Formatter\LineFormatter;
class ExtPagesEventSubscriberError extends ComPagesEventSubscriberAbstract
{
protected function _initialize(KObjectConfig $config)
{
$config->append([
'enabled' => JDEBUG ? true : false,
'base_path' => $this->getObject('com:pages.config')->getSitePath('logs'),
]);
parent::_initialize($config);
}
public function getFile()
{
return $this->getConfig()->base_path.'/'.'error.log';
}
//https://chrishewett.com/blog/monolog-human-readable-exception-email-with-stack-trace/
//http://www.inanzzz.com/index.php/post/xj81/handling-and-logging-exceptions-in-symfony-api-applications
public function onException(KEvent $event)
{
if(class_exists('Monolog\Logger'))
{
$exception = $event->exception;
//If the exception code does not correspond to a http status message, use 500
$code = $exception->getCode();
if(!isset(KHttpResponse::$status_messages[$code])) {
$code = '500';
}
if($code >= 500)
{
//Log invalid requests exception
$file = $this->getFile();
// Create a logger
$logger = new Logger('error');
$handler = new StreamHandler($file);
$handler->setFormatter(new PrintRLineFormatter());
$logger->pushHandler($handler);
$logger->pushProcessor(new WebProcessor());
$logger->pushProcessor(function ($record)
{
if( !empty( $_SERVER ) ){
$record['extra']['_SERVER'] = $_SERVER;
}
if( !empty( $_SESSION ) ){
$record['extra']['_SESSION'] = $_SESSION;
}
if( !empty( $_POST ) ){
$record['extra']['_POST'] = $_POST;
}
return $record;
});
$log = [
'code' => $exception->getCode(),
'message' => $exception->getMessage(),
'file' => $exception->getFile(),
'line' => $exception->getLine(),
];
if ($exception->getPrevious() instanceof Exception)
{
$log += [
'previous' => [
'message' => $exception->getPrevious()->getMessage(),
'exception' => get_class($exception->getPrevious()),
'file' => $exception->getPrevious()->getFile(),
'line' => $exception->getPrevious()->getLine(),
],
];
}
$logger->error($exception->getMessage(), $log);
}
}
}
}
class PrintrLineFormatter extends LineFormatter
{
public function format(array $record)
{
return print_r( $record, true );
}
} LogrotationThe exception logger is build to be used during development to log exceptions. If you want to use it production it's advised tot setup a log rotator. You can use the Monolog RotatingFileHandler to handle log rotation. You should use logrotate for high profile setups though, this is just meant as a quick and dirty solution. |
Beta Was this translation helpful? Give feedback.
Exceptions in Pages are exposed as events. They can easily be intercepted through event subscribers. Based on the type of exception different event handlers could do different things, for example:
What exceptions does Pages capture
If Pages is activate all errors and exceptions will be captured, exceptions can be triggered anywhere. Depending on your Joomla exception and error handling settings Pages will also capture exceptions in Joomla extensions and plugins that are triggered in the request/response cycle.
Pages does that by registering itself as a Joomla exception callback handler through:
JError::…