Abstract.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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-2014 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-2014 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. *
  41. * @var Zend_Log_Formatter_Interface
  42. */
  43. protected $_formatter;
  44. /**
  45. * Add a filter specific to this writer.
  46. *
  47. * @param Zend_Log_Filter_Interface|int $filter Filter class or filter priority
  48. * @return Zend_Log_Writer_Abstract
  49. * @throws Zend_Log_Exception
  50. */
  51. public function addFilter($filter)
  52. {
  53. if (is_int($filter)) {
  54. $filter = new Zend_Log_Filter_Priority($filter);
  55. }
  56. if (!$filter instanceof Zend_Log_Filter_Interface) {
  57. /** @see Zend_Log_Exception */
  58. require_once 'Zend/Log/Exception.php';
  59. throw new Zend_Log_Exception('Invalid filter provided');
  60. }
  61. $this->_filters[] = $filter;
  62. return $this;
  63. }
  64. /**
  65. * Log a message to this writer.
  66. *
  67. * @param array $event log data event
  68. * @return void
  69. */
  70. public function write($event)
  71. {
  72. foreach ($this->_filters as $filter) {
  73. if (! $filter->accept($event)) {
  74. return;
  75. }
  76. }
  77. // exception occurs on error
  78. $this->_write($event);
  79. }
  80. /**
  81. * Set a new formatter for this writer
  82. *
  83. * @param Zend_Log_Formatter_Interface $formatter
  84. * @return Zend_Log_Writer_Abstract
  85. */
  86. public function setFormatter(Zend_Log_Formatter_Interface $formatter)
  87. {
  88. $this->_formatter = $formatter;
  89. return $this;
  90. }
  91. /**
  92. * Perform shutdown activites such as closing open resources
  93. *
  94. * @return void
  95. */
  96. public function shutdown()
  97. {}
  98. /**
  99. * Write a message to the log.
  100. *
  101. * @param array $event log data event
  102. * @return void
  103. */
  104. abstract protected function _write($event);
  105. /**
  106. * Validate and optionally convert the config to array
  107. *
  108. * @param array|Zend_Config $config Zend_Config or Array
  109. * @return array
  110. * @throws Zend_Log_Exception
  111. */
  112. static protected function _parseConfig($config)
  113. {
  114. if ($config instanceof Zend_Config) {
  115. $config = $config->toArray();
  116. }
  117. if (!is_array($config)) {
  118. require_once 'Zend/Log/Exception.php';
  119. throw new Zend_Log_Exception(
  120. 'Configuration must be an array or instance of Zend_Config'
  121. );
  122. }
  123. return $config;
  124. }
  125. }