[ Index ]

PHP Cross Reference of phpBB-3.3.3-deutsch

title

Body

[close]

/vendor/symfony/http-kernel/Log/ -> Logger.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of the Symfony package.
   5   *
   6   * (c) Fabien Potencier <fabien@symfony.com>
   7   *
   8   * For the full copyright and license information, please view the LICENSE
   9   * file that was distributed with this source code.
  10   */
  11  
  12  namespace Symfony\Component\HttpKernel\Log;
  13  
  14  use Psr\Log\AbstractLogger;
  15  use Psr\Log\InvalidArgumentException;
  16  use Psr\Log\LogLevel;
  17  
  18  /**
  19   * Minimalist PSR-3 logger designed to write in stderr or any other stream.
  20   *
  21   * @author Kévin Dunglas <dunglas@gmail.com>
  22   */
  23  class Logger extends AbstractLogger
  24  {
  25      private static $levels = [
  26          LogLevel::DEBUG => 0,
  27          LogLevel::INFO => 1,
  28          LogLevel::NOTICE => 2,
  29          LogLevel::WARNING => 3,
  30          LogLevel::ERROR => 4,
  31          LogLevel::CRITICAL => 5,
  32          LogLevel::ALERT => 6,
  33          LogLevel::EMERGENCY => 7,
  34      ];
  35  
  36      private $minLevelIndex;
  37      private $formatter;
  38      private $handle;
  39  
  40      public function __construct($minLevel = null, $output = null, callable $formatter = null)
  41      {
  42          if (null === $minLevel) {
  43              $minLevel = null === $output || 'php://stdout' === $output || 'php://stderr' === $output ? LogLevel::ERROR : LogLevel::WARNING;
  44  
  45              if (isset($_ENV['SHELL_VERBOSITY']) || isset($_SERVER['SHELL_VERBOSITY'])) {
  46                  switch ((int) (isset($_ENV['SHELL_VERBOSITY']) ? $_ENV['SHELL_VERBOSITY'] : $_SERVER['SHELL_VERBOSITY'])) {
  47                      case -1: $minLevel = LogLevel::ERROR; break;
  48                      case 1: $minLevel = LogLevel::NOTICE; break;
  49                      case 2: $minLevel = LogLevel::INFO; break;
  50                      case 3: $minLevel = LogLevel::DEBUG; break;
  51                  }
  52              }
  53          }
  54  
  55          if (!isset(self::$levels[$minLevel])) {
  56              throw new InvalidArgumentException(sprintf('The log level "%s" does not exist.', $minLevel));
  57          }
  58  
  59          $this->minLevelIndex = self::$levels[$minLevel];
  60          $this->formatter = $formatter ?: [$this, 'format'];
  61          if ($output && false === $this->handle = \is_resource($output) ? $output : @fopen($output, 'a')) {
  62              throw new InvalidArgumentException(sprintf('Unable to open "%s".', $output));
  63          }
  64      }
  65  
  66      /**
  67       * {@inheritdoc}
  68       */
  69      public function log($level, $message, array $context = [])
  70      {
  71          if (!isset(self::$levels[$level])) {
  72              throw new InvalidArgumentException(sprintf('The log level "%s" does not exist.', $level));
  73          }
  74  
  75          if (self::$levels[$level] < $this->minLevelIndex) {
  76              return;
  77          }
  78  
  79          $formatter = $this->formatter;
  80          if ($this->handle) {
  81              @fwrite($this->handle, $formatter($level, $message, $context));
  82          } else {
  83              error_log($formatter($level, $message, $context, false));
  84          }
  85      }
  86  
  87      /**
  88       * @param string $level
  89       * @param string $message
  90       *
  91       * @return string
  92       */
  93      private function format($level, $message, array $context, $prefixDate = true)
  94      {
  95          if (false !== strpos($message, '{')) {
  96              $replacements = [];
  97              foreach ($context as $key => $val) {
  98                  if (null === $val || is_scalar($val) || (\is_object($val) && method_exists($val, '__toString'))) {
  99                      $replacements["{{$key}}"] = $val;
 100                  } elseif ($val instanceof \DateTimeInterface) {
 101                      $replacements["{{$key}}"] = $val->format(\DateTime::RFC3339);
 102                  } elseif (\is_object($val)) {
 103                      $replacements["{{$key}}"] = '[object '.\get_class($val).']';
 104                  } else {
 105                      $replacements["{{$key}}"] = '['.\gettype($val).']';
 106                  }
 107              }
 108  
 109              $message = strtr($message, $replacements);
 110          }
 111  
 112          $log = sprintf('[%s] %s', $level, $message).\PHP_EOL;
 113          if ($prefixDate) {
 114              $log = date(\DateTime::RFC3339).' '.$log;
 115          }
 116  
 117          return $log;
 118      }
 119  }


Generated: Sun Feb 14 20:08:31 2021 Cross-referenced by PHPXref 0.7.1