php - Logs for an API in symfony 1.4 -


i working on rest api in symfony 1.4

i log comes in , out of "api" application. in log/api folder, keep track of api calls in various files. call mymodule/myaction, have 3 files:

  • mymodule_myaction_rq.log requests
  • mymodule_myaction_rs.log responses
  • mymodule_myaction_error.log error responses

i know how manually, adding code @ beginning , @ end of each action. here how go:

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 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');                      // api logic 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');     }    } 

here example of logged information:

 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 out of service area  

now tedious...

i understand knowledge of symfony internals, achieve nicely (dryly). here come questions:

  • events may way done. right ? if so, events should use ? how put ?
  • with $request->getcontent(), able content of message sent me. how can pick response content ? (as content of view known after action over, not can done "as usual").
  • well, filters possibly way achieve logging functionnality ?
  • maybe problem sooo standard set in config file ? silly ? or module may doing ?

this level of symfony internals still quite new me, hint, piece of code... welcome !

well, seems filters way it... first, buid filter class in lib folder of app:

<?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');   }   /**  * log  *  @param integer $seed: random number identical across request , response.  *  @param string $content: content of message logged  *  @param type: type of message (rq = request, rs = response)  */ private function customlog($seed, $content, $type) {     // 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"     ); }  } 

register filter in filters.yml configuration file (in app config folder), between security , cache:

rendering: ~ security:  ~  # insert own filters here mycustomfilter:     class: logfilter  cache:     ~ execution: ~ 

and... !


Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -