简体   繁体   English

Luracast Restler 3 登录响应

[英]Luracast Restler 3 logging onRespond

Follow up questions for this solution: https://stackoverflow.com/a/31883204/3548238跟进此解决方案的问题: https : //stackoverflow.com/a/31883204/3548238

  1. If i want to log the request in "onRespond()" event, how can i access to the status code?如果我想在“onRespond()”事件中记录请求,我如何访问状态代码? For example: if request responds with status code 200, i'd want to log "success = 1" and if its something else, then "success = 0"例如:如果请求以状态代码 200 响应,我想记录“success = 1”,如果是其他内容,则记录“success = 0”
  2. Can i somehow access the message, if my API throws exception with "throw new RestException(404, "Example not found");"如果我的 API 使用“throw new RestException(404, “Example not found”);”抛出异常,我可以以某种方式访问​​该消息吗? or if auth failed or whatever the cause is.或者如果身份验证失败或任何原因。
  3. Can i/how can i make some of the requests so that they wont be logged in any situation?我可以/如何提出一些请求,以便它们在任何情况下都不会被记录? I know i should create some kind of annotation for it, but: how?我知道我应该为它创建某种注释,但是:如何?

For example something like this:例如这样的事情:

/**
 *
 * @status      200
 *
 * @description Get all logs
 * @url         GET logs
 * @access      protected
 * @class       AccessControl {@requires admin}
 *
 * @log         false
 *
 * @throws RestException
 */
public function list_all_logs() {
...
...

You should be using onComplete instead of onRespond您应该使用onComplete而不是onRespond

Why?为什么?

  • onRespond() - fired before sending response onRespond() - 在发送响应之前触发
  • onComplete() - fired after sending response onComplete() - 发送响应后触发

Here is the complete solution that answers all your questions, assuming you are adding @log false comment to the api method you want to exclude这是回答您所有问题的完整解决方案,假设您在要排除的 api 方法中添加了@log false注释

use Luracast\Restler\Restler;
use Luracast\Restler\User;

$r = new Restler();
$r->onComplete(function () use ($r) {
    if (
        !isset($r->apiMethodInfo->metadata['log']) ||
        $r->apiMethodInfo->metadata['log'] == 'true'
    ) {
        $success = $r->responseCode == 200;
        $info = array(
            'success' => $success,
            'message' => $success ? '' : $r->exception->getErrorMessage()
        );
        print_r($info); //your logging function here!
    }
});
$r->addAPIClass('Say');
$r->handle();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM