diff --git a/src/Monolog/Processor/MemoryPeakUsageProcessor.php b/src/Monolog/Processor/MemoryPeakUsageProcessor.php index e48672bf..552fd709 100644 --- a/src/Monolog/Processor/MemoryPeakUsageProcessor.php +++ b/src/Monolog/Processor/MemoryPeakUsageProcessor.php @@ -26,7 +26,7 @@ class MemoryPeakUsageProcessor extends MemoryProcessor public function __invoke(array $record) { $bytes = memory_get_peak_usage($this->realUsage); - $formatted = self::formatBytes($bytes); + $formatted = $this->formatBytes($bytes); $record['extra'] = array_merge( $record['extra'], diff --git a/src/Monolog/Processor/MemoryProcessor.php b/src/Monolog/Processor/MemoryProcessor.php index 7551043e..9058b57f 100644 --- a/src/Monolog/Processor/MemoryProcessor.php +++ b/src/Monolog/Processor/MemoryProcessor.php @@ -18,26 +18,38 @@ namespace Monolog\Processor; */ abstract class MemoryProcessor { + /** + * @var boolean Set this to true to get the real size of memory allocated from system. + * If not set or false only the memory used by emalloc() is reported. + */ protected $realUsage; + protected $useFormatting; + /** - * @param boolean $realUsage + * @param boolean $realUsage Set this to true to get the real size of memory allocated from system. + * @param bool $useFormatting If true, then format memory size to human readable string (MB, KB, B depending on size) */ - public function __construct($realUsage = true) + public function __construct($realUsage = true, $useFormatting = true) { $this->realUsage = (boolean) $realUsage; + $this->useFormatting = (boolean) $useFormatting; } /** - * Formats bytes into a human readable string + * Formats bytes into a human readable string (if $this->useFormatting is true) * * @param int $bytes - * @return string + * @return string|int */ - protected static function formatBytes($bytes) + protected function formatBytes($bytes) { $bytes = (int) $bytes; + if (!$this->useFormatting) { + return $bytes; + } + if ($bytes > 1024*1024) { return round($bytes/1024/1024, 2).' MB'; } elseif ($bytes > 1024) { diff --git a/src/Monolog/Processor/MemoryUsageProcessor.php b/src/Monolog/Processor/MemoryUsageProcessor.php index 2c4a8079..0c4dd9ab 100644 --- a/src/Monolog/Processor/MemoryUsageProcessor.php +++ b/src/Monolog/Processor/MemoryUsageProcessor.php @@ -26,7 +26,7 @@ class MemoryUsageProcessor extends MemoryProcessor public function __invoke(array $record) { $bytes = memory_get_usage($this->realUsage); - $formatted = self::formatBytes($bytes); + $formatted = $this->formatBytes($bytes); $record['extra'] = array_merge( $record['extra'], diff --git a/tests/Monolog/Processor/MemoryPeakUsageProcessorTest.php b/tests/Monolog/Processor/MemoryPeakUsageProcessorTest.php index 4bdf22c3..eb666144 100644 --- a/tests/Monolog/Processor/MemoryPeakUsageProcessorTest.php +++ b/tests/Monolog/Processor/MemoryPeakUsageProcessorTest.php @@ -26,4 +26,17 @@ class MemoryPeakUsageProcessorTest extends TestCase $this->assertArrayHasKey('memory_peak_usage', $record['extra']); $this->assertRegExp('#[0-9.]+ (M|K)?B$#', $record['extra']['memory_peak_usage']); } + + /** + * @covers Monolog\Processor\MemoryPeakUsageProcessor::__invoke + * @covers Monolog\Processor\MemoryProcessor::formatBytes + */ + public function testProcessorWithoutFormatting() + { + $processor = new MemoryPeakUsageProcessor(true, false); + $record = $processor($this->getRecord()); + $this->assertArrayHasKey('memory_peak_usage', $record['extra']); + $this->assertInternalType('int', $record['extra']['memory_peak_usage']); + $this->assertGreaterThan(0, $record['extra']['memory_peak_usage']); + } } diff --git a/tests/Monolog/Processor/MemoryUsageProcessorTest.php b/tests/Monolog/Processor/MemoryUsageProcessorTest.php index a30d6de6..4692dbfc 100644 --- a/tests/Monolog/Processor/MemoryUsageProcessorTest.php +++ b/tests/Monolog/Processor/MemoryUsageProcessorTest.php @@ -26,4 +26,17 @@ class MemoryUsageProcessorTest extends TestCase $this->assertArrayHasKey('memory_usage', $record['extra']); $this->assertRegExp('#[0-9.]+ (M|K)?B$#', $record['extra']['memory_usage']); } + + /** + * @covers Monolog\Processor\MemoryUsageProcessor::__invoke + * @covers Monolog\Processor\MemoryProcessor::formatBytes + */ + public function testProcessorWithoutFormatting() + { + $processor = new MemoryUsageProcessor(true, false); + $record = $processor($this->getRecord()); + $this->assertArrayHasKey('memory_usage', $record['extra']); + $this->assertInternalType('int', $record['extra']['memory_usage']); + $this->assertGreaterThan(0, $record['extra']['memory_usage']); + } }