1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-06 05:07:36 +02:00

Merge pull request #340 from hver/processors

Added an option to MemoryProcessors to disable formatting.
This commit is contained in:
Jordi Boggiano
2014-03-31 19:22:13 +02:00
5 changed files with 47 additions and 7 deletions

View File

@@ -26,7 +26,7 @@ class MemoryPeakUsageProcessor extends MemoryProcessor
public function __invoke(array $record) public function __invoke(array $record)
{ {
$bytes = memory_get_peak_usage($this->realUsage); $bytes = memory_get_peak_usage($this->realUsage);
$formatted = self::formatBytes($bytes); $formatted = $this->formatBytes($bytes);
$record['extra'] = array_merge( $record['extra'] = array_merge(
$record['extra'], $record['extra'],

View File

@@ -18,26 +18,40 @@ namespace Monolog\Processor;
*/ */
abstract class MemoryProcessor abstract class MemoryProcessor
{ {
/**
* @var boolean If true, get the real size of memory allocated from system. Else, only the memory used by emalloc() is reported.
*/
protected $realUsage; protected $realUsage;
/** /**
* @param boolean $realUsage * @var boolean If true, then format memory size to human readable string (MB, KB, B depending on size)
*/ */
public function __construct($realUsage = true) protected $useFormatting;
/**
* @param boolean $realUsage Set this to true to get the real size of memory allocated from system.
* @param boolean $useFormatting If true, then format memory size to human readable string (MB, KB, B depending on size)
*/
public function __construct($realUsage = true, $useFormatting = true)
{ {
$this->realUsage = (boolean) $realUsage; $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, otherwise return $bytes as is
* *
* @param int $bytes * @param int $bytes
* @return string * @return string|int Formatted string if $this->useFormatting is true, otherwise return $bytes as is
*/ */
protected static function formatBytes($bytes) protected function formatBytes($bytes)
{ {
$bytes = (int) $bytes; $bytes = (int) $bytes;
if (!$this->useFormatting) {
return $bytes;
}
if ($bytes > 1024*1024) { if ($bytes > 1024*1024) {
return round($bytes/1024/1024, 2).' MB'; return round($bytes/1024/1024, 2).' MB';
} elseif ($bytes > 1024) { } elseif ($bytes > 1024) {

View File

@@ -26,7 +26,7 @@ class MemoryUsageProcessor extends MemoryProcessor
public function __invoke(array $record) public function __invoke(array $record)
{ {
$bytes = memory_get_usage($this->realUsage); $bytes = memory_get_usage($this->realUsage);
$formatted = self::formatBytes($bytes); $formatted = $this->formatBytes($bytes);
$record['extra'] = array_merge( $record['extra'] = array_merge(
$record['extra'], $record['extra'],

View File

@@ -26,4 +26,17 @@ class MemoryPeakUsageProcessorTest extends TestCase
$this->assertArrayHasKey('memory_peak_usage', $record['extra']); $this->assertArrayHasKey('memory_peak_usage', $record['extra']);
$this->assertRegExp('#[0-9.]+ (M|K)?B$#', $record['extra']['memory_peak_usage']); $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']);
}
} }

View File

@@ -26,4 +26,17 @@ class MemoryUsageProcessorTest extends TestCase
$this->assertArrayHasKey('memory_usage', $record['extra']); $this->assertArrayHasKey('memory_usage', $record['extra']);
$this->assertRegExp('#[0-9.]+ (M|K)?B$#', $record['extra']['memory_usage']); $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']);
}
} }