PageRenderTime 47ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Log/Writer/Abstract.php

https://bitbucket.org/dbaltas/zend-framework-1.x-on-git
PHP | 138 lines | 49 code | 14 blank | 75 comment | 5 complexity | 0d9185194d914829042bc7a6baf20e89 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, MIT
  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-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Abstract.php 24593 2012-01-05 20:35:02Z matthew $
  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-2012 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. * @version $Id: Abstract.php 24593 2012-01-05 20:35:02Z matthew $
  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 $filter
  48. * @return Zend_Log_Writer_Abstract
  49. */
  50. public function addFilter($filter)
  51. {
  52. if (is_int($filter)) {
  53. $filter = new Zend_Log_Filter_Priority($filter);
  54. }
  55. if (!$filter instanceof Zend_Log_Filter_Interface) {
  56. /** @see Zend_Log_Exception */
  57. require_once 'Zend/Log/Exception.php';
  58. throw new Zend_Log_Exception('Invalid filter provided');
  59. }
  60. $this->_filters[] = $filter;
  61. return $this;
  62. }
  63. /**
  64. * Log a message to this writer.
  65. *
  66. * @param array $event log data event
  67. * @return void
  68. */
  69. public function write($event)
  70. {
  71. foreach ($this->_filters as $filter) {
  72. if (! $filter->accept($event)) {
  73. return;
  74. }
  75. }
  76. // exception occurs on error
  77. $this->_write($event);
  78. }
  79. /**
  80. * Set a new formatter for this writer
  81. *
  82. * @param Zend_Log_Formatter_Interface $formatter
  83. * @return Zend_Log_Writer_Abstract
  84. */
  85. public function setFormatter(Zend_Log_Formatter_Interface $formatter)
  86. {
  87. $this->_formatter = $formatter;
  88. return $this;
  89. }
  90. /**
  91. * Perform shutdown activites such as closing open resources
  92. *
  93. * @return void
  94. */
  95. public function shutdown()
  96. {}
  97. /**
  98. * Write a message to the log.
  99. *
  100. * @param array $event log data event
  101. * @return void
  102. */
  103. abstract protected function _write($event);
  104. /**
  105. * Validate and optionally convert the config to array
  106. *
  107. * @param array|Zend_Config $config Zend_Config or Array
  108. * @return array
  109. * @throws Zend_Log_Exception
  110. */
  111. static protected function _parseConfig($config)
  112. {
  113. if ($config instanceof Zend_Config) {
  114. $config = $config->toArray();
  115. }
  116. if (!is_array($config)) {
  117. require_once 'Zend/Log/Exception.php';
  118. throw new Zend_Log_Exception(
  119. 'Configuration must be an array or instance of Zend_Config'
  120. );
  121. }
  122. return $config;
  123. }
  124. }