Deprecated: Constant E_STRICT is deprecated in /var/www/PixelForce/vendor/symfony/error-handler/ErrorHandler.php on line 58

Deprecated: Constant E_STRICT is deprecated in /var/www/PixelForce/vendor/symfony/error-handler/ErrorHandler.php on line 76
Symfony Profiler

vendor/monolog/monolog/src/Monolog/Logger.php line 312

Open in your IDE?
  1. <?php declare(strict_types=1);
  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;
  11. use DateTimeZone;
  12. use Monolog\Handler\HandlerInterface;
  13. use Psr\Log\LoggerInterface;
  14. use Psr\Log\InvalidArgumentException;
  15. use Psr\Log\LogLevel;
  16. use Throwable;
  17. use Stringable;
  18. /**
  19.  * Monolog log channel
  20.  *
  21.  * It contains a stack of Handlers and a stack of Processors,
  22.  * and uses them to store records that are added to it.
  23.  *
  24.  * @author Jordi Boggiano <j.boggiano@seld.be>
  25.  *
  26.  * @phpstan-type Level Logger::DEBUG|Logger::INFO|Logger::NOTICE|Logger::WARNING|Logger::ERROR|Logger::CRITICAL|Logger::ALERT|Logger::EMERGENCY
  27.  * @phpstan-type LevelName 'DEBUG'|'INFO'|'NOTICE'|'WARNING'|'ERROR'|'CRITICAL'|'ALERT'|'EMERGENCY'
  28.  * @phpstan-type Record array{message: string, context: mixed[], level: Level, level_name: LevelName, channel: string, datetime: \DateTimeImmutable, extra: mixed[]}
  29.  */
  30. class Logger implements LoggerInterfaceResettableInterface
  31. {
  32.     /**
  33.      * Detailed debug information
  34.      */
  35.     public const DEBUG 100;
  36.     /**
  37.      * Interesting events
  38.      *
  39.      * Examples: User logs in, SQL logs.
  40.      */
  41.     public const INFO 200;
  42.     /**
  43.      * Uncommon events
  44.      */
  45.     public const NOTICE 250;
  46.     /**
  47.      * Exceptional occurrences that are not errors
  48.      *
  49.      * Examples: Use of deprecated APIs, poor use of an API,
  50.      * undesirable things that are not necessarily wrong.
  51.      */
  52.     public const WARNING 300;
  53.     /**
  54.      * Runtime errors
  55.      */
  56.     public const ERROR 400;
  57.     /**
  58.      * Critical conditions
  59.      *
  60.      * Example: Application component unavailable, unexpected exception.
  61.      */
  62.     public const CRITICAL 500;
  63.     /**
  64.      * Action must be taken immediately
  65.      *
  66.      * Example: Entire website down, database unavailable, etc.
  67.      * This should trigger the SMS alerts and wake you up.
  68.      */
  69.     public const ALERT 550;
  70.     /**
  71.      * Urgent alert.
  72.      */
  73.     public const EMERGENCY 600;
  74.     /**
  75.      * Monolog API version
  76.      *
  77.      * This is only bumped when API breaks are done and should
  78.      * follow the major version of the library
  79.      *
  80.      * @var int
  81.      */
  82.     public const API 2;
  83.     /**
  84.      * This is a static variable and not a constant to serve as an extension point for custom levels
  85.      *
  86.      * @var array<int, string> $levels Logging levels with the levels as key
  87.      *
  88.      * @phpstan-var array<Level, LevelName> $levels Logging levels with the levels as key
  89.      */
  90.     protected static $levels = [
  91.         self::DEBUG     => 'DEBUG',
  92.         self::INFO      => 'INFO',
  93.         self::NOTICE    => 'NOTICE',
  94.         self::WARNING   => 'WARNING',
  95.         self::ERROR     => 'ERROR',
  96.         self::CRITICAL  => 'CRITICAL',
  97.         self::ALERT     => 'ALERT',
  98.         self::EMERGENCY => 'EMERGENCY',
  99.     ];
  100.     /**
  101.      * @var string
  102.      */
  103.     protected $name;
  104.     /**
  105.      * The handler stack
  106.      *
  107.      * @var HandlerInterface[]
  108.      */
  109.     protected $handlers;
  110.     /**
  111.      * Processors that will process all log records
  112.      *
  113.      * To process records of a single handler instead, add the processor on that specific handler
  114.      *
  115.      * @var callable[]
  116.      */
  117.     protected $processors;
  118.     /**
  119.      * @var bool
  120.      */
  121.     protected $microsecondTimestamps true;
  122.     /**
  123.      * @var DateTimeZone
  124.      */
  125.     protected $timezone;
  126.     /**
  127.      * @var callable|null
  128.      */
  129.     protected $exceptionHandler;
  130.     /**
  131.      * @var int Keeps track of depth to prevent infinite logging loops
  132.      */
  133.     private $logDepth 0;
  134.     /**
  135.      * @var bool Whether to detect infinite logging loops
  136.      *
  137.      * This can be disabled via {@see useLoggingLoopDetection} if you have async handlers that do not play well with this
  138.      */
  139.     private $detectCycles true;
  140.     /**
  141.      * @psalm-param array<callable(array): array> $processors
  142.      *
  143.      * @param string             $name       The logging channel, a simple descriptive name that is attached to all log records
  144.      * @param HandlerInterface[] $handlers   Optional stack of handlers, the first one in the array is called first, etc.
  145.      * @param callable[]         $processors Optional array of processors
  146.      * @param DateTimeZone|null  $timezone   Optional timezone, if not provided date_default_timezone_get() will be used
  147.      */
  148.     public function __construct(string $name, array $handlers = [], array $processors = [], ?DateTimeZone $timezone null)
  149.     {
  150.         $this->name $name;
  151.         $this->setHandlers($handlers);
  152.         $this->processors $processors;
  153.         $this->timezone $timezone ?: new DateTimeZone(date_default_timezone_get() ?: 'UTC');
  154.     }
  155.     public function getName(): string
  156.     {
  157.         return $this->name;
  158.     }
  159.     /**
  160.      * Return a new cloned instance with the name changed
  161.      */
  162.     public function withName(string $name): self
  163.     {
  164.         $new = clone $this;
  165.         $new->name $name;
  166.         return $new;
  167.     }
  168.     /**
  169.      * Pushes a handler on to the stack.
  170.      */
  171.     public function pushHandler(HandlerInterface $handler): self
  172.     {
  173.         array_unshift($this->handlers$handler);
  174.         return $this;
  175.     }
  176.     /**
  177.      * Pops a handler from the stack
  178.      *
  179.      * @throws \LogicException If empty handler stack
  180.      */
  181.     public function popHandler(): HandlerInterface
  182.     {
  183.         if (!$this->handlers) {
  184.             throw new \LogicException('You tried to pop from an empty handler stack.');
  185.         }
  186.         return array_shift($this->handlers);
  187.     }
  188.     /**
  189.      * Set handlers, replacing all existing ones.
  190.      *
  191.      * If a map is passed, keys will be ignored.
  192.      *
  193.      * @param HandlerInterface[] $handlers
  194.      */
  195.     public function setHandlers(array $handlers): self
  196.     {
  197.         $this->handlers = [];
  198.         foreach (array_reverse($handlers) as $handler) {
  199.             $this->pushHandler($handler);
  200.         }
  201.         return $this;
  202.     }
  203.     /**
  204.      * @return HandlerInterface[]
  205.      */
  206.     public function getHandlers(): array
  207.     {
  208.         return $this->handlers;
  209.     }
  210.     /**
  211.      * Adds a processor on to the stack.
  212.      */
  213.     public function pushProcessor(callable $callback): self
  214.     {
  215.         array_unshift($this->processors$callback);
  216.         return $this;
  217.     }
  218.     /**
  219.      * Removes the processor on top of the stack and returns it.
  220.      *
  221.      * @throws \LogicException If empty processor stack
  222.      * @return callable
  223.      */
  224.     public function popProcessor(): callable
  225.     {
  226.         if (!$this->processors) {
  227.             throw new \LogicException('You tried to pop from an empty processor stack.');
  228.         }
  229.         return array_shift($this->processors);
  230.     }
  231.     /**
  232.      * @return callable[]
  233.      */
  234.     public function getProcessors(): array
  235.     {
  236.         return $this->processors;
  237.     }
  238.     /**
  239.      * Control the use of microsecond resolution timestamps in the 'datetime'
  240.      * member of new records.
  241.      *
  242.      * As of PHP7.1 microseconds are always included by the engine, so
  243.      * there is no performance penalty and Monolog 2 enabled microseconds
  244.      * by default. This function lets you disable them though in case you want
  245.      * to suppress microseconds from the output.
  246.      *
  247.      * @param bool $micro True to use microtime() to create timestamps
  248.      */
  249.     public function useMicrosecondTimestamps(bool $micro): self
  250.     {
  251.         $this->microsecondTimestamps $micro;
  252.         return $this;
  253.     }
  254.     public function useLoggingLoopDetection(bool $detectCycles): self
  255.     {
  256.         $this->detectCycles $detectCycles;
  257.         return $this;
  258.     }
  259.     /**
  260.      * Adds a log record.
  261.      *
  262.      * @param  int               $level    The logging level
  263.      * @param  string            $message  The log message
  264.      * @param  mixed[]           $context  The log context
  265.      * @param  DateTimeImmutable $datetime Optional log date to log into the past or future
  266.      * @return bool              Whether the record has been processed
  267.      *
  268.      * @phpstan-param Level $level
  269.      */
  270.     public function addRecord(int $levelstring $message, array $context = [], DateTimeImmutable $datetime null): bool
  271.     {
  272.         if ($this->detectCycles) {
  273.             $this->logDepth += 1;
  274.         }
  275.         if ($this->logDepth === 3) {
  276.             $this->warning('A possible infinite logging loop was detected and aborted. It appears some of your handler code is triggering logging, see the previous log record for a hint as to what may be the cause.');
  277.             return false;
  278.         } elseif ($this->logDepth >= 5) { // log depth 4 is let through so we can log the warning above
  279.             return false;
  280.         }
  281.         try {
  282.             $record null;
  283.             foreach ($this->handlers as $handler) {
  284.                 if (null === $record) {
  285.                     // skip creating the record as long as no handler is going to handle it
  286.                     if (!$handler->isHandling(['level' => $level])) {
  287.                         continue;
  288.                     }
  289.                     $levelName = static::getLevelName($level);
  290.                     $record = [
  291.                         'message' => $message,
  292.                         'context' => $context,
  293.                         'level' => $level,
  294.                         'level_name' => $levelName,
  295.                         'channel' => $this->name,
  296.                         'datetime' => $datetime ?? new DateTimeImmutable($this->microsecondTimestamps$this->timezone),
  297.                         'extra' => [],
  298.                     ];
  299.                     try {
  300.                         foreach ($this->processors as $processor) {
  301.                             $record $processor($record);
  302.                         }
  303.                     } catch (Throwable $e) {
  304.                         $this->handleException($e$record);
  305.                         return true;
  306.                     }
  307.                 }
  308.                 // once the record exists, send it to all handlers as long as the bubbling chain is not interrupted
  309.                 try {
  310.                     if (true === $handler->handle($record)) {
  311.                         break;
  312.                     }
  313.                 } catch (Throwable $e) {
  314.                     $this->handleException($e$record);
  315.                     return true;
  316.                 }
  317.             }
  318.         } finally {
  319.             if ($this->detectCycles) {
  320.                 $this->logDepth--;
  321.             }
  322.         }
  323.         return null !== $record;
  324.     }
  325.     /**
  326.      * Ends a log cycle and frees all resources used by handlers.
  327.      *
  328.      * Closing a Handler means flushing all buffers and freeing any open resources/handles.
  329.      * Handlers that have been closed should be able to accept log records again and re-open
  330.      * themselves on demand, but this may not always be possible depending on implementation.
  331.      *
  332.      * This is useful at the end of a request and will be called automatically on every handler
  333.      * when they get destructed.
  334.      */
  335.     public function close(): void
  336.     {
  337.         foreach ($this->handlers as $handler) {
  338.             $handler->close();
  339.         }
  340.     }
  341.     /**
  342.      * Ends a log cycle and resets all handlers and processors to their initial state.
  343.      *
  344.      * Resetting a Handler or a Processor means flushing/cleaning all buffers, resetting internal
  345.      * state, and getting it back to a state in which it can receive log records again.
  346.      *
  347.      * This is useful in case you want to avoid logs leaking between two requests or jobs when you
  348.      * have a long running process like a worker or an application server serving multiple requests
  349.      * in one process.
  350.      */
  351.     public function reset(): void
  352.     {
  353.         foreach ($this->handlers as $handler) {
  354.             if ($handler instanceof ResettableInterface) {
  355.                 $handler->reset();
  356.             }
  357.         }
  358.         foreach ($this->processors as $processor) {
  359.             if ($processor instanceof ResettableInterface) {
  360.                 $processor->reset();
  361.             }
  362.         }
  363.     }
  364.     /**
  365.      * Gets all supported logging levels.
  366.      *
  367.      * @return array<string, int> Assoc array with human-readable level names => level codes.
  368.      * @phpstan-return array<LevelName, Level>
  369.      */
  370.     public static function getLevels(): array
  371.     {
  372.         return array_flip(static::$levels);
  373.     }
  374.     /**
  375.      * Gets the name of the logging level.
  376.      *
  377.      * @throws \Psr\Log\InvalidArgumentException If level is not defined
  378.      *
  379.      * @phpstan-param  Level     $level
  380.      * @phpstan-return LevelName
  381.      */
  382.     public static function getLevelName(int $level): string
  383.     {
  384.         if (!isset(static::$levels[$level])) {
  385.             throw new InvalidArgumentException('Level "'.$level.'" is not defined, use one of: '.implode(', 'array_keys(static::$levels)));
  386.         }
  387.         return static::$levels[$level];
  388.     }
  389.     /**
  390.      * Converts PSR-3 levels to Monolog ones if necessary
  391.      *
  392.      * @param  string|int                        $level Level number (monolog) or name (PSR-3)
  393.      * @throws \Psr\Log\InvalidArgumentException If level is not defined
  394.      *
  395.      * @phpstan-param  Level|LevelName|LogLevel::* $level
  396.      * @phpstan-return Level
  397.      */
  398.     public static function toMonologLevel($level): int
  399.     {
  400.         if (is_string($level)) {
  401.             if (is_numeric($level)) {
  402.                 /** @phpstan-ignore-next-line */
  403.                 return intval($level);
  404.             }
  405.             // Contains chars of all log levels and avoids using strtoupper() which may have
  406.             // strange results depending on locale (for example, "i" will become "İ" in Turkish locale)
  407.             $upper strtr($level'abcdefgilmnortuwy''ABCDEFGILMNORTUWY');
  408.             if (defined(__CLASS__.'::'.$upper)) {
  409.                 return constant(__CLASS__ '::' $upper);
  410.             }
  411.             throw new InvalidArgumentException('Level "'.$level.'" is not defined, use one of: '.implode(', 'array_keys(static::$levels) + static::$levels));
  412.         }
  413.         if (!is_int($level)) {
  414.             throw new InvalidArgumentException('Level "'.var_export($leveltrue).'" is not defined, use one of: '.implode(', 'array_keys(static::$levels) + static::$levels));
  415.         }
  416.         return $level;
  417.     }
  418.     /**
  419.      * Checks whether the Logger has a handler that listens on the given level
  420.      *
  421.      * @phpstan-param Level $level
  422.      */
  423.     public function isHandling(int $level): bool
  424.     {
  425.         $record = [
  426.             'level' => $level,
  427.         ];
  428.         foreach ($this->handlers as $handler) {
  429.             if ($handler->isHandling($record)) {
  430.                 return true;
  431.             }
  432.         }
  433.         return false;
  434.     }
  435.     /**
  436.      * Set a custom exception handler that will be called if adding a new record fails
  437.      *
  438.      * The callable will receive an exception object and the record that failed to be logged
  439.      */
  440.     public function setExceptionHandler(?callable $callback): self
  441.     {
  442.         $this->exceptionHandler $callback;
  443.         return $this;
  444.     }
  445.     public function getExceptionHandler(): ?callable
  446.     {
  447.         return $this->exceptionHandler;
  448.     }
  449.     /**
  450.      * Adds a log record at an arbitrary level.
  451.      *
  452.      * This method allows for compatibility with common interfaces.
  453.      *
  454.      * @param mixed             $level   The log level
  455.      * @param string|Stringable $message The log message
  456.      * @param mixed[]           $context The log context
  457.      *
  458.      * @phpstan-param Level|LevelName|LogLevel::* $level
  459.      */
  460.     public function log($level$message, array $context = []): void
  461.     {
  462.         if (!is_int($level) && !is_string($level)) {
  463.             throw new \InvalidArgumentException('$level is expected to be a string or int');
  464.         }
  465.         $level = static::toMonologLevel($level);
  466.         $this->addRecord($level, (string) $message$context);
  467.     }
  468.     /**
  469.      * Adds a log record at the DEBUG level.
  470.      *
  471.      * This method allows for compatibility with common interfaces.
  472.      *
  473.      * @param string|Stringable $message The log message
  474.      * @param mixed[]           $context The log context
  475.      */
  476.     public function debug($message, array $context = []): void
  477.     {
  478.         $this->addRecord(static::DEBUG, (string) $message$context);
  479.     }
  480.     /**
  481.      * Adds a log record at the INFO level.
  482.      *
  483.      * This method allows for compatibility with common interfaces.
  484.      *
  485.      * @param string|Stringable $message The log message
  486.      * @param mixed[]           $context The log context
  487.      */
  488.     public function info($message, array $context = []): void
  489.     {
  490.         $this->addRecord(static::INFO, (string) $message$context);
  491.     }
  492.     /**
  493.      * Adds a log record at the NOTICE level.
  494.      *
  495.      * This method allows for compatibility with common interfaces.
  496.      *
  497.      * @param string|Stringable $message The log message
  498.      * @param mixed[]           $context The log context
  499.      */
  500.     public function notice($message, array $context = []): void
  501.     {
  502.         $this->addRecord(static::NOTICE, (string) $message$context);
  503.     }
  504.     /**
  505.      * Adds a log record at the WARNING level.
  506.      *
  507.      * This method allows for compatibility with common interfaces.
  508.      *
  509.      * @param string|Stringable $message The log message
  510.      * @param mixed[]           $context The log context
  511.      */
  512.     public function warning($message, array $context = []): void
  513.     {
  514.         $this->addRecord(static::WARNING, (string) $message$context);
  515.     }
  516.     /**
  517.      * Adds a log record at the ERROR level.
  518.      *
  519.      * This method allows for compatibility with common interfaces.
  520.      *
  521.      * @param string|Stringable $message The log message
  522.      * @param mixed[]           $context The log context
  523.      */
  524.     public function error($message, array $context = []): void
  525.     {
  526.         $this->addRecord(static::ERROR, (string) $message$context);
  527.     }
  528.     /**
  529.      * Adds a log record at the CRITICAL level.
  530.      *
  531.      * This method allows for compatibility with common interfaces.
  532.      *
  533.      * @param string|Stringable $message The log message
  534.      * @param mixed[]           $context The log context
  535.      */
  536.     public function critical($message, array $context = []): void
  537.     {
  538.         $this->addRecord(static::CRITICAL, (string) $message$context);
  539.     }
  540.     /**
  541.      * Adds a log record at the ALERT level.
  542.      *
  543.      * This method allows for compatibility with common interfaces.
  544.      *
  545.      * @param string|Stringable $message The log message
  546.      * @param mixed[]           $context The log context
  547.      */
  548.     public function alert($message, array $context = []): void
  549.     {
  550.         $this->addRecord(static::ALERT, (string) $message$context);
  551.     }
  552.     /**
  553.      * Adds a log record at the EMERGENCY level.
  554.      *
  555.      * This method allows for compatibility with common interfaces.
  556.      *
  557.      * @param string|Stringable $message The log message
  558.      * @param mixed[]           $context The log context
  559.      */
  560.     public function emergency($message, array $context = []): void
  561.     {
  562.         $this->addRecord(static::EMERGENCY, (string) $message$context);
  563.     }
  564.     /**
  565.      * Sets the timezone to be used for the timestamp of log records.
  566.      */
  567.     public function setTimezone(DateTimeZone $tz): self
  568.     {
  569.         $this->timezone $tz;
  570.         return $this;
  571.     }
  572.     /**
  573.      * Returns the timezone to be used for the timestamp of log records.
  574.      */
  575.     public function getTimezone(): DateTimeZone
  576.     {
  577.         return $this->timezone;
  578.     }
  579.     /**
  580.      * Delegates exception management to the custom exception handler,
  581.      * or throws the exception if no custom handler is set.
  582.      *
  583.      * @param array $record
  584.      * @phpstan-param Record $record
  585.      */
  586.     protected function handleException(Throwable $e, array $record): void
  587.     {
  588.         if (!$this->exceptionHandler) {
  589.             throw $e;
  590.         }
  591.         ($this->exceptionHandler)($e$record);
  592.     }
  593. }