Skip to content

Instantly share code, notes, and snippets.

@gaambo
Last active July 23, 2024 09:18
Show Gist options
  • Select an option

  • Save gaambo/6ea135cdc28fe3b30e61c2713db78040 to your computer and use it in GitHub Desktop.

Select an option

Save gaambo/6ea135cdc28fe3b30e61c2713db78040 to your computer and use it in GitHub Desktop.

Revisions

  1. gaambo revised this gist Jul 23, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion logging.php
    Original file line number Diff line number Diff line change
    @@ -9,6 +9,6 @@
    // 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 );
    Wonolog\USE_DEFAULT_HOOK_LISTENERS | Wonolog\USE_DEFAULT_PROCESSOR | Wonolog\USE_DEFAULT_HANDLER );

    // [..further customization]
  2. gaambo created this gist Jul 23, 2024.
    73 changes: 73 additions & 0 deletions Php_Error_Controller.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,73 @@
    <?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
    );
    }
    }
    14 changes: 14 additions & 0 deletions logging.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    <?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]