2012-07-20 36 views
1

我已經開始學習Symfony2了,目前我正在處理一個我想單獨登錄的命令。symfony2中的自定義記錄器和monolog格式化程序 - configure services.xml

我的目標是使用自定義記錄器併爲我的命令獲取乾淨的日誌文件。 我在使用xml配置文件的項目上工作,但我沒有得到如何翻譯一些.yml參數和選項。

我已閱讀How to write logs from one service into separate file?並獲得了獨立的日誌文件。

<?xml version="1.0" ?> 
<container xmlns="http://symfony.com/schema/dic/services" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> 

    <services> 
     <service id="myproject_mycommand.logger" class="Symfony\Bridge\Monolog\Logger"> 
      <argument>myproject_mycommand.logger</argument> 
      <call method="pushHandler"> 
       <argument type="service" id="myproject_mycommand.logger_handler" /> 
      </call> 
     </service> 

     <service id="myproject_mycommand.logger_handler" class="Monolog\Handler\StreamHandler"> 
      <argument>%kernel.logs_dir%/my_custom_file_log.log</argument> 
     </service> 
    </services> 
</container> 

尋找Symfony2 : use Processors while logging in different files我想通過我的唯一希望的獨白行格式化領域,像「[%%日期時間%%] %%消息%% \ n」個

我猜後那我會添加類似:

container 
... 
    xmlns:monolog="http://symfony.com/schema/dic/monolog" 
... 

<service id="myproject_mycommand.logger.formatter" class="Monolog\Formatter\LineFormatter"> 
    <argument>"[%%datetime%%] %%message%%\n"</argument> 
</service> 

,但我不明白如何在xml文件中配置這個簡單的格式化程序。

感謝您的幫助!

回答

1

您需要創建您的格式化程序並將其從FormatterInterface擴展並實現formatformatBatch

然後添加服務定義

<service id="myproject_mycommand.logger.formatter" class="MyBundle\Formatter\XmlFormatter"> 
    <argument>some arguments if you need one</argument> 
</service> 

<service id="myproject_mycommand.logger_handler" class="Monolog\Handler\StreamHandler"> 
    <argument>%kernel.logs_dir%/my_custom_file_log.log</argument> 
    <call method="setFormatter"> 
     <argument type="service" id="myproject_mycommand.logger.formatter"/> 
    </call>  
</service> 

這就是它。有關如何格式化的想法,您可以檢查獨立包源中不同格式化程序的示例。

相關問題