PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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

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