PageRenderTime 67ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/Service/WindowsAzure/Log/Writer/WindowsAzure.php

https://bitbucket.org/netglue/zf-1.12-release
PHP | 196 lines | 89 code | 18 blank | 89 comment | 8 complexity | 8fbf5d7386e3c668d4564646ed9a9cb1 MD5 | raw file
  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. * @see Zend_Log_Writer_Abstract
  24. */
  25. require_once 'Zend/Log/Writer/Abstract.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Service_WindowsAzure
  29. * @subpackage Log
  30. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class Zend_Service_WindowsAzure_Log_Writer_WindowsAzure
  34. extends Zend_Log_Writer_Abstract
  35. {
  36. /**
  37. * @var Zend_Service_Log_Formatter_Interface
  38. */
  39. protected $_formatter;
  40. /**
  41. * Connection to a windows Azure
  42. *
  43. * @var Zend_Service_Service_WindowsAzure_Storage_Table
  44. */
  45. protected $_tableStorageConnection = null;
  46. /**
  47. * Name of the table to use for logging purposes
  48. *
  49. * @var string
  50. */
  51. protected $_tableName = null;
  52. /**
  53. * Whether to keep all messages to be logged in an external buffer until the script ends and
  54. * only then to send the messages in batch to the logging component.
  55. *
  56. * @var bool
  57. */
  58. protected $_bufferMessages = false;
  59. /**
  60. * If message buffering is activated, it will store all the messages in this buffer and only
  61. * write them to table storage (in a batch transaction) when the script ends.
  62. *
  63. * @var array
  64. */
  65. protected $_messageBuffer = array();
  66. /**
  67. * @param Zend_Service_Service_WindowsAzure_Storage_Table|Zend_Service_WindowsAzure_Storage_Table $tableStorageConnection
  68. * @param string $tableName
  69. * @param bool $createTable create the Windows Azure table for logging if it does not exist
  70. * @param bool $bufferMessages
  71. * @throws Zend_Service_Log_Exception
  72. */
  73. public function __construct(
  74. Zend_Service_WindowsAzure_Storage_Table $tableStorageConnection,
  75. $tableName, $createTable = true, $bufferMessages = true
  76. )
  77. {
  78. if ($tableStorageConnection == null) {
  79. require_once 'Zend/Service/Log/Exception.php';
  80. throw new Zend_Service_Log_Exception(
  81. 'No connection to the Windows Azure tables provided.'
  82. );
  83. }
  84. if (!is_string($tableName)) {
  85. require_once 'Zend/Service/Log/Exception.php';
  86. throw new Zend_Service_Log_Exception(
  87. 'Provided Windows Azure table name must be a string.'
  88. );
  89. }
  90. $this->_tableStorageConnection = $tableStorageConnection;
  91. $this->_tableName = $tableName;
  92. // create the logging table if it does not exist. It will add some overhead, so it's optional
  93. if ($createTable) {
  94. $this->_tableStorageConnection->createTableIfNotExists(
  95. $this->_tableName
  96. );
  97. }
  98. // keep messages to be logged in an internal buffer and only send them over the wire when
  99. // the script execution ends
  100. if ($bufferMessages) {
  101. $this->_bufferMessages = $bufferMessages;
  102. }
  103. $this->_formatter =
  104. new Zend_Service_WindowsAzure_Log_Formatter_WindowsAzure();
  105. }
  106. /**
  107. * If the log messages have been stored in the internal buffer, just send them
  108. * to table storage.
  109. */
  110. public function shutdown()
  111. {
  112. parent::shutdown();
  113. if ($this->_bufferMessages) {
  114. $this->_tableStorageConnection->startBatch();
  115. foreach ($this->_messageBuffer as $logEntity) {
  116. $this->_tableStorageConnection->insertEntity(
  117. $this->_tableName, $logEntity
  118. );
  119. }
  120. $this->_tableStorageConnection->commit();
  121. }
  122. }
  123. /**
  124. * Create a new instance of Zend_Service_Log_Writer_WindowsAzure
  125. *
  126. * @param array $config
  127. * @return Zend_Service_Log_Writer_WindowsAzure
  128. * @throws Zend_Service_Log_Exception
  129. */
  130. static public function factory($config)
  131. {
  132. $config = self::_parseConfig($config);
  133. $config = array_merge(
  134. array(
  135. 'connection' => null,
  136. 'tableName' => null,
  137. 'createTable' => true,
  138. ), $config
  139. );
  140. return new self(
  141. $config['connection'],
  142. $config['tableName'],
  143. $config['createTable']
  144. );
  145. }
  146. /**
  147. * The only formatter accepted is already loaded in the constructor
  148. *
  149. * @todo enable custom formatters using the WindowsAzure_Storage_DynamicTableEntity class
  150. */
  151. public function setFormatter(
  152. Zend_Service_Log_Formatter_Interface $formatter
  153. )
  154. {
  155. require_once 'Zend/Service/Log/Exception.php';
  156. throw new Zend_Service_Log_Exception(
  157. get_class($this) . ' does not support formatting');
  158. }
  159. /**
  160. * Write a message to the table storage. If buffering is activated, then messages will just be
  161. * added to an internal buffer.
  162. *
  163. * @param array $event
  164. * @return void
  165. * @todo format the event using a formatted, not in this method
  166. */
  167. protected function _write($event)
  168. {
  169. $logEntity = $this->_formatter->format($event);
  170. if ($this->_bufferMessages) {
  171. $this->_messageBuffer[] = $logEntity;
  172. } else {
  173. $this->_tableStorageConnection->insertEntity(
  174. $this->_tableName, $logEntity
  175. );
  176. }
  177. }
  178. }