SimpleFormatter.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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\Formatter;
  11. use Monolog\Logger;
  12. class SimpleFormatter implements FormatterInterface
  13. {
  14. const SIMPLE_FORMAT = "[%date%] %log%.%level%: %message%\n";
  15. const SIMPLE_DATE = "Y-m-d H:i:s";
  16. protected $format;
  17. protected $dateFormat;
  18. public function __construct($format = null, $dateFormat = null)
  19. {
  20. $this->format = $format ?: self::SIMPLE_FORMAT;
  21. $this->dateFormat = $dateFormat ?: self::SIMPLE_DATE;
  22. }
  23. public function format($log, $level, $message)
  24. {
  25. $defaults = array(
  26. 'log' => $log,
  27. 'level' => Logger::getLevelName($level),
  28. 'date' => date($this->dateFormat),
  29. );
  30. if (is_array($message)) {
  31. $vars = array_merge($defaults, $message);
  32. } else {
  33. $vars = $defaults;
  34. $vars['message'] = $message;
  35. }
  36. $message = $this->format;
  37. foreach ($vars as $var => $val) {
  38. $message = str_replace('%'.$var.'%', $val, $message);
  39. }
  40. return $message;
  41. }
  42. }