PageRenderTime 19ms CodeModel.GetById 9ms app.highlight 6ms RepoModel.GetById 2ms app.codeStats 0ms

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

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