StreamWriter.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. /*
  3. * This file is part of the Monolog package.
  4. *
  5. * (c) Jordi Boggiano <j.boggiano@seld.be>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Monolog\Writer;
  11. use Monolog\Formatter\FormatterInterface;
  12. class StreamWriter implements WriterInterface
  13. {
  14. protected $formatter;
  15. protected $stream;
  16. protected $url;
  17. public function __construct($streamUrl)
  18. {
  19. if (is_resource($streamUrl)) {
  20. $this->stream = $streamUrl;
  21. } else {
  22. $this->url = $streamUrl;
  23. }
  24. }
  25. public function write($log, $level, $message)
  26. {
  27. if (null === $this->stream) {
  28. $this->stream = fopen($this->url, 'a');
  29. }
  30. fwrite($this->stream, $this->formatter->format($log, $level, $message));
  31. }
  32. public function close()
  33. {
  34. fclose($this->stream);
  35. }
  36. public function setFormatter(FormatterInterface $formatter)
  37. {
  38. $this->formatter = $formatter;
  39. }
  40. }