Skip to content
This repository has been archived by the owner on Apr 16, 2024. It is now read-only.

Commit

Permalink
Merge pull request #82 from niels-nijens/logging-improvements
Browse files Browse the repository at this point in the history
Implemented output formatting for missing log levels
  • Loading branch information
niels-nijens committed Dec 15, 2015
2 parents dba7088 + 4e5a4f1 commit dc2eb45
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 10 deletions.
10 changes: 9 additions & 1 deletion src/Console/Formatter/OutputFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Accompli\Console\Formatter;

use Psr\Log\LogLevel;
use Symfony\Component\Console\Formatter\OutputFormatter as BaseOutputFormatter;
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
use Symfony\Component\Console\Formatter\OutputFormatterStyleInterface;
Expand All @@ -23,7 +24,14 @@ public function __construct($decorated = false, array $styles = array())
{
parent::__construct($decorated, $styles);

$this->setStyle('info', new OutputFormatterStyle());
$this->setStyle(LogLevel::EMERGENCY, new OutputFormatterStyle('white', 'red'));
$this->setStyle(LogLevel::CRITICAL, new OutputFormatterStyle('white', 'red'));
$this->setStyle(LogLevel::ALERT, new OutputFormatterStyle('white', 'red'));
$this->setStyle(LogLevel::ERROR, new OutputFormatterStyle('white', 'red'));
$this->setStyle(LogLevel::WARNING, new OutputFormatterStyle('white', 'red'));
$this->setStyle(LogLevel::NOTICE, new OutputFormatterStyle('white', 'blue'));
$this->setStyle(LogLevel::INFO, new OutputFormatterStyle());
$this->setStyle(LogLevel::DEBUG, new OutputFormatterStyle('black', 'yellow'));
$this->setStyle('event-name', new OutputFormatterStyle('yellow'));
$this->setStyle('event-task-name', new OutputFormatterStyle('yellow'));
$this->setStyle('event-task-action-in_progress', new OutputFormatterStyle());
Expand Down
11 changes: 11 additions & 0 deletions src/Console/Logger/ConsoleLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ class ConsoleLogger extends AbstractLogger
*/
const ANSI_CURSOR_FORWARD_FORMAT = "\e[%dC";

/**
* ANSI code to move the cursor back by the specified number of columns without changing lines.
* If the cursor is already in the leftmost column, this sequence is ignored.
*
* @var string
*/
const ANSI_CURSOR_BACKWARD_FORMAT = "\e[%dD";

/**
* ANSI code to clear the screen from cursor to end of display. The cursor position is unchanged.
*
Expand Down Expand Up @@ -227,6 +235,9 @@ private function getMessageSectionsFromContext(array $context)
private function getTaskActionStatusSectionFromContext(array $context)
{
$actionStatusSection = '';
if ($this->output->isDecorated()) {
$actionStatusSection = sprintf(self::ANSI_CURSOR_BACKWARD_FORMAT, 1);
}
if (isset($context['event.task.action']) && isset($this->taskActionStatusToOutputMap[$context['event.task.action']])) {
$actionStatusSection = sprintf('[<event-task-action-%1$s>%2$s</event-task-action-%1$s>]', $context['event.task.action'], $this->taskActionStatusToOutputMap[$context['event.task.action']]);
}
Expand Down
37 changes: 31 additions & 6 deletions tests/Console/Formatter/OutputFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Accompli\Console\Formatter\OutputFormatter;
use PHPUnit_Framework_TestCase;
use Psr\Log\LogLevel;

/**
* OutputFormatterTest.
Expand All @@ -14,15 +15,39 @@ class OutputFormatterTest extends PHPUnit_Framework_TestCase
{
/**
* Tests if constructing a new OutputFormatter sets the styles.
*
* @dataProvider provideTestConstruct
*
* @param string $styleName
*/
public function testConstruct()
public function testConstruct($styleName)
{
$formatter = new OutputFormatter();

$this->assertTrue($formatter->hasStyle('event-name'));
$this->assertTrue($formatter->hasStyle('event-task-name'));
$this->assertTrue($formatter->hasStyle('event-task-action-in_progress'));
$this->assertTrue($formatter->hasStyle('event-task-action-failed'));
$this->assertTrue($formatter->hasStyle('event-task-action-completed'));
$this->assertTrue($formatter->hasStyle($styleName));
}

/**
* Returns an array with test cases for @see testConstruct.
*
* @return array
*/
public function provideTestConstruct()
{
return array(
array(LogLevel::EMERGENCY),
array(LogLevel::CRITICAL),
array(LogLevel::ALERT),
array(LogLevel::ERROR),
array(LogLevel::WARNING),
array(LogLevel::NOTICE),
array(LogLevel::INFO),
array(LogLevel::DEBUG),
array('event-name'),
array('event-task-name'),
array('event-task-action-in_progress'),
array('event-task-action-failed'),
array('event-task-action-completed'),
);
}
}
6 changes: 3 additions & 3 deletions tests/Console/Logger/ConsoleLoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public function testLogReplaceLine()
{
$outputMock = $this->getMockBuilder('Symfony\Component\Console\Output\OutputInterface')->getMock();
$outputMock->expects($this->exactly(2))->method('getVerbosity')->willReturn(OutputInterface::VERBOSITY_NORMAL);
$outputMock->expects($this->exactly(3))->method('isDecorated')->willReturn(true);
$outputMock->expects($this->exactly(5))->method('isDecorated')->willReturn(true);
$outputMock->expects($this->exactly(3))
->method('getFormatter')
->willReturn(new OutputFormatter(true));
Expand All @@ -215,8 +215,8 @@ public function testLogReplaceLine()
$outputMock->expects($this->exactly(2))
->method('writeln')
->withConsecutive(
array($this->equalTo('[<event-name>accompli.test </event-name>][<event-task-name>TestTask </event-task-name>] <info>message</info>')),
array($this->equalTo(' <info>message</info>'))
array($this->equalTo("[<event-name>accompli.test </event-name>][<event-task-name>TestTask </event-task-name>] \e[1D <info>message</info>")),
array($this->equalTo(" \e[1D <info>message</info>"))
);

$logger = $this->getMockBuilder('Accompli\Console\Logger\ConsoleLogger')
Expand Down

0 comments on commit dc2eb45

Please sign in to comment.