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