2015-12-13 30 views
2

新手symfony所以請指出任何明顯:)Symfony 2 - 登錄服務

我有一個服務,發送推送通知。我正在嘗試將日誌對象傳遞給該服務,以便我可以寫入主日誌處理程序。底線是這不寫任何地方,我不知道我哪裏出了錯。

我從我的代碼中拿出了一些東西,但這通常是這個想法。

等等\ CoreBundle \服務\ PushTask.php

public function __construct(
    \Doctrine\ORM\EntityManager $entityManager, 
    $logger 
) { 
    $this->entityManager = $entityManager; 
    $this->logger = $logger; 
} 
... 
public function pushSomething() 
{ 
    $this->logger->addInfo('test'); // not writing 
} 

config_dev.yml

monolog: 
    handlers: 
     main: 
      type: stream 
      path: %kernel.logs_dir%/%kernel.environment%.log 
      level: info 

等等\ CoreBundle \資源\ CONFIG \ services.xml的

<service id="civix_core.push" class="blah\CoreBundle\Service\PushTask"> 
     <argument type="service" id="doctrine.orm.entity_manager" /> 
     <argument type="service" id="logger" /> 
</service> 

回答

1

完整的堆棧,你可以用以下的例子:

等等\ CoreBundle \服務\ PushTask.php

/** 
* @var LoggerInterface 
*/ 
private $logger; 

public function __construct(
    \Doctrine\ORM\EntityManager $entityManager, 
    LoggerInterface $logger 
) { 
    $this->entityManager = $entityManager; 
    $this->logger = $logger; 
} 
... 
public function pushSomething() 
{ 
    $this->logger->info('test'); 
} 

config.yml

monolog: 
    channels: 
    - your_new_channel 
    handlers: 
    // just keep it or add a new handler like: 
    your_handler: 
     type: stream 
     path: "%kernel.logs_dir%/%kernel.environment%_new_channel.log" 
     level: info 
     channels: ["your_new_channel"] 

等等\ CoreBundle \資源\ config \ services.xml

<service id="civix_core.push" class="blah\CoreBundle\Service\PushTask"> 
     <argument type="service" id="doctrine.orm.entity_manager" /> 
     <argument type="service" id="monolog.logger.your_new_channel" /> 
</service> 

請檢查文件:dev_new_channel.log

您也可以在執行此操作後清除緩存,以確保所有yml/xml更改都已到位!

爲什麼這是我的建議? 使用頻道和處理程序將幫助您保持您的日誌組織和輕鬆更改!

+0

感謝您的回覆。當我可以的時候我會在今晚檢查一下。這似乎是我剛剛還沒有完全理解的方向。我會及時向大家發佈! –

+0

它看起來像這是配置monolog 2.4。我使用2.3。對不起,我沒有指定。這讓我在正確的軌道上仍然摸索着配置中的正確設置。 –

+0

如果您沒有任何特定原因留在舊版本中,我建議您更新它! –

0

嘗試用這個:

$this->logger->info('test'); 

,而不是這樣的:

$this->logger->addInfo('test'); 

希望這有助於(ASAP更多信息)。

+0

感謝回覆@matteo。將嘗試,但我敢肯定,我已經嘗試了你的建議。這是我最初的嘗試,但只嘗試了'addInfo()',因爲我在其他地方看過其他地方。我很絕望。哈!我會盡快更新。 –

-2
public function getCustomerAction(Request $request){ 
Common::Init($this->get('logger'),$request->headers->get('Request-id'));} 

class Common { 

    public static $logger; 
    public static $request_id; 



    public static function Init($logger, $request_id) { 

     self::$logger = $logger; 


     // self::$logger->pushHandler(new StreamHandler('php://stderr', Logger::DEBUG)); 



     if (is_null($request_id)) { 
      self::$request_id = Common::generate_uuid(); 
     } 
     else{ 
      self::$request_id =$request_id; 
     } 

      self::$logger->pushProcessor(function ($record) { 

      $record['context']['request-id'] = isset($_SERVER['HTTP_X_REQUEST_ID']) ? $_SERVER['HTTP_X_REQUEST_ID'] : self::$request_id; 
      dump($record); 
      return $record; 
     }); 

     Common::$logger->debug('hey it works'); 



    } 


    public static function generate_uuid() { 
     return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0x0fff) | 0x4000, mt_rand(0, 0x3fff) | 0x8000, mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff) 
     ); 
    }} 
+0

一個簡短的總結會幫助評論者(比如我)判斷你的答案的有效性;感興趣的用戶不需要深入代碼來理解你的方法。只需一兩句話就沒問題,工作也不會太多。 –