2013-04-25 88 views
0

我在Symfony的1.4日志中的symfony 1.4 API

在REST API的工作,我想记录那些进入和离开我的“API”的应用程序的一切。在日志/ api文件夹中,我将跟踪各种文件中的api调用。对于以Mymodule中/ myAction打电话,我将有三个文件:

  • myModule_myAction_RQ.log所有请求
  • myModule_myAction_RS.log所有响应
  • myModule_myAction_error.log所有错误响应

我知道如何手动执行该操作,在每个操作的开始和结束时添加一些代码。这是我如何去:

class myActions extends sfActions 
{ 
/** 
* log function 
*/ 
private static function customLog($message, $seed, $url, $content, $type) 
{ 
    $file = sprintf('%s/%s_%s.log', sfConfig::get('sf_log_dir', "no_log_dir")."/api", $message, $type); 
    $logger = new sfFileLogger(
       new sfEventDispatcher(), 
       array('file'=> $file) 
      ); 

    $logger->log(sprintf("#%s# (%s) %s ", $seed, $url, $content), 
        0, 
        "info" 
    ); 
} 

/** 
    * Executes index action 
    * 
    * @param sfRequest $request A request object 
    */ 
    public function executeIndex(sfWebRequest $request) 
    { 
    try {   
     $json_msg = $request->getContent(); 
     // LOG !!! 
     $seed = rand(); 
     $current_uri = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; 
     self::customLog("availability", $seed, $current_uri, $json_msg, 'RQ'); 

        // Do some API logic to set $response_msg 
        // ... 

        $this->response_msg = $response_msg; 

     // LOG !!! 
     self::customLog("myModule_index", $seed, $current_uri, $response_msg, 'RS'); 

    } 
    catch(Exception $e) 
    { 
     // throw $e; 
     $this->setTemplate("error"); 
     $this->error = $e; 

     // LOG !!! 
     self::customLog("myModule_index", $seed, $current_uri, $e->getCode().":".$e->getMessage(), 'error'); 
    } 

    } 

这里有记录信息的一些例子:

myModule_index_RQ.log: 
Apr 25 11:49:31 symfony [emerg] #958824120# (http://host.local/api_dev.php/users/1/index {"price_km":0.66,"reservation_type":3, "position":{"long":2.139015,"lat":41.37947}} 
Apr 25 11:56:27 symfony [emerg] #512729287# (http://host.local/api_dev.php/users/1/index {"price_km":0.66,"reservation_type":3,"position":{"long":2.161576,"lat":41.396896}} 

myModule_index_RS.log: 
Apr 25 11:49:32 symfony [emerg] #958824120# (http://host.local/api_dev.php/users/1/index) {"id_availability":539,"alternatives":[{"id_alternative":1,"duration":9,"reservation_type":3,"distance":3.5,"price":1.62,"original_price":2.31}]} 
Apr 25 11:56:27 symfony [emerg] #512729287# (http://host.local/api_dev.php/users/1/index) {"id_availability":540} 

myModule_index_error.log: 
Apr 25 11:38:20 symfony [emerg] #1059359810# (http://host.local/api_dev.php/users/1/index) 4205:Position to out of service area 

现在,这可能会很快变得单调而乏味......

我了解,Symfony的良好的知识内部,我可以很好地实现(DRYly)。所以这里来我的问题:

  • 事件可能是完成它的方式。我对吗 ?如果是这样,我应该使用哪些事件?我将如何把它放在一起?
  • with $ request-> getContent(),我能够获得发送给我的消息的内容。我怎样才能拿起我的回应内容? (因为我的观点内容只有在我的行动结束后才知道,这不是“照常做”的事情)。
  • 那么,过滤器可能是实现所有这些日志功能的方式?
  • 也许这个问题是标准的,我可以在一些配置文件中设置它?这是愚蠢的吗?或者某个模块可能会这样做?

这个级别的Symfony内部对我来说还是比较新的,所以任何提示,代码段......都会非常受欢迎!

回答

1

嗯,这似乎是过滤器来做到这一点... 首先,我打造专业化了在应用程序的lib文件夹的过滤器类:

<?php 
class LogFilter extends sfFilter{ 

public function execute($filterChain){ 

     $request = $this->context->getRequest(); 
     $response = $this->context->getResponse(); 

     $seed = rand(); 
     $this->customLog($seed, $request->getContent(), 'RQ'); 

     $filterChain->execute($filterChain);  

     $this->customLog($seed, $response->getContent(), 'RS'); 
    } 


/** 
* just log 
* @param integer $seed: a random number that will be identical across request and response. 
* @param string $content: the content of the message to be logged 
* @param type: the type of the message (RQ = request, RS = response) 
*/ 
private function customLog($seed, $content, $type) 
{ 
    // get the current action information 
    $moduleName = $this->context->getModuleName(); 
    $actionName = $this->context->getActionName(); 
    $message = $moduleName."-".$actionName; 
    $url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; 

    $file = sprintf('%s/%s-%s.log', sfConfig::get('sf_log_dir', "no_log_dir")."/api-in", $message, $type); 
    $logger = new sfFileLogger(
       new sfEventDispatcher(), 
       array('file'=> $file) 
      ); 

    $logger->log(sprintf("#%s# (%s) %s ", $seed, $url, $content), 
        0, 
        "info" 
    ); 
} 

} 

在filters.yml注册您的过滤器配置文件(在您的应用程序配置文件夹中),安全和缓存之间:

rendering: ~ 
security: ~ 

# insert your own filters here 
MyCustomFilter: 
    class: LogFilter 

cache:  ~ 
execution: ~ 

And ...这就是它!