From 13eab53515c2ece73b70fcb2d1510a4ce1b7e801 Mon Sep 17 00:00:00 2001 From: Philip Nelson Date: Fri, 29 Jul 2022 10:35:15 +1000 Subject: [PATCH 1/3] Update to work more nicely with monolog ^2.5 and PHP 8 --- composer.json | 8 ++++---- .../Monolog/Formatter/ElasticCommonSchemaFormatter.php | 9 ++++++++- src/Elastic/Types/BaseType.php | 1 - src/Elastic/Types/Service.php | 1 - src/Elastic/Types/Tracing.php | 1 - src/Elastic/Types/User.php | 1 - tests/Elastic/BaseTestCase.php | 1 - tests/Elastic/HelperForMonolog.php | 1 + .../Formatter/ElasticCommonSchemaFormatterTest.php | 3 ++- tests/Elastic/Types/BaseTypeTest.php | 1 - tests/Elastic/Types/ErrorTest.php | 1 - tests/Elastic/Types/ServiceTest.php | 1 - tests/Elastic/Types/TracingTest.php | 1 - tests/Elastic/Types/UserTest.php | 1 - 14 files changed, 15 insertions(+), 16 deletions(-) diff --git a/composer.json b/composer.json index a6eaa68..6619820 100644 --- a/composer.json +++ b/composer.json @@ -18,14 +18,14 @@ ], "require": { "php": "^7.2||^8.0", - "psr/log": "^1.0.1", + "psr/log": "^1.0.1 || ^2.0 || ^3.0", "ext-json": "*" }, "require-dev": { "php-parallel-lint/php-parallel-lint": "^1.3.0", - "squizlabs/php_codesniffer": "3.*", - "brianium/paratest": "^3.1", - "monolog/monolog": "^2.0" + "squizlabs/php_codesniffer": "^3.7", + "brianium/paratest": "^6.6", + "monolog/monolog": "^2.5" }, "autoload": { "psr-4": {"Elastic\\": "src/Elastic"} diff --git a/src/Elastic/Monolog/Formatter/ElasticCommonSchemaFormatter.php b/src/Elastic/Monolog/Formatter/ElasticCommonSchemaFormatter.php index baf0bd5..1a48be3 100644 --- a/src/Elastic/Monolog/Formatter/ElasticCommonSchemaFormatter.php +++ b/src/Elastic/Monolog/Formatter/ElasticCommonSchemaFormatter.php @@ -27,7 +27,7 @@ class ElasticCommonSchemaFormatter extends NormalizerFormatter { private const ECS_VERSION = '1.2.0'; - private static $logOriginKeys = ['file' => true, 'line' => true, 'class' => true, 'function' => true]; + private static $logOriginKeys = ['file' => true, 'line' => true, 'class' => true, 'function' => true, 'callType' => true]; /** * @var array @@ -183,6 +183,13 @@ private function formatLogOrigin(array $inContext, array &$outRecord): void $originVal['file'] = $fileVal; } + if (array_key_exists('callType', $inContext)) { + $callType = $inContext['callType']; + if (is_string($callType)) { + $originVal['callType'] = $callType; + } + } + $outFunctionVal = null; if (array_key_exists('function', $inContext)) { $inFunctionVal = $inContext['function']; diff --git a/src/Elastic/Types/BaseType.php b/src/Elastic/Types/BaseType.php index 1bb77f0..fc5da3b 100644 --- a/src/Elastic/Types/BaseType.php +++ b/src/Elastic/Types/BaseType.php @@ -10,7 +10,6 @@ class BaseType { - /** * Get the Popo as array * diff --git a/src/Elastic/Types/Service.php b/src/Elastic/Types/Service.php index 9f5a7b2..3bf1cf5 100644 --- a/src/Elastic/Types/Service.php +++ b/src/Elastic/Types/Service.php @@ -21,7 +21,6 @@ */ class Service extends BaseType implements JsonSerializable { - /** * @var array */ diff --git a/src/Elastic/Types/Tracing.php b/src/Elastic/Types/Tracing.php index 1cc5dcd..92ac376 100644 --- a/src/Elastic/Types/Tracing.php +++ b/src/Elastic/Types/Tracing.php @@ -22,7 +22,6 @@ */ class Tracing extends BaseType implements JsonSerializable { - /** * Unique identifier of the trace * diff --git a/src/Elastic/Types/User.php b/src/Elastic/Types/User.php index 7ffe528..a8a732d 100644 --- a/src/Elastic/Types/User.php +++ b/src/Elastic/Types/User.php @@ -21,7 +21,6 @@ */ class User extends BaseType implements JsonSerializable { - /** * @var array */ diff --git a/tests/Elastic/BaseTestCase.php b/tests/Elastic/BaseTestCase.php index 1cd3c08..0a3b2e2 100644 --- a/tests/Elastic/BaseTestCase.php +++ b/tests/Elastic/BaseTestCase.php @@ -19,7 +19,6 @@ */ class BaseTestCase extends TestCase { - /** * @return string */ diff --git a/tests/Elastic/HelperForMonolog.php b/tests/Elastic/HelperForMonolog.php index 17b5939..77f8edf 100644 --- a/tests/Elastic/HelperForMonolog.php +++ b/tests/Elastic/HelperForMonolog.php @@ -14,6 +14,7 @@ class HelperForMonolog { public static function logEmergency(Logger $logger, string $message, array &$logOrigin): void { + $logOrigin['callType'] = '::'; $logOrigin['class'] = __CLASS__; $logOrigin['function'] = __FUNCTION__; $logOrigin['file'] = __FILE__; diff --git a/tests/Elastic/Monolog/Formatter/ElasticCommonSchemaFormatterTest.php b/tests/Elastic/Monolog/Formatter/ElasticCommonSchemaFormatterTest.php index 0a05bb0..83d8e12 100644 --- a/tests/Elastic/Monolog/Formatter/ElasticCommonSchemaFormatterTest.php +++ b/tests/Elastic/Monolog/Formatter/ElasticCommonSchemaFormatterTest.php @@ -432,7 +432,7 @@ public function testsIntrospectionProcessor(bool $useLogOriginFromContext) if ($useLogOriginFromContext) { $testHelper->expectedAdditionalLogKeys = ['origin']; } else { - $testHelper->expectedAdditionalTopLevelKeys = ['file', 'line', 'class', 'function']; + $testHelper->expectedAdditionalTopLevelKeys = ['file', 'line', 'class', 'callType', 'function']; } $logOrigin = []; @@ -450,6 +450,7 @@ function (Logger $logger) use ($testHelper, &$logOrigin) { self::assertSame($logOrigin['file'], $decodedJson['log']['origin']['file']['name']); self::assertSame($logOrigin['line'], $decodedJson['log']['origin']['file']['line']); self::assertSame($logOrigin['class'] . '::' . $logOrigin['function'], $decodedJson['log']['origin']['function']); + self::assertSame($logOrigin['callType'], $decodedJson['log']['origin']['callType']); } else { self::assertArrayNotHasKey('origin', $decodedJson['log']); } diff --git a/tests/Elastic/Types/BaseTypeTest.php b/tests/Elastic/Types/BaseTypeTest.php index 113e886..c0e0c09 100644 --- a/tests/Elastic/Types/BaseTypeTest.php +++ b/tests/Elastic/Types/BaseTypeTest.php @@ -23,7 +23,6 @@ */ class BaseTypeTest extends BaseTestCase { - /** * @covers Elastic\Types\BaseType::toArray */ diff --git a/tests/Elastic/Types/ErrorTest.php b/tests/Elastic/Types/ErrorTest.php index cb728cb..8a4a8c5 100644 --- a/tests/Elastic/Types/ErrorTest.php +++ b/tests/Elastic/Types/ErrorTest.php @@ -23,7 +23,6 @@ */ class ErrorTest extends BaseTestCase { - /** * @covers \Elastic\Types\Error::__construct * @covers \Elastic\Types\Error::jsonSerialize diff --git a/tests/Elastic/Types/ServiceTest.php b/tests/Elastic/Types/ServiceTest.php index 7124fd7..5fd1b73 100644 --- a/tests/Elastic/Types/ServiceTest.php +++ b/tests/Elastic/Types/ServiceTest.php @@ -23,7 +23,6 @@ */ class ServiceTest extends BaseTestCase { - /** * @covers Elastic\Types\Service::__construct * @covers Elastic\Types\Service::jsonSerialize diff --git a/tests/Elastic/Types/TracingTest.php b/tests/Elastic/Types/TracingTest.php index 770e575..2dcb943 100644 --- a/tests/Elastic/Types/TracingTest.php +++ b/tests/Elastic/Types/TracingTest.php @@ -23,7 +23,6 @@ */ class TracingTest extends BaseTestCase { - /** * @covers Elastic\Types\Tracing::__construct * @covers Elastic\Types\Tracing::jsonSerialize diff --git a/tests/Elastic/Types/UserTest.php b/tests/Elastic/Types/UserTest.php index 8748a61..c5afa8d 100644 --- a/tests/Elastic/Types/UserTest.php +++ b/tests/Elastic/Types/UserTest.php @@ -23,7 +23,6 @@ */ class UserTest extends BaseTestCase { - /** * @covers Elastic\Types\User::__construct * @covers Elastic\Types\User::jsonSerialize From e481d55c4bccc9a075bff8cd47661275199a7cb3 Mon Sep 17 00:00:00 2001 From: Philip Nelson Date: Fri, 29 Jul 2022 10:55:40 +1000 Subject: [PATCH 2/3] include callType in function rather than its own key which isn't part of ECS --- .../Formatter/ElasticCommonSchemaFormatter.php | 12 ++++-------- .../Formatter/ElasticCommonSchemaFormatterTest.php | 1 - 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/src/Elastic/Monolog/Formatter/ElasticCommonSchemaFormatter.php b/src/Elastic/Monolog/Formatter/ElasticCommonSchemaFormatter.php index 1a48be3..2f05e1e 100644 --- a/src/Elastic/Monolog/Formatter/ElasticCommonSchemaFormatter.php +++ b/src/Elastic/Monolog/Formatter/ElasticCommonSchemaFormatter.php @@ -183,13 +183,6 @@ private function formatLogOrigin(array $inContext, array &$outRecord): void $originVal['file'] = $fileVal; } - if (array_key_exists('callType', $inContext)) { - $callType = $inContext['callType']; - if (is_string($callType)) { - $originVal['callType'] = $callType; - } - } - $outFunctionVal = null; if (array_key_exists('function', $inContext)) { $inFunctionVal = $inContext['function']; @@ -197,7 +190,10 @@ private function formatLogOrigin(array $inContext, array &$outRecord): void if (array_key_exists('class', $inContext)) { $inClassVal = $inContext['class']; if (is_string($inClassVal)) { - $outFunctionVal = $inClassVal . '::' . $inFunctionVal; + $callType = array_key_exists('callType', $inContext) && is_string($inContext['callType']) + ? $inContext['callType'] + : '::'; + $outFunctionVal = $inClassVal . $callType . $inFunctionVal; } } diff --git a/tests/Elastic/Monolog/Formatter/ElasticCommonSchemaFormatterTest.php b/tests/Elastic/Monolog/Formatter/ElasticCommonSchemaFormatterTest.php index 83d8e12..27ab7f8 100644 --- a/tests/Elastic/Monolog/Formatter/ElasticCommonSchemaFormatterTest.php +++ b/tests/Elastic/Monolog/Formatter/ElasticCommonSchemaFormatterTest.php @@ -450,7 +450,6 @@ function (Logger $logger) use ($testHelper, &$logOrigin) { self::assertSame($logOrigin['file'], $decodedJson['log']['origin']['file']['name']); self::assertSame($logOrigin['line'], $decodedJson['log']['origin']['file']['line']); self::assertSame($logOrigin['class'] . '::' . $logOrigin['function'], $decodedJson['log']['origin']['function']); - self::assertSame($logOrigin['callType'], $decodedJson['log']['origin']['callType']); } else { self::assertArrayNotHasKey('origin', $decodedJson['log']); } From 575433bee12f439cd6166914c3c939bd62406cdd Mon Sep 17 00:00:00 2001 From: Philip Nelson Date: Fri, 29 Jul 2022 11:02:51 +1000 Subject: [PATCH 3/3] update test --- .../Monolog/Formatter/ElasticCommonSchemaFormatterTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Elastic/Monolog/Formatter/ElasticCommonSchemaFormatterTest.php b/tests/Elastic/Monolog/Formatter/ElasticCommonSchemaFormatterTest.php index 27ab7f8..f0eeaf7 100644 --- a/tests/Elastic/Monolog/Formatter/ElasticCommonSchemaFormatterTest.php +++ b/tests/Elastic/Monolog/Formatter/ElasticCommonSchemaFormatterTest.php @@ -449,7 +449,7 @@ function (Logger $logger) use ($testHelper, &$logOrigin) { if ($useLogOriginFromContext) { self::assertSame($logOrigin['file'], $decodedJson['log']['origin']['file']['name']); self::assertSame($logOrigin['line'], $decodedJson['log']['origin']['file']['line']); - self::assertSame($logOrigin['class'] . '::' . $logOrigin['function'], $decodedJson['log']['origin']['function']); + self::assertSame($logOrigin['class'] . $logOrigin['callType'] . $logOrigin['function'], $decodedJson['log']['origin']['function']); } else { self::assertArrayNotHasKey('origin', $decodedJson['log']); }