-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
81f0c65
commit 405cf16
Showing
16 changed files
with
649 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Beste\Psr\Log; | ||
|
||
/** | ||
* @phpstan-type BestePsrLogContextShape array<string, mixed> | ||
*/ | ||
final class Context | ||
{ | ||
/** | ||
* @param BestePsrLogContextShape $data | ||
*/ | ||
public function __construct( | ||
public readonly array $data = [], | ||
) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Beste\Psr\Log; | ||
|
||
use DateTimeInterface; | ||
use Stringable; | ||
|
||
/** | ||
* @phpstan-import-type BestePsrLogContextShape from Context | ||
*/ | ||
final class Message implements Stringable | ||
{ | ||
private string $value; | ||
|
||
/** | ||
* @param Context|BestePsrLogContextShape|null $context | ||
*/ | ||
public function __construct( | ||
string $message, | ||
Context|array|null $context = null, | ||
) { | ||
if (!$context instanceof Context) { | ||
$context = new Context($context ?? []); | ||
} | ||
|
||
$this->value = self::processMessage($message, $context); | ||
} | ||
|
||
public function __toString() | ||
{ | ||
return $this->value; | ||
} | ||
|
||
public function contains(string $needle): bool | ||
{ | ||
return str_contains(mb_strtolower($this->value), mb_strtolower($needle)) !== false; | ||
} | ||
|
||
public function matches(string $pattern): bool | ||
{ | ||
return preg_match($pattern, $this->value) > 0; | ||
} | ||
|
||
private static function processMessage(string $message, Context $context): string | ||
{ | ||
if (str_contains($message, '{') === false) { | ||
return $message; | ||
} | ||
|
||
$replacements = []; | ||
|
||
foreach ($context->data as $key => $value) { | ||
$placeholder = '{' . $key . '}'; | ||
|
||
if (str_contains($message, $placeholder) === false) { | ||
continue; | ||
} | ||
|
||
if ($value === null || \is_scalar($value) || $value instanceof Stringable) { | ||
$replacements[$placeholder] = $value; | ||
} elseif ($value instanceof DateTimeInterface) { | ||
$replacements[$placeholder] = $value->format(\DATE_ATOM); | ||
} else { | ||
$replacements[$placeholder] = '[' . \gettype($value) . ']'; | ||
} | ||
} | ||
|
||
return strtr($message, $replacements); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Beste\Psr\Log; | ||
|
||
use Psr\Log\LogLevel; | ||
|
||
/** | ||
* @phpstan-type BestePsrLogRecordShape array{ | ||
* level: LogLevel::*, | ||
* message: string, | ||
* context: array<string, mixed> | ||
* } | ||
*/ | ||
final class Record | ||
{ | ||
private function __construct( | ||
/** @var LogLevel::* $level */ | ||
public readonly string $level, | ||
public readonly Message $message, | ||
public readonly Context $context, | ||
) { | ||
} | ||
|
||
/** | ||
* @param LogLevel::* $level | ||
* @param array<string, mixed> $context | ||
*/ | ||
public static function with(string $level, string $message, array $context): self | ||
{ | ||
$c = new Context($context); | ||
|
||
return new self( | ||
$level, | ||
new Message($message, $context), | ||
new Context($context), | ||
); | ||
} | ||
} |
Oops, something went wrong.