Skip to content

Commit

Permalink
Merge pull request #46 from WyriHaximus/renovate/monolog-monolog-3.x
Browse files Browse the repository at this point in the history
Update dependency monolog/monolog to v3
  • Loading branch information
WyriHaximus authored Aug 10, 2023
2 parents f4cbb34 + 78195b7 commit 3061f2d
Show file tree
Hide file tree
Showing 17 changed files with 172 additions and 198 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"require": {
"php": "^8.2",
"igorw/get-in": "^1.0.3",
"monolog/monolog": "^2.9.1"
"monolog/monolog": "^3.4.0"
},
"require-dev": {
"wyrihaximus/test-utilities": "^5.6.0"
Expand Down
39 changes: 19 additions & 20 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 9 additions & 13 deletions src/CopyProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@

namespace WyriHaximus\Monolog\Processors;

use Monolog\Logger;
use Monolog\LogRecord;
use Monolog\Processor\ProcessorInterface;

use function array_merge_recursive;
use function explode;
use function igorw\assoc_in;
use function igorw\get_in;

/** @phpstan-import-type Record from Logger */
final class CopyProcessor
final class CopyProcessor implements ProcessorInterface
{
/** @var string[] */
/** @var array<string> */
private array $from;

/** @var string[] */
/** @var array<string> */
private array $to;

public function __construct(string $from, string $to)
Expand All @@ -25,16 +26,11 @@ public function __construct(string $from, string $to)
$this->to = explode('.', $to);
}

/**
* @phpstan-param Record $record
*
* @phpstan-return Record
*/
public function __invoke(array $record): array
public function __invoke(LogRecord $record): LogRecord
{
$value = get_in($record, $this->from);
$value = get_in($record->toArray(), $this->from);
if ($value !== null) {
$record = assoc_in($record, $this->to, $value);
$record = $record->with(...array_merge_recursive(assoc_in([], $this->to, $value), ['context' => $record->context, 'extra' => $record->extra]));
}

return $record;
Expand Down
17 changes: 6 additions & 11 deletions src/ExceptionClassProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,19 @@

namespace WyriHaximus\Monolog\Processors;

use Monolog\Logger;
use Monolog\LogRecord;
use Monolog\Processor\ProcessorInterface;
use Throwable;

use function array_key_exists;
use function get_class;

/** @phpstan-import-type Record from Logger */
final class ExceptionClassProcessor
final class ExceptionClassProcessor implements ProcessorInterface
{
/**
* @phpstan-param Record $record
*
* @phpstan-return Record
*/
public function __invoke(array $record): array
public function __invoke(LogRecord $record): LogRecord
{
if (array_key_exists('exception', $record['context']) && $record['context']['exception'] instanceof Throwable) {
$record['extra']['exception_class'] = get_class($record['context']['exception']);
if (array_key_exists('exception', $record->context) && $record->context['exception'] instanceof Throwable) {
$record->extra['exception_class'] = get_class($record->context['exception']);
}

return $record;
Expand Down
15 changes: 5 additions & 10 deletions src/KeyValueProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,18 @@

namespace WyriHaximus\Monolog\Processors;

use Monolog\Logger;
use Monolog\LogRecord;
use Monolog\Processor\ProcessorInterface;

/** @phpstan-import-type Record from Logger */
final class KeyValueProcessor
final class KeyValueProcessor implements ProcessorInterface
{
public function __construct(private string $key, private mixed $value)
{
}

/**
* @phpstan-param Record $record
*
* @phpstan-return Record
*/
public function __invoke(array $record): array
public function __invoke(LogRecord $record): LogRecord
{
$record['extra'][$this->key] = $this->value;
$record->extra[$this->key] = $this->value;

return $record;
}
Expand Down
15 changes: 5 additions & 10 deletions src/RuntimeProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

namespace WyriHaximus\Monolog\Processors;

use Monolog\Logger;
use Monolog\LogRecord;
use Monolog\Processor\ProcessorInterface;

use function microtime;

/** @phpstan-import-type Record from Logger */
final class RuntimeProcessor
final class RuntimeProcessor implements ProcessorInterface
{
private float $start;

Expand All @@ -18,14 +18,9 @@ public function __construct()
$this->start = microtime(true);
}

/**
* @phpstan-param Record $record
*
* @phpstan-return Record
*/
public function __invoke(array $record): array
public function __invoke(LogRecord $record): LogRecord
{
$record['extra']['runtime'] = microtime(true) - $this->start;
$record->extra['runtime'] = microtime(true) - $this->start;

return $record;
}
Expand Down
19 changes: 7 additions & 12 deletions src/ToContextProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,30 @@

namespace WyriHaximus\Monolog\Processors;

use Monolog\Logger;
use Monolog\LogRecord;
use Monolog\Processor\ProcessorInterface;

use function array_key_exists;

/** @phpstan-import-type Record from Logger */
final class ToContextProcessor
final class ToContextProcessor implements ProcessorInterface
{
/**
* @param string[] $keys
* @param array<string> $keys
*
* @phpstan-ignore-next-line
*/
public function __construct(private array $keys = ['channel', 'extra', 'datetime'])
{
}

/**
* @phpstan-param Record $record
*
* @phpstan-return Record
*/
public function __invoke(array $record): array
public function __invoke(LogRecord $record): LogRecord
{
foreach ($this->keys as $key) {
if (! array_key_exists($key, $record)) {
if (! array_key_exists($key, $record->toArray())) {
continue;
}

$record['context'][$key] = $record[$key];
$record = $record->with(context: [$key => $record->toArray()[$key]] + $record->toArray()['context']);
}

return $record;
Expand Down
25 changes: 10 additions & 15 deletions src/TraceProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,37 @@

namespace WyriHaximus\Monolog\Processors;

use Monolog\Logger;
use Monolog\LogRecord;
use Monolog\Processor\ProcessorInterface;
use Throwable;

use function array_key_exists;
use function debug_backtrace;

use const DEBUG_BACKTRACE_IGNORE_ARGS;

/** @phpstan-import-type Record from Logger */
final class TraceProcessor
final class TraceProcessor implements ProcessorInterface
{
/** @phpstan-ignore-next-line */
public function __construct(private bool $always = false)
{
}

/**
* @phpstan-param Record $record
*
* @phpstan-return Record
*/
public function __invoke(array $record): array
public function __invoke(LogRecord $record): LogRecord
{
if (array_key_exists('exception', $record['context']) && $record['context']['exception'] instanceof Throwable) {
$record['extra']['trace'] = $record['context']['exception']->getTrace();
foreach ($record['extra']['trace'] as $index => $line) {
if (array_key_exists('exception', $record->context) && $record->context['exception'] instanceof Throwable) {
$record->extra['trace'] = $record->context['exception']->getTrace();
foreach ($record->extra['trace'] as $index => $line) {
if (! array_key_exists('args', $line)) {
continue;
}

unset($record['extra']['trace'][$index]['args']);
unset($record->extra['trace'][$index]['args']);
}
}

if ($this->always && ! array_key_exists('trace', $record['extra'])) {
$record['extra']['trace'] = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
if ($this->always && ! array_key_exists('trace', $record->extra)) {
$record->extra['trace'] = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
}

return $record;
Expand Down
11 changes: 6 additions & 5 deletions tests/CopyProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace WyriHaximus\Tests\Monolog\Processors;

use Monolog\LogRecord;
use PHPUnit\Framework\TestCase;
use WyriHaximus\Monolog\Processors\CopyProcessor;

Expand All @@ -13,14 +14,14 @@ final class CopyProcessorTest extends TestCase
public function testCopy(): void
{
$processor = new CopyProcessor('context.abc', 'context.def');
$fromRecord = [
$fromRecord = new LogRecord(...[
'context' => ['abc' => 'value'],
] + Records::basic();
] + Records::basic());

$toRecord = $processor($fromRecord);

self::assertArrayHasKey('def', $toRecord['context']);
self::assertEquals('value', $toRecord['context']['abc']);
self::assertEquals('value', $toRecord['context']['def']);
self::assertArrayHasKey('def', $toRecord->context);
self::assertEquals('value', $toRecord->context['abc']);
self::assertEquals('value', $toRecord->context['def']);
}
}
11 changes: 6 additions & 5 deletions tests/ExceptionClassProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace WyriHaximus\Tests\Monolog\Processors;

use InvalidArgumentException;
use Monolog\LogRecord;
use PHPUnit\Framework\TestCase;
use WyriHaximus\Monolog\Processors\ExceptionClassProcessor;

Expand All @@ -13,7 +14,7 @@ final class ExceptionClassProcessorTest extends TestCase
{
public function testNoException(): void
{
$input = Records::basic();
$input = new LogRecord(...Records::basic());
$processor = new ExceptionClassProcessor();
$record = $processor($input);

Expand All @@ -24,11 +25,11 @@ public function testException(): void
{
$processor = new ExceptionClassProcessor();
$exception = new InvalidArgumentException('fail!');
$record = $processor([
$record = $processor(new LogRecord(...[
'context' => ['exception' => $exception],
] + Records::basic());
] + Records::basic()));

self::assertArrayHasKey('exception_class', $record['extra']);
self::assertSame(InvalidArgumentException::class, $record['extra']['exception_class']);
self::assertArrayHasKey('exception_class', $record->extra);
self::assertSame(InvalidArgumentException::class, $record->extra['exception_class']);
}
}
Loading

0 comments on commit 3061f2d

Please sign in to comment.