From 0f42ac075f189e97ef3311dc65f7eec30dbc380a Mon Sep 17 00:00:00 2001 From: kolaente Date: Fri, 23 Jun 2023 10:32:17 +0200 Subject: [PATCH 1/5] chore: add flake --- ...e-a5d5b61aa8a61b7d9d765e1daf971a9a578f1cfa | 1 + ...5d5b61aa8a61b7d9d765e1daf971a9a578f1cfa.rc | 1 + .envrc | 1 + flake.lock | 25 +++++++++++ flake.nix | 44 +++++++++++++++++++ 5 files changed, 72 insertions(+) create mode 120000 .direnv/flake-profile-a5d5b61aa8a61b7d9d765e1daf971a9a578f1cfa create mode 100644 .direnv/flake-profile-a5d5b61aa8a61b7d9d765e1daf971a9a578f1cfa.rc create mode 100644 .envrc create mode 100644 flake.lock create mode 100644 flake.nix diff --git a/.direnv/flake-profile-a5d5b61aa8a61b7d9d765e1daf971a9a578f1cfa b/.direnv/flake-profile-a5d5b61aa8a61b7d9d765e1daf971a9a578f1cfa new file mode 120000 index 0000000..4fc1957 --- /dev/null +++ b/.direnv/flake-profile-a5d5b61aa8a61b7d9d765e1daf971a9a578f1cfa @@ -0,0 +1 @@ +/home/konrad/www/graby/.direnv/flake-profile.17606 \ No newline at end of file diff --git a/.direnv/flake-profile-a5d5b61aa8a61b7d9d765e1daf971a9a578f1cfa.rc b/.direnv/flake-profile-a5d5b61aa8a61b7d9d765e1daf971a9a578f1cfa.rc new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/.direnv/flake-profile-a5d5b61aa8a61b7d9d765e1daf971a9a578f1cfa.rc @@ -0,0 +1 @@ + diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..3550a30 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use flake diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..18ec71f --- /dev/null +++ b/flake.lock @@ -0,0 +1,25 @@ +{ + "nodes": { + "nixpkgs": { + "locked": { + "lastModified": 1687274257, + "narHash": "sha256-TutzPriQcZ8FghDhEolnHcYU2oHIG5XWF+/SUBNnAOE=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "2c9ecd1f0400076a4d6b2193ad468ff0a7e7fdc5", + "type": "github" + }, + "original": { + "id": "nixpkgs", + "type": "indirect" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..8956e39 --- /dev/null +++ b/flake.nix @@ -0,0 +1,44 @@ +{ + description = "Dev environment"; + + outputs = { self, nixpkgs }: + let pkgs = nixpkgs.legacyPackages.x86_64-linux; + in { + defaultPackage.x86_64-linux = + pkgs.mkShell { + buildInputs = with pkgs; [ + (php82.withExtensions + ({ all, ... }: with all; [ + bz2 + curl + dom + filter + fileinfo + gd + iconv + imagick + intl + mbstring + openssl + pdo + pdo_mysql + pdo_sqlite + session + sodium + sqlite3 + tidy + tokenizer + xdebug + xmlwriter +# yaml + zip + zlib + ]) + ) + php82Packages.composer + libjpeg + libpng + libyaml + ]; }; + }; +} From cd1c19e077937f8f5c33a04a12d9ce532526a1a9 Mon Sep 17 00:00:00 2001 From: kolaente Date: Fri, 23 Jun 2023 10:32:50 +0200 Subject: [PATCH 2/5] chore: add .direnv to ignore --- .direnv/flake-profile-a5d5b61aa8a61b7d9d765e1daf971a9a578f1cfa | 1 - .../flake-profile-a5d5b61aa8a61b7d9d765e1daf971a9a578f1cfa.rc | 1 - .gitignore | 1 + 3 files changed, 1 insertion(+), 2 deletions(-) delete mode 120000 .direnv/flake-profile-a5d5b61aa8a61b7d9d765e1daf971a9a578f1cfa delete mode 100644 .direnv/flake-profile-a5d5b61aa8a61b7d9d765e1daf971a9a578f1cfa.rc diff --git a/.direnv/flake-profile-a5d5b61aa8a61b7d9d765e1daf971a9a578f1cfa b/.direnv/flake-profile-a5d5b61aa8a61b7d9d765e1daf971a9a578f1cfa deleted file mode 120000 index 4fc1957..0000000 --- a/.direnv/flake-profile-a5d5b61aa8a61b7d9d765e1daf971a9a578f1cfa +++ /dev/null @@ -1 +0,0 @@ -/home/konrad/www/graby/.direnv/flake-profile.17606 \ No newline at end of file diff --git a/.direnv/flake-profile-a5d5b61aa8a61b7d9d765e1daf971a9a578f1cfa.rc b/.direnv/flake-profile-a5d5b61aa8a61b7d9d765e1daf971a9a578f1cfa.rc deleted file mode 100644 index 8b13789..0000000 --- a/.direnv/flake-profile-a5d5b61aa8a61b7d9d765e1daf971a9a578f1cfa.rc +++ /dev/null @@ -1 +0,0 @@ - diff --git a/.gitignore b/.gitignore index 8563349..feac9cb 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ log/html.log composer.lock /.php-cs-fixer.cache .phpunit.result.cache +.direnv From 85fd840b4273e426e23e524383de73cd13635783 Mon Sep 17 00:00:00 2001 From: kolaente Date: Fri, 23 Jun 2023 10:43:30 +0200 Subject: [PATCH 3/5] feat: upgrade monolog/monolog to v3 --- composer.json | 6 ++--- src/Monolog/Formatter/GrabyFormatter.php | 32 +++++++++++++----------- src/Monolog/Handler/GrabyHandler.php | 5 ++-- 3 files changed, 23 insertions(+), 20 deletions(-) diff --git a/composer.json b/composer.json index ba183c7..dd1d944 100644 --- a/composer.json +++ b/composer.json @@ -15,15 +15,15 @@ "minimum-stability": "dev", "prefer-stable": true, "require": { - "php": ">=7.4", + "php": ">=8.1", "ext-curl": "*", "ext-tidy": "*", "fossar/htmlawed": "^1.2.8", "guzzlehttp/psr7": "^2.0", "j0k3r/graby-site-config": "^1.0.147", "j0k3r/httplug-ssrf-plugin": "^2.0", - "j0k3r/php-readability": "^1.2.9", - "monolog/monolog": "^1.18.0|^2.3", + "j0k3r/php-readability": "dev-master", + "monolog/monolog": "^3", "php-http/client-common": "^2.5", "php-http/discovery": "^1.14", "php-http/httplug": "^2.2", diff --git a/src/Monolog/Formatter/GrabyFormatter.php b/src/Monolog/Formatter/GrabyFormatter.php index 4c8047b..b096bae 100644 --- a/src/Monolog/Formatter/GrabyFormatter.php +++ b/src/Monolog/Formatter/GrabyFormatter.php @@ -5,6 +5,8 @@ namespace Graby\Monolog\Formatter; use Monolog\Formatter\HtmlFormatter; +use Monolog\Level; +use Monolog\LogRecord; /** * Formats incoming records into an HTML table. @@ -18,32 +20,32 @@ class GrabyFormatter extends HtmlFormatter /** * Formats a log record. * - * @param array $record A record to format + * @param LogRecord $record A record to format * * @return string The formatted record */ - public function format(array $record): string + public function format(LogRecord $record): string { $output = ''; - $output .= $this->addRowWithLevel($record['level'], 'Time', $record['datetime']->format($this->dateFormat)); - $output .= $this->addRowWithLevel($record['level'], 'Message', (string) $record['message']); + $output .= $this->addRowWithLevel($record->level, 'Time', $record->datetime->format($this->dateFormat)); + $output .= $this->addRowWithLevel($record->level, 'Message', $record->message); - if ($record['context']) { + if ($record->context) { $embeddedTable = '
'; - foreach ($record['context'] as $key => $value) { - $embeddedTable .= $this->addRowWithLevel($record['level'], $key, $this->convertToString($value)); + foreach ($record->context as $key => $value) { + $embeddedTable .= $this->addRowWithLevel($record->level, $key, $this->convertToString($value)); } $embeddedTable .= '
'; - $output .= $this->addRowWithLevel($record['level'], 'Context', $embeddedTable, false); + $output .= $this->addRowWithLevel($record->level, 'Context', $embeddedTable, false); } - if ($record['extra']) { + if ($record->extra) { $embeddedTable = ''; - foreach ($record['extra'] as $key => $value) { - $embeddedTable .= $this->addRowWithLevel($record['level'], $key, $this->convertToString($value)); + foreach ($record->extra as $key => $value) { + $embeddedTable .= $this->addRowWithLevel($record->level, $key, $this->convertToString($value)); } $embeddedTable .= '
'; - $output .= $this->addRowWithLevel($record['level'], 'Extra', $embeddedTable, false); + $output .= $this->addRowWithLevel($record->level, 'Extra', $embeddedTable, false); } return $output . ''; @@ -61,18 +63,18 @@ protected function convertToString($data): string /** * Creates an HTML table row with background cellon title cell. * - * @param int $level Error level + * @param Level $level Error level * @param string $th Row header content * @param string $td Row standard cell content * @param bool $escapeTd false if td content must not be html escaped */ - private function addRowWithLevel(int $level, string $th, string $td = ' ', bool $escapeTd = true): string + private function addRowWithLevel(Level $level, string $th, string $td = ' ', bool $escapeTd = true): string { $th = htmlspecialchars($th, \ENT_NOQUOTES, 'UTF-8'); if ($escapeTd) { $td = '
' . htmlspecialchars($td, \ENT_NOQUOTES, 'UTF-8') . '
'; } - return "\nlogLevels[$level] . "\" width=\"100px\">$th:\n" . $td . "\n"; + return "\ngetLevelColor($level) . "\" width=\"100px\">$th:\n" . $td . "\n"; } } diff --git a/src/Monolog/Handler/GrabyHandler.php b/src/Monolog/Handler/GrabyHandler.php index 878895a..f6a625c 100644 --- a/src/Monolog/Handler/GrabyHandler.php +++ b/src/Monolog/Handler/GrabyHandler.php @@ -7,6 +7,7 @@ use Graby\Monolog\Formatter\GrabyFormatter; use Monolog\Handler\AbstractProcessingHandler; use Monolog\Logger; +use Monolog\LogRecord; use Monolog\Processor\PsrLogMessageProcessor; /** @@ -45,9 +46,9 @@ public function hasRecords($level): bool return isset($this->recordsByLevel[$level]); } - protected function write(array $record): void + protected function write(LogRecord $record): void { - $this->recordsByLevel[$record['level']][] = $record; + $this->recordsByLevel[$record->level->value][] = $record; $this->records[] = $record; } } From d31f59e2687288d53a880d53274534cb71596413 Mon Sep 17 00:00:00 2001 From: kolaente Date: Mon, 26 Jun 2023 09:32:01 +0200 Subject: [PATCH 4/5] feat: support monolog 2.3 and 3.x at the same time --- composer.json | 4 ++-- src/Monolog/Formatter/GrabyFormatter.php | 28 ++++++++++++++---------- src/Monolog/Handler/GrabyHandler.php | 12 ++++++++-- 3 files changed, 28 insertions(+), 16 deletions(-) diff --git a/composer.json b/composer.json index dd1d944..81f05fc 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ "minimum-stability": "dev", "prefer-stable": true, "require": { - "php": ">=8.1", + "php": ">=7.4|>=8.1", "ext-curl": "*", "ext-tidy": "*", "fossar/htmlawed": "^1.2.8", @@ -23,7 +23,7 @@ "j0k3r/graby-site-config": "^1.0.147", "j0k3r/httplug-ssrf-plugin": "^2.0", "j0k3r/php-readability": "dev-master", - "monolog/monolog": "^3", + "monolog/monolog": "^2.3|^3", "php-http/client-common": "^2.5", "php-http/discovery": "^1.14", "php-http/httplug": "^2.2", diff --git a/src/Monolog/Formatter/GrabyFormatter.php b/src/Monolog/Formatter/GrabyFormatter.php index b096bae..61145c6 100644 --- a/src/Monolog/Formatter/GrabyFormatter.php +++ b/src/Monolog/Formatter/GrabyFormatter.php @@ -20,32 +20,36 @@ class GrabyFormatter extends HtmlFormatter /** * Formats a log record. * - * @param LogRecord $record A record to format + * @param array|LogRecord $record A record to format * * @return string The formatted record */ - public function format(LogRecord $record): string + public function format($record): string { + if ($record instanceof LogRecord) { + $record = $record->toArray(); + } + $output = ''; - $output .= $this->addRowWithLevel($record->level, 'Time', $record->datetime->format($this->dateFormat)); - $output .= $this->addRowWithLevel($record->level, 'Message', $record->message); + $output .= $this->addRowWithLevel($record['level'], 'Time', $record['datetime']->format($this->dateFormat)); + $output .= $this->addRowWithLevel($record['level'], 'Message', (string) $record['message']); - if ($record->context) { + if ($record['context']) { $embeddedTable = '
'; - foreach ($record->context as $key => $value) { - $embeddedTable .= $this->addRowWithLevel($record->level, $key, $this->convertToString($value)); + foreach ($record['context'] as $key => $value) { + $embeddedTable .= $this->addRowWithLevel($record['level'], $key, $this->convertToString($value)); } $embeddedTable .= '
'; - $output .= $this->addRowWithLevel($record->level, 'Context', $embeddedTable, false); + $output .= $this->addRowWithLevel($record['level'], 'Context', $embeddedTable, false); } - if ($record->extra) { + if ($record['extra']) { $embeddedTable = ''; - foreach ($record->extra as $key => $value) { - $embeddedTable .= $this->addRowWithLevel($record->level, $key, $this->convertToString($value)); + foreach ($record['extra'] as $key => $value) { + $embeddedTable .= $this->addRowWithLevel($record['level'], $key, $this->convertToString($value)); } $embeddedTable .= '
'; - $output .= $this->addRowWithLevel($record->level, 'Extra', $embeddedTable, false); + $output .= $this->addRowWithLevel($record['level'], 'Extra', $embeddedTable, false); } return $output . ''; diff --git a/src/Monolog/Handler/GrabyHandler.php b/src/Monolog/Handler/GrabyHandler.php index f6a625c..172db49 100644 --- a/src/Monolog/Handler/GrabyHandler.php +++ b/src/Monolog/Handler/GrabyHandler.php @@ -46,9 +46,17 @@ public function hasRecords($level): bool return isset($this->recordsByLevel[$level]); } - protected function write(LogRecord $record): void + /** + * @param array|LogRecord $record + * @return void + */ + protected function write($record): void { - $this->recordsByLevel[$record->level->value][] = $record; + $level = $record instanceof LogRecord + ? $record->level->value + : $record['level']; + + $this->recordsByLevel[$level][] = $record; $this->records[] = $record; } } From 5aef4192045351faae7130f312a650bef7d22074 Mon Sep 17 00:00:00 2001 From: kolaente Date: Mon, 26 Jun 2023 10:23:16 +0200 Subject: [PATCH 5/5] chore: simplify flake --- flake.nix | 28 +--------------------------- 1 file changed, 1 insertion(+), 27 deletions(-) diff --git a/flake.nix b/flake.nix index 8956e39..fb7c5b3 100644 --- a/flake.nix +++ b/flake.nix @@ -8,37 +8,11 @@ pkgs.mkShell { buildInputs = with pkgs; [ (php82.withExtensions - ({ all, ... }: with all; [ - bz2 - curl - dom - filter - fileinfo - gd - iconv - imagick - intl - mbstring - openssl - pdo - pdo_mysql - pdo_sqlite - session - sodium - sqlite3 + ({ enabled, all }: with all; enabled ++ [ tidy - tokenizer - xdebug - xmlwriter -# yaml - zip - zlib ]) ) php82Packages.composer - libjpeg - libpng - libyaml ]; }; }; }