sfAggregateLogger.class.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * sfAggregateLogger logs messages through several loggers.
  11. *
  12. * @package symfony
  13. * @subpackage log
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfAggregateLogger.class.php 9081 2008-05-20 00:47:12Z Carl.Vondrick $
  16. */
  17. class sfAggregateLogger extends sfLogger
  18. {
  19. protected
  20. $dispatcher = null,
  21. $loggers = array();
  22. /**
  23. * Initializes this logger.
  24. *
  25. * Available options:
  26. *
  27. * - loggers: Logger objects that extends sfLogger.
  28. *
  29. * @param sfEventDispatcher $dispatcher A sfEventDispatcher instance
  30. * @param array $options An array of options.
  31. *
  32. * @return Boolean true, if initialization completes successfully, otherwise false.
  33. */
  34. public function initialize(sfEventDispatcher $dispatcher, $options = array())
  35. {
  36. $this->dispatcher = $dispatcher;
  37. if (isset($options['loggers']))
  38. {
  39. if (!is_array($options['loggers']))
  40. {
  41. $options['loggers'] = array($options['loggers']);
  42. }
  43. $this->addLoggers($options['loggers']);
  44. }
  45. return parent::initialize($dispatcher, $options);
  46. }
  47. /**
  48. * Retrieves current loggers.
  49. *
  50. * @return array List of loggers
  51. */
  52. public function getLoggers()
  53. {
  54. return $this->loggers;
  55. }
  56. /**
  57. * Adds an array of loggers.
  58. *
  59. * @param object $loggers An array of Logger objects
  60. */
  61. public function addLoggers($loggers)
  62. {
  63. foreach ($loggers as $logger)
  64. {
  65. $this->addLogger($logger);
  66. }
  67. }
  68. /**
  69. * Adds a logger.
  70. *
  71. * @param object $logger The Logger object
  72. */
  73. public function addLogger(sfLogger $logger)
  74. {
  75. $this->loggers[] = $logger;
  76. $this->dispatcher->disconnect('application.log', array($logger, 'listenToLogEvent'));
  77. }
  78. /**
  79. * Logs a message.
  80. *
  81. * @param string $message Message
  82. * @param string $priority Message priority
  83. */
  84. protected function doLog($message, $priority)
  85. {
  86. foreach ($this->loggers as $logger)
  87. {
  88. $logger->log($message, $priority);
  89. }
  90. }
  91. /**
  92. * Executes the shutdown method.
  93. */
  94. public function shutdown()
  95. {
  96. foreach ($this->loggers as $logger)
  97. {
  98. $logger->shutdown();
  99. }
  100. $this->loggers = array();
  101. }
  102. }