Abstract.php 3.7 KB

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