sfStreamLogger.class.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) 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. * sfStreamLogger logs messages to a PHP stream.
  11. *
  12. * @package symfony
  13. * @subpackage log
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfStreamLogger.class.php 9081 2008-05-20 00:47:12Z Carl.Vondrick $
  16. */
  17. class sfStreamLogger extends sfLogger
  18. {
  19. protected
  20. $stream = null;
  21. /**
  22. * Initializes this logger.
  23. *
  24. * Available options:
  25. *
  26. * - stream: A PHP stream
  27. *
  28. * @param sfEventDispatcher $dispatcher A sfEventDispatcher instance
  29. * @param array $options An array of options.
  30. *
  31. * @return Boolean true, if initialization completes successfully, otherwise false.
  32. */
  33. public function initialize(sfEventDispatcher $dispatcher, $options = array())
  34. {
  35. if (!isset($options['stream']))
  36. {
  37. throw new sfConfigurationException('You must provide a "stream" option for this logger.');
  38. }
  39. else
  40. {
  41. if (is_resource($options['stream']) && 'stream' != get_resource_type($options['stream']))
  42. {
  43. throw new sfConfigurationException('The provided "stream" option is not a stream.');
  44. }
  45. }
  46. $this->stream = $options['stream'];
  47. return parent::initialize($dispatcher, $options);
  48. }
  49. /**
  50. * Sets the PHP stream to use for this logger.
  51. *
  52. * @param stream $stream A php stream
  53. */
  54. public function setStream($stream)
  55. {
  56. $this->stream = $stream;
  57. }
  58. /**
  59. * Logs a message.
  60. *
  61. * @param string $message Message
  62. * @param string $priority Message priority
  63. */
  64. protected function doLog($message, $priority)
  65. {
  66. fwrite($this->stream, $message.PHP_EOL);
  67. flush();
  68. }
  69. }