Created
May 12, 2021 13:30
-
-
Save design-innovations/ac3c634d8cbc7c2fbed8640d3eed05b4 to your computer and use it in GitHub Desktop.
Pages - Log exceptions and errors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
From https://github.com/joomlatools/joomlatools-pages/discussions/692 | |
Exceptions in Pages are exposed as events, meaning that you they can be easily intercepted them through event subscribers. This allows for a whole lot of flexibility. Based on the type of exception different event handlers could do different things, for example; | |
handling 404 not found exceptions | |
handling error logging | |
rendering debug information | |
Basic exception event subscriber | |
This is an example of a custom exception handler you can add to your own site. Add it to: /joomlatools-pages/extensions/pages/event/subscriber/exception.php | |
*/ | |
class ExtPagesEventSubscriberException extends ComPagesEventSubscriberAbstract | |
{ | |
public function onException(KEvent $event) | |
{ | |
$exception = $event->exception; | |
if($exception->getCode() == 500) | |
{ | |
//Handle exception here | |
} | |
} | |
} | |
/* | |
Exception logger using Monolog | |
This is an example of a Monolog exception logger you can add to your own site. Add it to: /joomlatools-pages/extensions/pages/event/subscriber/error.php | |
Object properties: | |
enabled: An event subscriber has an enabled property to to allow you to disable it dynamically, by default a subscriber is enabled. Here i'm disabling it if debug is off. If you want to always enable it, take out this code. | |
base_path: Using the pages logs path to store the error logs | |
*/ | |
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($exception->getCode() >= 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 ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment