From 42d84e6a8d94e68ba52e15bbdd58dde1023609ad Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Sun, 4 Nov 2018 18:23:20 +0100 Subject: [PATCH] Move getClass method to a Utils class, refs #1190 --- src/Monolog/ErrorHandler.php | 2 +- src/Monolog/Formatter/JsonFormatter.php | 6 ++--- src/Monolog/Formatter/LineFormatter.php | 8 +++--- src/Monolog/Formatter/MongoDBFormatter.php | 6 ++--- src/Monolog/Formatter/NormalizerFormatter.php | 10 ++++---- .../Processor/PsrLogMessageProcessor.php | 4 +-- src/Monolog/Utils.php | 25 +++++++++++++++++++ 7 files changed, 43 insertions(+), 18 deletions(-) create mode 100644 src/Monolog/Utils.php diff --git a/src/Monolog/ErrorHandler.php b/src/Monolog/ErrorHandler.php index 7c7d48c2..adc55bdf 100644 --- a/src/Monolog/ErrorHandler.php +++ b/src/Monolog/ErrorHandler.php @@ -134,7 +134,7 @@ class ErrorHandler { $this->logger->log( $this->uncaughtExceptionLevel === null ? LogLevel::ERROR : $this->uncaughtExceptionLevel, - sprintf('Uncaught Exception %s: "%s" at %s line %s', Registry::getClass($e), $e->getMessage(), $e->getFile(), $e->getLine()), + sprintf('Uncaught Exception %s: "%s" at %s line %s', Utils::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 fcb08474..9bd305f2 100644 --- a/src/Monolog/Formatter/JsonFormatter.php +++ b/src/Monolog/Formatter/JsonFormatter.php @@ -12,7 +12,7 @@ namespace Monolog\Formatter; use Exception; -use Monolog\Registry; +use Monolog\Utils; use Throwable; /** @@ -180,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).' / '.Registry::getClass($e)); + throw new \InvalidArgumentException('Exception/Throwable expected, got '.gettype($e).' / '.Utils::getClass($e)); } $data = array( - 'class' => Registry::getClass($e), + 'class' => Utils::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 939cd043..f98e1a6f 100644 --- a/src/Monolog/Formatter/LineFormatter.php +++ b/src/Monolog/Formatter/LineFormatter.php @@ -11,7 +11,7 @@ namespace Monolog\Formatter; -use Monolog\Registry; +use Monolog\Utils; /** * Formats incoming records into a one-line string @@ -131,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).' / '.Registry::getClass($e)); + throw new \InvalidArgumentException('Exception/Throwable expected, got '.gettype($e).' / '.Utils::getClass($e)); } $previousText = ''; if ($previous = $e->getPrevious()) { do { - $previousText .= ', '.Registry::getClass($previous).'(code: '.$previous->getCode().'): '.$previous->getMessage().' at '.$previous->getFile().':'.$previous->getLine(); + $previousText .= ', '.Utils::getClass($previous).'(code: '.$previous->getCode().'): '.$previous->getMessage().' at '.$previous->getFile().':'.$previous->getLine(); } while ($previous = $previous->getPrevious()); } - $str = '[object] ('.Registry::getClass($e).'(code: '.$e->getCode().'): '.$e->getMessage().' at '.$e->getFile().':'.$e->getLine().$previousText.')'; + $str = '[object] ('.Utils::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 46944c53..eb7be849 100644 --- a/src/Monolog/Formatter/MongoDBFormatter.php +++ b/src/Monolog/Formatter/MongoDBFormatter.php @@ -11,7 +11,7 @@ namespace Monolog\Formatter; -use Monolog\Registry; +use Monolog\Utils; /** * Formats a record for use with the MongoDBHandler. @@ -77,7 +77,7 @@ class MongoDBFormatter implements FormatterInterface protected function formatObject($value, $nestingLevel) { $objectVars = get_object_vars($value); - $objectVars['class'] = Registry::getClass($value); + $objectVars['class'] = Utils::getClass($value); return $this->formatArray($objectVars, $nestingLevel); } @@ -85,7 +85,7 @@ class MongoDBFormatter implements FormatterInterface protected function formatException(\Exception $exception, $nestingLevel) { $formattedException = array( - 'class' => Registry::getClass($exception), + 'class' => Utils::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 50b4f50e..66866578 100644 --- a/src/Monolog/Formatter/NormalizerFormatter.php +++ b/src/Monolog/Formatter/NormalizerFormatter.php @@ -12,7 +12,7 @@ namespace Monolog\Formatter; use Exception; -use Monolog\Registry; +use Monolog\Utils; /** * Normalizes incoming records to remove objects/resources so it's easier to dump to various targets @@ -109,7 +109,7 @@ class NormalizerFormatter implements FormatterInterface $value = $this->toJson($data, true); } - return sprintf("[object] (%s: %s)", Registry::getClass($data), $value); + return sprintf("[object] (%s: %s)", Utils::getClass($data), $value); } if (is_resource($data)) { @@ -123,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).' / '.Registry::getClass($e)); + throw new \InvalidArgumentException('Exception/Throwable expected, got '.gettype($e).' / '.Utils::getClass($e)); } $data = array( - 'class' => Registry::getClass($e), + 'class' => Utils::getClass($e), 'message' => $e->getMessage(), 'code' => $e->getCode(), 'file' => $e->getFile().':'.$e->getLine(), @@ -160,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)", Registry::getClass($arg)); + return sprintf("[object] (%s)", Utils::getClass($arg)); } return $arg; diff --git a/src/Monolog/Processor/PsrLogMessageProcessor.php b/src/Monolog/Processor/PsrLogMessageProcessor.php index 543c4584..00885054 100644 --- a/src/Monolog/Processor/PsrLogMessageProcessor.php +++ b/src/Monolog/Processor/PsrLogMessageProcessor.php @@ -11,7 +11,7 @@ namespace Monolog\Processor; -use Monolog\Registry; +use Monolog\Utils; /** * Processes a record's message according to PSR-3 rules @@ -37,7 +37,7 @@ class PsrLogMessageProcessor implements ProcessorInterface if (is_null($val) || is_scalar($val) || (is_object($val) && method_exists($val, "__toString"))) { $replacements['{'.$key.'}'] = $val; } elseif (is_object($val)) { - $replacements['{'.$key.'}'] = '[object '.Registry::getClass($val).']'; + $replacements['{'.$key.'}'] = '[object '.Utils::getClass($val).']'; } else { $replacements['{'.$key.'}'] = '['.gettype($val).']'; } diff --git a/src/Monolog/Utils.php b/src/Monolog/Utils.php new file mode 100644 index 00000000..df611a0d --- /dev/null +++ b/src/Monolog/Utils.php @@ -0,0 +1,25 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Monolog; + +class Utils +{ + /** + * @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; + } +}