Abstract.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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_Log
  17. * @subpackage Writer
  18. * @copyright Copyright (c) 2005-2010 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. /** Zend_Log_Filter_Priority */
  23. require_once 'Zend/Log/Filter/Priority.php';
  24. /**
  25. * @category Zend
  26. * @package Zend_Log
  27. * @subpackage Writer
  28. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. * @version $Id$
  31. */
  32. abstract class Zend_Log_Writer_Abstract implements Zend_Log_FactoryInterface
  33. {
  34. /**
  35. * @var array of Zend_Log_Filter_Interface
  36. */
  37. protected $_filters = array();
  38. /**
  39. * Formats the log message before writing.
  40. * @var Zend_Log_Formatter_Interface
  41. */
  42. protected $_formatter;
  43. /**
  44. * Add a filter specific to this writer.
  45. *
  46. * @param Zend_Log_Filter_Interface $filter
  47. * @return void
  48. */
  49. public function addFilter($filter)
  50. {
  51. if (is_integer($filter)) {
  52. $filter = new Zend_Log_Filter_Priority($filter);
  53. }
  54. if (!$filter instanceof Zend_Log_Filter_Interface) {
  55. /** @see Zend_Log_Exception */
  56. require_once 'Zend/Log/Exception.php';
  57. throw new Zend_Log_Exception('Invalid filter provided');
  58. }
  59. $this->_filters[] = $filter;
  60. }
  61. /**
  62. * Log a message to this writer.
  63. *
  64. * @param array $event log data event
  65. * @return void
  66. */
  67. public function write($event)
  68. {
  69. foreach ($this->_filters as $filter) {
  70. if (! $filter->accept($event)) {
  71. return;
  72. }
  73. }
  74. // exception occurs on error
  75. $this->_write($event);
  76. }
  77. /**
  78. * Set a new formatter for this writer
  79. *
  80. * @param Zend_Log_Formatter_Interface $formatter
  81. * @return void
  82. */
  83. public function setFormatter(Zend_Log_Formatter_Interface $formatter)
  84. {
  85. $this->_formatter = $formatter;
  86. }
  87. /**
  88. * Perform shutdown activites such as closing open resources
  89. *
  90. * @return void
  91. */
  92. public function shutdown()
  93. {}
  94. /**
  95. * Write a message to the log.
  96. *
  97. * @param array $event log data event
  98. * @return void
  99. */
  100. abstract protected function _write($event);
  101. /**
  102. * Validate and optionally convert the config to array
  103. *
  104. * @param array|Zend_Config $config Zend_Config or Array
  105. * @return array
  106. * @throws Zend_Log_Exception
  107. */
  108. static protected function _parseConfig($config)
  109. {
  110. if ($config instanceof Zend_Config) {
  111. $config = $config->toArray();
  112. }
  113. if (!is_array($config)) {
  114. require_once 'Zend/Log/Exception.php';
  115. throw new Zend_Log_Exception(
  116. 'Configuration must be an array or instance of Zend_Config'
  117. );
  118. }
  119. return $config;
  120. }
  121. }