[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Log/ -> DelegatingPsrLogger.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2017 Open Source Matters, Inc. <https://www.joomla.org>
   7   * @license    GNU General Public License version 2 or later; see LICENSE.txt
   8   */
   9  
  10  namespace Joomla\CMS\Log;
  11  
  12  use Psr\Log\AbstractLogger;
  13  use Psr\Log\InvalidArgumentException;
  14  use Psr\Log\LogLevel;
  15  
  16  // phpcs:disable PSR1.Files.SideEffects
  17  \defined('JPATH_PLATFORM') or die;
  18  // phpcs:enable PSR1.Files.SideEffects
  19  
  20  /**
  21   * Delegating logger which delegates log messages received from the PSR-3 interface to the Joomla! Log object.
  22   *
  23   * @since  3.8.0
  24   */
  25  class DelegatingPsrLogger extends AbstractLogger
  26  {
  27      /**
  28       * The Log instance to delegate messages to.
  29       *
  30       * @var    Log
  31       * @since  3.8.0
  32       */
  33      protected $logger;
  34  
  35      /**
  36       * Mapping array to map a PSR-3 level to a Joomla priority.
  37       *
  38       * @var    array
  39       * @since  3.8.0
  40       */
  41      protected $priorityMap = array(
  42          LogLevel::EMERGENCY => Log::EMERGENCY,
  43          LogLevel::ALERT     => Log::ALERT,
  44          LogLevel::CRITICAL  => Log::CRITICAL,
  45          LogLevel::ERROR     => Log::ERROR,
  46          LogLevel::WARNING   => Log::WARNING,
  47          LogLevel::NOTICE    => Log::NOTICE,
  48          LogLevel::INFO      => Log::INFO,
  49          LogLevel::DEBUG     => Log::DEBUG
  50      );
  51  
  52      /**
  53       * Constructor.
  54       *
  55       * @param   Log  $logger  The Log instance to delegate messages to.
  56       *
  57       * @since   3.8.0
  58       */
  59      public function __construct(Log $logger)
  60      {
  61          $this->logger = $logger;
  62      }
  63  
  64      /**
  65       * Logs with an arbitrary level.
  66       *
  67       * @param   mixed   $level    The log level.
  68       * @param   string  $message  The log message.
  69       * @param   array   $context  Additional message context.
  70       *
  71       * @return  void
  72       *
  73       * @since   3.8.0
  74       * @throws  InvalidArgumentException
  75       */
  76      public function log($level, $message, array $context = array())
  77      {
  78          // Make sure the log level is valid
  79          if (!\array_key_exists($level, $this->priorityMap)) {
  80              throw new \InvalidArgumentException('An invalid log level has been given.');
  81          }
  82  
  83          // Map the level to Joomla's priority
  84          $priority = $this->priorityMap[$level];
  85  
  86          $category = null;
  87          $date     = null;
  88  
  89          // If a message category is given, map it
  90          if (!empty($context['category'])) {
  91              $category = $context['category'];
  92          }
  93  
  94          // If a message timestamp is given, map it
  95          if (!empty($context['date'])) {
  96              $date = $context['date'];
  97          }
  98  
  99          // Joomla's logging API will only process a string or a LogEntry object, if $message is an object without __toString() we can't use it
 100          if (!\is_string($message) && !($message instanceof LogEntry)) {
 101              if (!\is_object($message) || !method_exists($message, '__toString')) {
 102                  throw new \InvalidArgumentException(
 103                      'The message must be a string, a LogEntry object, or an object implementing the __toString() method.'
 104                  );
 105              }
 106  
 107              $message = (string) $message;
 108          }
 109  
 110          $this->logger->add($message, $priority, $category, $date, $context);
 111      }
 112  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer