Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 8 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| PadColonColumnsLogProcessor3 | |
0.00% |
0 / 8 |
|
0.00% |
0 / 2 |
12 | |
0.00% |
0 / 1 |
| __invoke | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| pad | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Align text following `:` in a log message. |
| 4 | * |
| 5 | * Use `:::` to indicate it should be padded. |
| 6 | * |
| 7 | * Monolog v3 compatible. |
| 8 | * |
| 9 | * @package brianhenryie/strauss |
| 10 | */ |
| 11 | |
| 12 | namespace BrianHenryIE\Strauss\Helpers\Log; |
| 13 | |
| 14 | use CommunityHive\App\Illuminate\Support\Facades\Log; |
| 15 | use DateTimeInterface; |
| 16 | use Monolog\LogRecord; |
| 17 | use Monolog\Processor\ProcessorInterface; |
| 18 | |
| 19 | class PadColonColumnsLogProcessor3 implements ProcessorInterface |
| 20 | { |
| 21 | /** @var int $padLength */ |
| 22 | protected int $padLength = 0; |
| 23 | |
| 24 | /** |
| 25 | * @param LogRecord $record |
| 26 | * @return LogRecord |
| 27 | */ |
| 28 | public function __invoke(LogRecord $record): LogRecord |
| 29 | { |
| 30 | $messageParts = explode(':::', $record->message, 2); |
| 31 | |
| 32 | /** |
| 33 | * @see https://github.com/BrianHenryIE/strauss/pull/231#pullrequestreview-3600736232 |
| 34 | */ |
| 35 | if (count($messageParts) < 2) { |
| 36 | return $record; |
| 37 | } |
| 38 | |
| 39 | $this->padLength = max($this->padLength, strlen($messageParts[0]) + 1); |
| 40 | |
| 41 | $messageParts[0] = $this->pad($messageParts[0], $this->padLength); |
| 42 | |
| 43 | return $record->with(...['message'=> implode('', $messageParts)]); |
| 44 | } |
| 45 | |
| 46 | private function pad(string $text, int $padLength): string |
| 47 | { |
| 48 | $padded = str_pad($text, $padLength, ' ', STR_PAD_RIGHT); |
| 49 | return str_replace($text, $text . ':', $padded); |
| 50 | } |
| 51 | } |