2022-10-17 21:25:03 +02:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Monolog package.
|
|
|
|
*
|
|
|
|
* (c) Jordi Boggiano <j.boggiano@seld.be>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Monolog\Handler;
|
|
|
|
|
|
|
|
use Monolog\Formatter\ChromePHPFormatter;
|
|
|
|
use Monolog\Formatter\FormatterInterface;
|
2023-07-02 23:57:24 +02:00
|
|
|
use Monolog\Logger;
|
2022-10-17 21:25:03 +02:00
|
|
|
use Monolog\Utils;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler sending logs to the ChromePHP extension (http://www.chromephp.com/)
|
|
|
|
*
|
|
|
|
* This also works out of the box with Firefox 43+
|
|
|
|
*
|
|
|
|
* @author Christophe Coevoet <stof@notk.org>
|
2023-07-02 23:57:24 +02:00
|
|
|
*
|
|
|
|
* @phpstan-import-type Record from \Monolog\Logger
|
2022-10-17 21:25:03 +02:00
|
|
|
*/
|
|
|
|
class ChromePHPHandler extends AbstractProcessingHandler
|
|
|
|
{
|
|
|
|
use WebRequestRecognizerTrait;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Version of the extension
|
|
|
|
*/
|
|
|
|
protected const VERSION = '4.0';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Header name
|
|
|
|
*/
|
|
|
|
protected const HEADER_NAME = 'X-ChromeLogger-Data';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Regular expression to detect supported browsers (matches any Chrome, or Firefox 43+)
|
|
|
|
*/
|
|
|
|
protected const USER_AGENT_REGEX = '{\b(?:Chrome/\d+(?:\.\d+)*|HeadlessChrome|Firefox/(?:4[3-9]|[5-9]\d|\d{3,})(?:\.\d)*)\b}';
|
|
|
|
|
2023-07-02 23:57:24 +02:00
|
|
|
/** @var bool */
|
|
|
|
protected static $initialized = false;
|
2022-10-17 21:25:03 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tracks whether we sent too much data
|
|
|
|
*
|
|
|
|
* Chrome limits the headers to 4KB, so when we sent 3KB we stop sending
|
2023-07-02 23:57:24 +02:00
|
|
|
*
|
|
|
|
* @var bool
|
2022-10-17 21:25:03 +02:00
|
|
|
*/
|
2023-07-02 23:57:24 +02:00
|
|
|
protected static $overflowed = false;
|
2022-10-17 21:25:03 +02:00
|
|
|
|
|
|
|
/** @var mixed[] */
|
2023-07-02 23:57:24 +02:00
|
|
|
protected static $json = [
|
2022-10-17 21:25:03 +02:00
|
|
|
'version' => self::VERSION,
|
|
|
|
'columns' => ['label', 'log', 'backtrace', 'type'],
|
|
|
|
'rows' => [],
|
|
|
|
];
|
|
|
|
|
2023-07-02 23:57:24 +02:00
|
|
|
/** @var bool */
|
|
|
|
protected static $sendHeaders = true;
|
2022-10-17 21:25:03 +02:00
|
|
|
|
2023-07-02 23:57:24 +02:00
|
|
|
public function __construct($level = Logger::DEBUG, bool $bubble = true)
|
2022-10-17 21:25:03 +02:00
|
|
|
{
|
|
|
|
parent::__construct($level, $bubble);
|
|
|
|
if (!function_exists('json_encode')) {
|
|
|
|
throw new \RuntimeException('PHP\'s json extension is required to use Monolog\'s ChromePHPHandler');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-07-02 23:57:24 +02:00
|
|
|
* {@inheritDoc}
|
2022-10-17 21:25:03 +02:00
|
|
|
*/
|
|
|
|
public function handleBatch(array $records): void
|
|
|
|
{
|
|
|
|
if (!$this->isWebRequest()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$messages = [];
|
|
|
|
|
|
|
|
foreach ($records as $record) {
|
2023-07-02 23:57:24 +02:00
|
|
|
if ($record['level'] < $this->level) {
|
2022-10-17 21:25:03 +02:00
|
|
|
continue;
|
|
|
|
}
|
2023-07-02 23:57:24 +02:00
|
|
|
/** @var Record $message */
|
2022-10-17 21:25:03 +02:00
|
|
|
$message = $this->processRecord($record);
|
|
|
|
$messages[] = $message;
|
|
|
|
}
|
|
|
|
|
2023-07-02 23:57:24 +02:00
|
|
|
if (!empty($messages)) {
|
2022-10-17 21:25:03 +02:00
|
|
|
$messages = $this->getFormatter()->formatBatch($messages);
|
|
|
|
self::$json['rows'] = array_merge(self::$json['rows'], $messages);
|
|
|
|
$this->send();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-07-02 23:57:24 +02:00
|
|
|
* {@inheritDoc}
|
2022-10-17 21:25:03 +02:00
|
|
|
*/
|
|
|
|
protected function getDefaultFormatter(): FormatterInterface
|
|
|
|
{
|
|
|
|
return new ChromePHPFormatter();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates & sends header for a record
|
|
|
|
*
|
|
|
|
* @see sendHeader()
|
|
|
|
* @see send()
|
|
|
|
*/
|
2023-07-02 23:57:24 +02:00
|
|
|
protected function write(array $record): void
|
2022-10-17 21:25:03 +02:00
|
|
|
{
|
|
|
|
if (!$this->isWebRequest()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-07-02 23:57:24 +02:00
|
|
|
self::$json['rows'][] = $record['formatted'];
|
2022-10-17 21:25:03 +02:00
|
|
|
|
|
|
|
$this->send();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends the log header
|
|
|
|
*
|
|
|
|
* @see sendHeader()
|
|
|
|
*/
|
|
|
|
protected function send(): void
|
|
|
|
{
|
|
|
|
if (self::$overflowed || !self::$sendHeaders) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!self::$initialized) {
|
|
|
|
self::$initialized = true;
|
|
|
|
|
|
|
|
self::$sendHeaders = $this->headersAccepted();
|
|
|
|
if (!self::$sendHeaders) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
self::$json['request_uri'] = $_SERVER['REQUEST_URI'] ?? '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$json = Utils::jsonEncode(self::$json, Utils::DEFAULT_JSON_FLAGS & ~JSON_UNESCAPED_UNICODE, true);
|
|
|
|
$data = base64_encode($json);
|
|
|
|
if (strlen($data) > 3 * 1024) {
|
|
|
|
self::$overflowed = true;
|
|
|
|
|
2023-07-02 23:57:24 +02:00
|
|
|
$record = [
|
|
|
|
'message' => 'Incomplete logs, chrome header size limit reached',
|
|
|
|
'context' => [],
|
|
|
|
'level' => Logger::WARNING,
|
|
|
|
'level_name' => Logger::getLevelName(Logger::WARNING),
|
|
|
|
'channel' => 'monolog',
|
|
|
|
'datetime' => new \DateTimeImmutable(),
|
|
|
|
'extra' => [],
|
|
|
|
];
|
2022-10-17 21:25:03 +02:00
|
|
|
self::$json['rows'][count(self::$json['rows']) - 1] = $this->getFormatter()->format($record);
|
|
|
|
$json = Utils::jsonEncode(self::$json, Utils::DEFAULT_JSON_FLAGS & ~JSON_UNESCAPED_UNICODE, true);
|
|
|
|
$data = base64_encode($json);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (trim($data) !== '') {
|
|
|
|
$this->sendHeader(static::HEADER_NAME, $data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send header string to the client
|
|
|
|
*/
|
|
|
|
protected function sendHeader(string $header, string $content): void
|
|
|
|
{
|
|
|
|
if (!headers_sent() && self::$sendHeaders) {
|
|
|
|
header(sprintf('%s: %s', $header, $content));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Verifies if the headers are accepted by the current user agent
|
|
|
|
*/
|
|
|
|
protected function headersAccepted(): bool
|
|
|
|
{
|
2023-07-02 23:57:24 +02:00
|
|
|
if (empty($_SERVER['HTTP_USER_AGENT'])) {
|
2022-10-17 21:25:03 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return preg_match(static::USER_AGENT_REGEX, $_SERVER['HTTP_USER_AGENT']) === 1;
|
|
|
|
}
|
|
|
|
}
|