File indexing completed on 2024-09-22 05:21:21

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Service_WindowsAzure
0017  * @subpackage Storage
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 /**
0024  * @see Zend_Log_Writer_Abstract
0025  */
0026 // require_once 'Zend/Log/Writer/Abstract.php';
0027 
0028 /**
0029  * @category   Zend
0030  * @package    Zend_Service_WindowsAzure
0031  * @subpackage Log
0032  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0033  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0034  */
0035 class Zend_Service_WindowsAzure_Log_Writer_WindowsAzure
0036     extends Zend_Log_Writer_Abstract
0037 {
0038     /**
0039      * @var Zend_Service_Log_Formatter_Interface
0040      */
0041     protected $_formatter;
0042 
0043     /**
0044      * Connection to a windows Azure
0045      *
0046      * @var Zend_Service_Service_WindowsAzure_Storage_Table
0047      */
0048     protected $_tableStorageConnection = null;
0049 
0050     /**
0051      * Name of the table to use for logging purposes
0052      *
0053      * @var string
0054      */
0055     protected $_tableName = null;
0056 
0057     /**
0058      * Whether to keep all messages to be logged in an external buffer until the script ends and
0059      * only then to send the messages in batch to the logging component.
0060      *
0061      * @var bool
0062      */
0063     protected $_bufferMessages = false;
0064 
0065     /**
0066      * If message buffering is activated, it will store all the messages in this buffer and only
0067      * write them to table storage (in a batch transaction) when the script ends.
0068      *
0069      * @var array
0070      */
0071     protected $_messageBuffer = array();
0072 
0073     /**
0074      * @param Zend_Service_Service_WindowsAzure_Storage_Table|Zend_Service_WindowsAzure_Storage_Table $tableStorageConnection
0075      * @param string                                                                                  $tableName
0076      * @param bool                                                                                    $createTable create the Windows Azure table for logging if it does not exist
0077      * @param bool                                                                                    $bufferMessages
0078      * @throws Zend_Service_Log_Exception
0079      */
0080     public function __construct(
0081         Zend_Service_WindowsAzure_Storage_Table $tableStorageConnection,
0082         $tableName, $createTable = true, $bufferMessages = true
0083     )
0084     {
0085         if ($tableStorageConnection == null) {
0086             // require_once 'Zend/Service/Log/Exception.php';
0087             throw new Zend_Service_Log_Exception(
0088                 'No connection to the Windows Azure tables provided.'
0089             );
0090         }
0091 
0092         if (!is_string($tableName)) {
0093             // require_once 'Zend/Service/Log/Exception.php';
0094             throw new Zend_Service_Log_Exception(
0095                 'Provided Windows Azure table name must be a string.'
0096             );
0097         }
0098 
0099         $this->_tableStorageConnection = $tableStorageConnection;
0100         $this->_tableName              = $tableName;
0101 
0102         // create the logging table if it does not exist. It will add some overhead, so it's optional
0103         if ($createTable) {
0104             $this->_tableStorageConnection->createTableIfNotExists(
0105                 $this->_tableName
0106             );
0107         }
0108 
0109         // keep messages to be logged in an internal buffer and only send them over the wire when
0110         // the script execution ends
0111         if ($bufferMessages) {
0112             $this->_bufferMessages = $bufferMessages;
0113         }
0114 
0115         $this->_formatter =
0116             new Zend_Service_WindowsAzure_Log_Formatter_WindowsAzure();
0117     }
0118 
0119     /**
0120      * If the log messages have been stored in the internal buffer, just send them
0121      * to table storage.
0122      */
0123     public function shutdown()
0124     {
0125         parent::shutdown();
0126         if ($this->_bufferMessages) {
0127             $this->_tableStorageConnection->startBatch();
0128             foreach ($this->_messageBuffer as $logEntity) {
0129                 $this->_tableStorageConnection->insertEntity(
0130                     $this->_tableName, $logEntity
0131                 );
0132             }
0133             $this->_tableStorageConnection->commit();
0134         }
0135     }
0136 
0137     /**
0138      * Create a new instance of Zend_Service_Log_Writer_WindowsAzure
0139      *
0140      * @param  array $config
0141      * @return Zend_Service_Log_Writer_WindowsAzure
0142      * @throws Zend_Service_Log_Exception
0143      */
0144     static public function factory($config)
0145     {
0146         $config = self::_parseConfig($config);
0147         $config = array_merge(
0148             array(
0149                  'connection' => null,
0150                  'tableName' => null,
0151                  'createTable' => true,
0152             ), $config
0153         );
0154 
0155         return new self(
0156             $config['connection'],
0157             $config['tableName'],
0158             $config['createTable']
0159         );
0160     }
0161 
0162     /**
0163      * The only formatter accepted is already  loaded in the constructor
0164      *
0165      * @todo enable custom formatters using the WindowsAzure_Storage_DynamicTableEntity class
0166      */
0167     public function setFormatter(
0168         Zend_Service_Log_Formatter_Interface $formatter
0169     )
0170     {
0171         // require_once 'Zend/Service/Log/Exception.php';
0172         throw new Zend_Service_Log_Exception(
0173             get_class($this) . ' does not support formatting');
0174     }
0175 
0176     /**
0177      * Write a message to the table storage. If buffering is activated, then messages will just be
0178      * added to an internal buffer.
0179      *
0180      * @param  array $event
0181      * @return void
0182      * @todo   format the event using a formatted, not in this method
0183      */
0184     protected function _write($event)
0185     {
0186         $logEntity = $this->_formatter->format($event);
0187 
0188         if ($this->_bufferMessages) {
0189             $this->_messageBuffer[] = $logEntity;
0190         } else {
0191             $this->_tableStorageConnection->insertEntity(
0192                 $this->_tableName, $logEntity
0193             );
0194         }
0195     }
0196 }