0

我搜索了 SilverStripe 4 的文档和 API,但在使用适当的类调用默认异常处理程序时遇到问题。

在我的 Page.php 控制器的 init() 中的 SilverStripe 4 之前它是如何工作的

    parent::init();

    set_exception_handler(function($exception) {
        if( get_class($exception) == 'CustomException' ) {
            Controller::curr()->handleCustomException($exception);
        }
        return exceptionHandler($exception);
    });

我希望它如何与 SilverStripe 4 一起使用

    parent::init();

    set_exception_handler(function($exception) {
        if( get_class($exception) == 'CustomException' ) {
            Controller::curr()->handleCustomException($exception);
        }
        return <SomeMonologClass>::handleException($exception);
    });

我知道 SilverStripe 4 现在使用 Monolog 并且我遇到了handleException我认为我需要调用的函数而不是exceptionHandler($exception).

任何建议将不胜感激。

4

1 回答 1

1
use SilverStripe\Control\Controller;

class MyController extends Controller
{
    private static $dependencies = [
        'logger' => '%$Psr\Log\LoggerInterface',
    ];

    // This will be set automatically, as long as MyController is instantiated via Injector
    public $logger;

    protected function init()
    {
        $this->logger->debug("MyController::init() called");
        parent::init();
    }
}

如此处所述:

https://docs.silverstripe.org/en/4/developer_guides/debugging/error_handling/#accessing-the-logger-via-dependency-injection

更多信息在这里:https ://www.silverstripe.org/learn/lessons/v4/advanced-environment-configuration-1

于 2018-01-15T14:52:12.967 回答