From 9117a6c747524c78bf0fff7eb5fb5e33a6a4f021 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Fri, 17 Aug 2018 09:54:55 +0200 Subject: [PATCH] Fix displaying anonymous classes --- src/Monolog/ErrorHandler.php | 3 ++- src/Monolog/Formatter/JsonFormatter.php | 5 +++-- src/Monolog/Formatter/LineFormatter.php | 8 +++++--- src/Monolog/Formatter/MongoDBFormatter.php | 6 ++++-- src/Monolog/Formatter/NormalizerFormatter.php | 9 +++++---- src/Monolog/Processor/PsrLogMessageProcessor.php | 4 +++- src/Monolog/Registry.php | 10 ++++++++++ 7 files changed, 32 insertions(+), 13 deletions(-) diff --git a/src/Monolog/ErrorHandler.php b/src/Monolog/ErrorHandler.php index b3025738..7c7d48c2 100644 --- a/src/Monolog/ErrorHandler.php +++ b/src/Monolog/ErrorHandler.php @@ -14,6 +14,7 @@ namespace Monolog; use Psr\Log\LoggerInterface; use Psr\Log\LogLevel; use Monolog\Handler\AbstractHandler; +use Monolog\Registry; /** * Monolog error handler @@ -133,7 +134,7 @@ class ErrorHandler { $this->logger->log( $this->uncaughtExceptionLevel === null ? LogLevel::ERROR : $this->uncaughtExceptionLevel, - sprintf('Uncaught Exception %s: "%s" at %s line %s', get_class($e), $e->getMessage(), $e->getFile(), $e->getLine()), + sprintf('Uncaught Exception %s: "%s" at %s line %s', Registry::getClass($e), $e->getMessage(), $e->getFile(), $e->getLine()), array('exception' => $e) ); diff --git a/src/Monolog/Formatter/JsonFormatter.php b/src/Monolog/Formatter/JsonFormatter.php index b8309b10..fcb08474 100644 --- a/src/Monolog/Formatter/JsonFormatter.php +++ b/src/Monolog/Formatter/JsonFormatter.php @@ -12,6 +12,7 @@ namespace Monolog\Formatter; use Exception; +use Monolog\Registry; use Throwable; /** @@ -179,11 +180,11 @@ class JsonFormatter extends NormalizerFormatter { // TODO 2.0 only check for Throwable if (!$e instanceof Exception && !$e instanceof Throwable) { - throw new \InvalidArgumentException('Exception/Throwable expected, got '.gettype($e).' / '.get_class($e)); + throw new \InvalidArgumentException('Exception/Throwable expected, got '.gettype($e).' / '.Registry::getClass($e)); } $data = array( - 'class' => get_class($e), + 'class' => Registry::getClass($e), 'message' => $e->getMessage(), 'code' => $e->getCode(), 'file' => $e->getFile().':'.$e->getLine(), diff --git a/src/Monolog/Formatter/LineFormatter.php b/src/Monolog/Formatter/LineFormatter.php index d3e209e6..939cd043 100644 --- a/src/Monolog/Formatter/LineFormatter.php +++ b/src/Monolog/Formatter/LineFormatter.php @@ -11,6 +11,8 @@ namespace Monolog\Formatter; +use Monolog\Registry; + /** * Formats incoming records into a one-line string * @@ -129,17 +131,17 @@ class LineFormatter extends NormalizerFormatter { // TODO 2.0 only check for Throwable if (!$e instanceof \Exception && !$e instanceof \Throwable) { - throw new \InvalidArgumentException('Exception/Throwable expected, got '.gettype($e).' / '.get_class($e)); + throw new \InvalidArgumentException('Exception/Throwable expected, got '.gettype($e).' / '.Registry::getClass($e)); } $previousText = ''; if ($previous = $e->getPrevious()) { do { - $previousText .= ', '.get_class($previous).'(code: '.$previous->getCode().'): '.$previous->getMessage().' at '.$previous->getFile().':'.$previous->getLine(); + $previousText .= ', '.Registry::getClass($previous).'(code: '.$previous->getCode().'): '.$previous->getMessage().' at '.$previous->getFile().':'.$previous->getLine(); } while ($previous = $previous->getPrevious()); } - $str = '[object] ('.get_class($e).'(code: '.$e->getCode().'): '.$e->getMessage().' at '.$e->getFile().':'.$e->getLine().$previousText.')'; + $str = '[object] ('.Registry::getClass($e).'(code: '.$e->getCode().'): '.$e->getMessage().' at '.$e->getFile().':'.$e->getLine().$previousText.')'; if ($this->includeStacktraces) { $str .= "\n[stacktrace]\n".$e->getTraceAsString()."\n"; } diff --git a/src/Monolog/Formatter/MongoDBFormatter.php b/src/Monolog/Formatter/MongoDBFormatter.php index eb067bb7..46944c53 100644 --- a/src/Monolog/Formatter/MongoDBFormatter.php +++ b/src/Monolog/Formatter/MongoDBFormatter.php @@ -11,6 +11,8 @@ namespace Monolog\Formatter; +use Monolog\Registry; + /** * Formats a record for use with the MongoDBHandler. * @@ -75,7 +77,7 @@ class MongoDBFormatter implements FormatterInterface protected function formatObject($value, $nestingLevel) { $objectVars = get_object_vars($value); - $objectVars['class'] = get_class($value); + $objectVars['class'] = Registry::getClass($value); return $this->formatArray($objectVars, $nestingLevel); } @@ -83,7 +85,7 @@ class MongoDBFormatter implements FormatterInterface protected function formatException(\Exception $exception, $nestingLevel) { $formattedException = array( - 'class' => get_class($exception), + 'class' => Registry::getClass($exception), 'message' => $exception->getMessage(), 'code' => $exception->getCode(), 'file' => $exception->getFile() . ':' . $exception->getLine(), diff --git a/src/Monolog/Formatter/NormalizerFormatter.php b/src/Monolog/Formatter/NormalizerFormatter.php index 0d96ed09..50b4f50e 100644 --- a/src/Monolog/Formatter/NormalizerFormatter.php +++ b/src/Monolog/Formatter/NormalizerFormatter.php @@ -12,6 +12,7 @@ namespace Monolog\Formatter; use Exception; +use Monolog\Registry; /** * Normalizes incoming records to remove objects/resources so it's easier to dump to various targets @@ -108,7 +109,7 @@ class NormalizerFormatter implements FormatterInterface $value = $this->toJson($data, true); } - return sprintf("[object] (%s: %s)", get_class($data), $value); + return sprintf("[object] (%s: %s)", Registry::getClass($data), $value); } if (is_resource($data)) { @@ -122,11 +123,11 @@ class NormalizerFormatter implements FormatterInterface { // TODO 2.0 only check for Throwable if (!$e instanceof Exception && !$e instanceof \Throwable) { - throw new \InvalidArgumentException('Exception/Throwable expected, got '.gettype($e).' / '.get_class($e)); + throw new \InvalidArgumentException('Exception/Throwable expected, got '.gettype($e).' / '.Registry::getClass($e)); } $data = array( - 'class' => get_class($e), + 'class' => Registry::getClass($e), 'message' => $e->getMessage(), 'code' => $e->getCode(), 'file' => $e->getFile().':'.$e->getLine(), @@ -159,7 +160,7 @@ class NormalizerFormatter implements FormatterInterface // as a class name to avoid any unexpected leak of sensitive information $frame['args'] = array_map(function ($arg) { if (is_object($arg) && !($arg instanceof \DateTime || $arg instanceof \DateTimeInterface)) { - return sprintf("[object] (%s)", get_class($arg)); + return sprintf("[object] (%s)", Registry::getClass($arg)); } return $arg; diff --git a/src/Monolog/Processor/PsrLogMessageProcessor.php b/src/Monolog/Processor/PsrLogMessageProcessor.php index c2686ce5..f7df0074 100644 --- a/src/Monolog/Processor/PsrLogMessageProcessor.php +++ b/src/Monolog/Processor/PsrLogMessageProcessor.php @@ -11,6 +11,8 @@ namespace Monolog\Processor; +use Monolog\Registry; + /** * Processes a record's message according to PSR-3 rules * @@ -35,7 +37,7 @@ class PsrLogMessageProcessor if (is_null($val) || is_scalar($val) || (is_object($val) && method_exists($val, "__toString"))) { $replacements['{'.$key.'}'] = $val; } elseif (is_object($val)) { - $replacements['{'.$key.'}'] = '[object '.get_class($val).']'; + $replacements['{'.$key.'}'] = '[object '.Registry::getClass($val).']'; } else { $replacements['{'.$key.'}'] = '['.gettype($val).']'; } diff --git a/src/Monolog/Registry.php b/src/Monolog/Registry.php index 159b751c..b165ccd4 100644 --- a/src/Monolog/Registry.php +++ b/src/Monolog/Registry.php @@ -131,4 +131,14 @@ class Registry { return self::getInstance($name); } + + /** + * @internal + */ + public function getClass($object) + { + $class = \get_class($object); + + return 'c' === $class[0] && 0 === strpos($class, "class@anonymous\0") ? get_parent_class($class).'@anonymous' : $class; + } }