Last active
July 23, 2024 09:18
-
-
Save gaambo/6ea135cdc28fe3b30e61c2713db78040 to your computer and use it in GitHub Desktop.
Workaround for Wonolog #81 (exception infinite recursion) (https://github.com/inpsyde/Wonolog/issues/81)
This file contains hidden or 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 | |
namespace Site\Logging; | |
use Inpsyde\Wonolog; | |
use Monolog\Handler\SlackWebhookHandler; | |
// Needs to happen on loading mu-plugin. | |
// Do not use default PhpErrorController because of bug https://github.com/inpsyde/Wonolog/issues/81 | |
Php_Error_Controller::register(); // needs to be called before bootstrap so PHP-ERROR is still registered. | |
$controller = \Inpsyde\Wonolog\bootstrap(null, | |
Wonolog\USE_DEFAULT_HOOK_LISTENERS | Wonolog\USE_DEFAULT_PROCESSOR | Wonolog\USE_DEFAULT_HANDLER ); | |
// [..further customization] |
This file contains hidden or 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 | |
namespace Site\Logging; | |
use Monolog\Logger; | |
use Inpsyde\Wonolog\PhpErrorController as Base_Controller; | |
use Inpsyde\Wonolog\Data\Log; | |
use Inpsyde\Wonolog\Channels; | |
class Php_Error_Controller extends Base_Controller { | |
public mixed $previous_exception_handler = null; | |
/** | |
* Uncaught exception handler. | |
* | |
* @param \Throwable $e | |
* | |
* @throws \Throwable | |
*/ | |
public function on_exception( $e ) { | |
// Log the PHP exception. | |
do_action( | |
\Inpsyde\Wonolog\LOG, | |
new Log( | |
$e->getMessage(), | |
Logger::CRITICAL, | |
Channels::PHP_ERROR, | |
[ | |
'exception' => get_class( $e ), | |
'file' => $e->getFile(), | |
'line' => $e->getLine(), | |
'trace' => $e->getTraceAsString(), | |
] | |
) | |
); | |
// after logging let's reset handler and throw the exception | |
// restore_exception_handler(); | |
// Fixes https://github.com/inpsyde/Wonolog/issues/81 | |
set_exception_handler($this->previous_exception_handler); | |
throw $e; | |
} | |
/** | |
* Fixes https://github.com/inpsyde/Wonolog/issues/81 | |
* @param mixed $error_types | |
* @return void | |
*/ | |
public static function register($error_types = NULL) { | |
is_int( $error_types ) or $error_types = E_ALL | E_STRICT; | |
$controller = new static(); | |
register_shutdown_function( [ $controller, 'on_fatal', ] ); | |
set_error_handler( [ $controller, 'on_error' ], $error_types ); | |
$previous_exception_handler = set_exception_handler( [ $controller, 'on_exception', ] ); | |
$controller->previous_exception_handler = $previous_exception_handler; | |
// Ensure that channel Channels::PHP_ERROR error is there | |
add_filter( | |
Channels::FILTER_CHANNELS, | |
function ( array $channels ) { | |
$channels[] = Channels::PHP_ERROR; | |
return $channels; | |
}, | |
PHP_INT_MAX | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment