-
-
Notifications
You must be signed in to change notification settings - Fork 21
Fix #99: Add customizable context format and value conversion #140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
1fd6066
7ded463
6bbf64c
51f2497
82d7b3b
3195398
932bb0b
5ca3646
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Yiisoft\Log\Message; | ||
|
|
||
| use Yiisoft\VarDumper\VarDumper; | ||
|
|
||
| use function is_object; | ||
| use function method_exists; | ||
|
|
||
| /** | ||
| * Default converter of a context value to its string representation. | ||
| * | ||
| * Returns the result of `__toString()` for stringable objects and falls back to {@see VarDumper} otherwise. | ||
| * | ||
| * @internal | ||
| */ | ||
| final class VarDumperValueConverter | ||
| { | ||
| public function __invoke(mixed $value): string | ||
| { | ||
| if (is_object($value) && method_exists($value, '__toString')) { | ||
| return (string) $value; | ||
| } | ||
|
|
||
| return VarDumper::create($value)->asString(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,8 @@ | |
| * @param callable|null $format A PHP callable that returns a string representation of the log message. | ||
| * @param callable|null $prefix A PHP callable that returns a string to be prefixed to every exported message. | ||
| * @param string|null $timestampFormat The date format for the log timestamp. | ||
| * @param string|callable|null $contextFormat A context format for the log context output. See {@see Target::__construct()}. | ||
| * @param callable|null $stringConverter A PHP callable that converts a context value to a string. See {@see Target::__construct()}. | ||
| * @param int $exportInterval How many messages should be accumulated before they are exported. | ||
| * @param bool|callable $enabled Whether this target is enabled, or a PHP callable that returns a boolean. | ||
| */ | ||
|
|
@@ -33,10 +35,12 @@ | |
| ?callable $format = null, | ||
| ?callable $prefix = null, | ||
| ?string $timestampFormat = null, | ||
| string|callable|null $contextFormat = null, | ||
| ?callable $stringConverter = null, | ||
|
Comment on lines
+38
to
+39
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These could be moved to the end to keep it backwards compatible. |
||
| int $exportInterval = self::DEFAULT_EXPORT_INTERVAL, | ||
| bool|callable $enabled = true, | ||
|
Check warning on line 41 in src/PsrTarget.php
|
||
| ) { | ||
| parent::__construct($levels, $categories, $exceptCategories, $format, $prefix, $timestampFormat, $exportInterval, $enabled); | ||
| parent::__construct($levels, $categories, $exceptCategories, $format, $prefix, $timestampFormat, $contextFormat, $stringConverter, $exportInterval, $enabled); | ||
| } | ||
|
|
||
| /** | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,6 +36,8 @@ | |
| * @param callable|null $format A PHP callable that returns a string representation of the log message. | ||
| * @param callable|null $prefix A PHP callable that returns a string to be prefixed to every exported message. | ||
| * @param string|null $timestampFormat The date format for the log timestamp. | ||
| * @param string|callable|null $contextFormat A context format for the log context output. See {@see Target::__construct()}. | ||
| * @param callable|null $stringConverter A PHP callable that converts a context value to a string. See {@see Target::__construct()}. | ||
| * @param int $exportInterval How many messages should be accumulated before they are exported. | ||
| * @param bool|callable $enabled Whether this target is enabled, or a PHP callable that returns a boolean. | ||
| */ | ||
|
|
@@ -47,19 +49,21 @@ | |
| ?callable $format = null, | ||
| ?callable $prefix = null, | ||
| ?string $timestampFormat = null, | ||
| string|callable|null $contextFormat = null, | ||
| ?callable $stringConverter = null, | ||
|
Comment on lines
+52
to
+53
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These could be moved to the end to keep it backwards compatible. |
||
| int $exportInterval = self::DEFAULT_EXPORT_INTERVAL, | ||
| bool|callable $enabled = true, | ||
|
Check warning on line 55 in src/StreamTarget.php
|
||
| ) { | ||
| parent::__construct($levels, $categories, $exceptCategories, $format, $prefix, $timestampFormat, $exportInterval, $enabled); | ||
| parent::__construct($levels, $categories, $exceptCategories, $format, $prefix, $timestampFormat, $contextFormat, $stringConverter, $exportInterval, $enabled); | ||
| } | ||
|
|
||
| protected function export(): void | ||
| { | ||
| $stream = $this->createStream(); | ||
| flock($stream, LOCK_EX); | ||
|
Check warning on line 63 in src/StreamTarget.php
|
||
|
|
||
| if (fwrite($stream, $this->formatMessages("\n")) === false) { | ||
| flock($stream, LOCK_UN); | ||
|
Check warning on line 66 in src/StreamTarget.php
|
||
| fclose($stream); | ||
| throw new RuntimeException(sprintf( | ||
| 'Unable to export the log because of an error writing to the stream: %s', | ||
|
|
||
|
WarLikeLaux marked this conversation as resolved.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -80,6 +80,11 @@ abstract class Target | |
| * @param callable|null $format A PHP callable that returns a string representation of the log message. | ||
| * @param callable|null $prefix A PHP callable that returns a string to be prefixed to every exported message. | ||
| * @param string|null $timestampFormat The date format for the log timestamp. | ||
| * @param string|callable|null $contextFormat A context format for the log context output. A template string | ||
| * supports `{trace}`, `{message}` and `{common}` placeholders, or a PHP callable for full control. See | ||
| * {@see Formatter::__construct()}. | ||
| * @param callable|null $stringConverter A PHP callable that converts a context value to a string. | ||
| * See {@see Formatter::__construct()}. | ||
| * @param int $exportInterval How many messages should be accumulated before they are exported. | ||
| * @param bool|callable $enabled Whether this target is enabled, or a PHP callable that returns a boolean. | ||
| */ | ||
|
|
@@ -90,11 +95,13 @@ public function __construct( | |
| ?callable $format = null, | ||
| ?callable $prefix = null, | ||
| ?string $timestampFormat = null, | ||
| string|callable|null $contextFormat = null, | ||
| ?callable $stringConverter = null, | ||
|
Comment on lines
+98
to
+99
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These could be moved to the end to keep it backwards compatible. |
||
| private int $exportInterval = self::DEFAULT_EXPORT_INTERVAL, | ||
| bool|callable $enabled = true, | ||
| ) { | ||
| $this->categories = new CategoryFilter($categories, $exceptCategories); | ||
| $this->formatter = new Formatter($format, $prefix, $timestampFormat); | ||
| $this->formatter = new Formatter($format, $prefix, $timestampFormat, $contextFormat, $stringConverter); | ||
| /** @psalm-suppress DeprecatedMethod */ | ||
| $this->setLevels($levels); | ||
| $this->enabled = $enabled; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.