CakePHP:控制台命令中的自定义错误报告 [英] CakePHP: Custom Error reporting in Console Commands

查看:135
本文介绍了CakePHP:控制台命令中的自定义错误报告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们目前使用CakePHP 2.4.7和自定义错误处理程序。自定义错误处理程序对通过HTTP或通过CronDispatcher发出的每个请求都很好。

we're currently using CakePHP 2.4.7 and a custom error handler. The custom error handler works fine for every request made through HTTP or via CronDispatcher.

不幸的是,当向我们的控制台命令发出控制台请求时,错误处理程序被忽略。

Unfortunately, when making a console request to one of our Console Commands, the error handler is ignored.

请参阅以下示例:

core.php: >

core.php:

App::uses('SentryErrorHandler', 'Sentry.Lib');

Configure::write('Sentry', array(
  //'production_only' => true, // true is default value -> no error in sentry when debug
  'PHP' => array(
    'server' => 'https://XXX:YYY@app.getsentry.com/1234'
  )
));

Configure::write('Error', array(
  'handler' => 'SentryErrorHandler::handleError',
  'level' => E_ALL & ~E_DEPRECATED,
  'trace' => true
));

Configure::write('Exception', array(
  'handler' => 'SentryErrorHandler::handleException',
  'renderer' => 'ExceptionRenderer',
  'log' => true
));

XYController.php / XYShell.php:
$ b

XYController.php / XYShell.php:

function test() {
  die(pr([]['test']));
}

在这两种情况下,都会抛出正确的错误:

In both cases, the correct error is thrown:

PHP Parse error:  syntax error, unexpected '[' in /vagrant/htdocs/app/Console/Command/XYShell.php on line 50

Parse error: syntax error, unexpected '[' in /vagrant/htdocs/app/Console/Command/XYShell.php on line 50
Fatal Error Error: syntax error, unexpected '[' in [/vagrant/htdocs/app/Console/Command/XYShell.php, line 50]

上述方法通过./cake XY测试,它看起来像,错误没有正确传播到自定义错误处理程序。

But when calling the mentioned method via ./cake XY test, it looks like, that the error is not properly propagated to the custom error handler.

我错过了什么?

推荐答案

控制台和网络不使用相同的配置



错误配置对标准异常和错误有单独的选项 config:


consoleHandler - callback - 用于在控制台中运行时处理错误的回调。如果未定义,将使用CakePHP的默认处理程序。

consoleHandler - callback - The callback used to handle errors when running in the console. If undefined, CakePHP’s default handlers will be used.

这也很明显

这样可以配置用于 web和cli请求的不同处理程序。 handler consoleHandler 键:

As such to configure a different handler to be used for both web and cli requests - define both the handler and consoleHandler keys:

Configure::write('Error', array(
  'handler' => 'SentryErrorHandler::handleError',
  'consoleHandler' => 'SentryErrorHandler::handleError', # <--
  'level' => E_ALL & ~E_DEPRECATED,
  'trace' => true
));

Configure::write('Exception', array(
  'handler' => 'SentryErrorHandler::handleException',
  'consoleHandler' => 'SentryErrorHandler::handleException', # <--
  'renderer' => 'ExceptionRenderer',
  'log' => true
));

这篇关于CakePHP:控制台命令中的自定义错误报告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆