2013-04-25 86 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 ...這就是它!