Abstract.php 3.2 KB

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