Skip to content

Commit

Permalink
Flush $rows array between render() calls
Browse files Browse the repository at this point in the history
  • Loading branch information
john-n-smith committed Nov 7, 2014
1 parent 60398ce commit 1695d79
Showing 1 changed file with 39 additions and 36 deletions.
75 changes: 39 additions & 36 deletions src/Graze/Monolog/Formatter/ConsoleFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@ class ConsoleFormatter extends NormalizerFormatter
Logger::ALERT => ['1;37','45'], // White/Purple
Logger::EMERGENCY => ['1;37','41'], // White/Red
);

/**
* @var array
*/
private $columnLengthMax = [];

/**
* @var array
*/
private $rows = [];

/**
* @param string $dateFormat The format of the timestamp: one supported by DateTime::format
*
Expand All @@ -53,28 +53,7 @@ public function __construct($dateFormat = null)
{
parent::__construct($dateFormat);
}

/**
* @param string $columnString,... unlimited OPTIONAL
*
* @return void
*/
protected function addRow()
{
$arguments = func_get_args();

// store the longest column so we can pad out to it on render
foreach ($arguments as $key => $value) {
$columnLength = strlen($value);

if (!isset($this->columnLengthMax[$key]) || $columnLength > $this->columnLengthMax[$key]) {
$this->columnLengthMax[$key] = $columnLength;
}
}

$this->rows[] = $arguments;
}


/**
* Formats a log record.
*
Expand All @@ -88,23 +67,44 @@ public function format(array $record)
$this->addRow('Message', (string) $record['message']);
$this->addRow('Time', $record['datetime']->format($this->dateFormat));
$this->addRow('Channel', $record['channel']);

if (isset($record['context']['file'])) {
$this->addRow('File', $record['context']['file']);
$this->addRow('Line', $record['context']['line']);
}

if (isset($record['context']['exception'])) {
$trace = explode(PHP_EOL, $record['context']['exception']->getTraceAsString());
$this->addRow('Trace', array_shift($trace));
foreach ($trace as $row) {
$this->addRow('', $row);
}
}

return $this->render($this->getColoursByLogLevel($record['level']));
}


/**
* @param string $columnString,... unlimited OPTIONAL
*
* @return void
*/
protected function addRow()
{
$arguments = func_get_args();

// store the longest column so we can pad out to it on render
foreach ($arguments as $key => $value) {
$columnLength = strlen($value);

if (!isset($this->columnLengthMax[$key]) || $columnLength > $this->columnLengthMax[$key]) {
$this->columnLengthMax[$key] = $columnLength;
}
}

$this->rows[] = $arguments;
}

/**
* @param array $colours
*
Expand All @@ -113,27 +113,30 @@ public function format(array $record)
protected function render($colours)
{
$separator = '+';

foreach ($this->columnLengthMax as $columnLength) {
$separator .= str_repeat('-', $columnLength + 2) . '+';
}

$output = $separator;

foreach ($this->rows as $row) {
$output .= PHP_EOL . '|';

foreach ($this->columnLengthMax as $key => $null) {
$cellContent = isset($row[$key]) ? $row[$key]: '';

$output .= ' ' . str_pad($cellContent, $this->columnLengthMax[$key]) . ' |';
}
}


// flush rows
$this->rows = [];

// Create the coloured string
return "\n\033[{$colours[0]}m\033[{$colours[1]}m" . $output . PHP_EOL . $separator . PHP_EOL . "\033[0m\n";
}

/**
* @param $logLevel
*
Expand Down

0 comments on commit 1695d79

Please sign in to comment.