web/lib/Zend/Service/WindowsAzure/Log/Writer/WindowsAzure.php
changeset 808 6b6c2214f778
child 1230 68c69c656a2c
equal deleted inserted replaced
807:877f952ae2bd 808:6b6c2214f778
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     7  * This source file is subject to the new BSD license that is bundled
       
     8  * with this package in the file LICENSE.txt.
       
     9  * It is also available through the world-wide-web at this URL:
       
    10  * http://framework.zend.com/license/new-bsd
       
    11  * If you did not receive a copy of the license and are unable to
       
    12  * obtain it through the world-wide-web, please send an email
       
    13  * to license@zend.com so we can send you a copy immediately.
       
    14  *
       
    15  * @category   Zend
       
    16  * @package    Zend_Service_WindowsAzure
       
    17  * @subpackage Storage
       
    18  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
       
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    20  * @version    $Id$
       
    21  */
       
    22 
       
    23 /**
       
    24  * @see Zend_Log_Writer_Abstract
       
    25  */
       
    26 require_once 'Zend/Log/Writer/Abstract.php';
       
    27 
       
    28 /**
       
    29  * @category   Zend
       
    30  * @package    Zend_Service_WindowsAzure
       
    31  * @subpackage Log
       
    32  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
       
    33  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    34  */
       
    35 class Zend_Service_WindowsAzure_Log_Writer_WindowsAzure
       
    36     extends Zend_Log_Writer_Abstract
       
    37 {
       
    38     /**
       
    39      * @var Zend_Service_Log_Formatter_Interface
       
    40      */
       
    41     protected $_formatter;
       
    42 
       
    43     /**
       
    44      * Connection to a windows Azure
       
    45      *
       
    46      * @var Zend_Service_Service_WindowsAzure_Storage_Table
       
    47      */
       
    48     protected $_tableStorageConnection = null;
       
    49 
       
    50     /**
       
    51      * Name of the table to use for logging purposes
       
    52      *
       
    53      * @var string
       
    54      */
       
    55     protected $_tableName = null;
       
    56 
       
    57     /**
       
    58      * Whether to keep all messages to be logged in an external buffer until the script ends and
       
    59      * only then to send the messages in batch to the logging component.
       
    60      *
       
    61      * @var bool
       
    62      */
       
    63     protected $_bufferMessages = false;
       
    64 
       
    65     /**
       
    66      * If message buffering is activated, it will store all the messages in this buffer and only
       
    67      * write them to table storage (in a batch transaction) when the script ends.
       
    68      *
       
    69      * @var array
       
    70      */
       
    71     protected $_messageBuffer = array();
       
    72 
       
    73     /**
       
    74      * @param Zend_Service_Service_WindowsAzure_Storage_Table|Zend_Service_WindowsAzure_Storage_Table $tableStorageConnection
       
    75      * @param string                                                                                  $tableName
       
    76      * @param bool                                                                                    $createTable create the Windows Azure table for logging if it does not exist
       
    77      * @param bool                                                                                    $bufferMessages
       
    78      * @throws Zend_Service_Log_Exception
       
    79      */
       
    80     public function __construct(
       
    81         Zend_Service_WindowsAzure_Storage_Table $tableStorageConnection,
       
    82         $tableName, $createTable = true, $bufferMessages = true
       
    83     )
       
    84     {
       
    85         if ($tableStorageConnection == null) {
       
    86             require_once 'Zend/Service/Log/Exception.php';
       
    87             throw new Zend_Service_Log_Exception(
       
    88                 'No connection to the Windows Azure tables provided.'
       
    89             );
       
    90         }
       
    91 
       
    92         if (!is_string($tableName)) {
       
    93             require_once 'Zend/Service/Log/Exception.php';
       
    94             throw new Zend_Service_Log_Exception(
       
    95                 'Provided Windows Azure table name must be a string.'
       
    96             );
       
    97         }
       
    98 
       
    99         $this->_tableStorageConnection = $tableStorageConnection;
       
   100         $this->_tableName              = $tableName;
       
   101 
       
   102         // create the logging table if it does not exist. It will add some overhead, so it's optional
       
   103         if ($createTable) {
       
   104             $this->_tableStorageConnection->createTableIfNotExists(
       
   105                 $this->_tableName
       
   106             );
       
   107         }
       
   108 
       
   109         // keep messages to be logged in an internal buffer and only send them over the wire when
       
   110         // the script execution ends
       
   111         if ($bufferMessages) {
       
   112             $this->_bufferMessages = $bufferMessages;
       
   113         }
       
   114 
       
   115         $this->_formatter =
       
   116             new Zend_Service_WindowsAzure_Log_Formatter_WindowsAzure();
       
   117     }
       
   118 
       
   119     /**
       
   120      * If the log messages have been stored in the internal buffer, just send them
       
   121      * to table storage.
       
   122      */
       
   123     public function shutdown()
       
   124     {
       
   125         parent::shutdown();
       
   126         if ($this->_bufferMessages) {
       
   127             $this->_tableStorageConnection->startBatch();
       
   128             foreach ($this->_messageBuffer as $logEntity) {
       
   129                 $this->_tableStorageConnection->insertEntity(
       
   130                     $this->_tableName, $logEntity
       
   131                 );
       
   132             }
       
   133             $this->_tableStorageConnection->commit();
       
   134         }
       
   135     }
       
   136 
       
   137     /**
       
   138      * Create a new instance of Zend_Service_Log_Writer_WindowsAzure
       
   139      *
       
   140      * @param  array $config
       
   141      * @return Zend_Service_Log_Writer_WindowsAzure
       
   142      * @throws Zend_Service_Log_Exception
       
   143      */
       
   144     static public function factory($config)
       
   145     {
       
   146         $config = self::_parseConfig($config);
       
   147         $config = array_merge(
       
   148             array(
       
   149                  'connection' => null,
       
   150                  'tableName' => null,
       
   151                  'createTable' => true,
       
   152             ), $config
       
   153         );
       
   154 
       
   155         return new self(
       
   156             $config['connection'],
       
   157             $config['tableName'],
       
   158             $config['createTable']
       
   159         );
       
   160     }
       
   161 
       
   162     /**
       
   163      * The only formatter accepted is already  loaded in the constructor
       
   164      *
       
   165      * @todo enable custom formatters using the WindowsAzure_Storage_DynamicTableEntity class
       
   166      */
       
   167     public function setFormatter(
       
   168         Zend_Service_Log_Formatter_Interface $formatter
       
   169     )
       
   170     {
       
   171         require_once 'Zend/Service/Log/Exception.php';
       
   172         throw new Zend_Service_Log_Exception(
       
   173             get_class($this) . ' does not support formatting');
       
   174     }
       
   175 
       
   176     /**
       
   177      * Write a message to the table storage. If buffering is activated, then messages will just be
       
   178      * added to an internal buffer.
       
   179      *
       
   180      * @param  array $event
       
   181      * @return void
       
   182      * @todo   format the event using a formatted, not in this method
       
   183      */
       
   184     protected function _write($event)
       
   185     {
       
   186         $logEntity = $this->_formatter->format($event);
       
   187 
       
   188         if ($this->_bufferMessages) {
       
   189             $this->_messageBuffer[] = $logEntity;
       
   190         } else {
       
   191             $this->_tableStorageConnection->insertEntity(
       
   192                 $this->_tableName, $logEntity
       
   193             );
       
   194         }
       
   195     }
       
   196 }