-
Notifications
You must be signed in to change notification settings - Fork 4
Debug logger #213
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Debug logger #213
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,6 +59,7 @@ public function __invoke(ContainerInterface $c): CliMenu | |
$builder->addItem( | ||
$exercise->getName(), | ||
function (CliMenu $menu) use ($exerciseRenderer, $eventDispatcher, $exercise) { | ||
$menu->close(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is it moved here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, read the pr description! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah jesus, my bad 😂 🤦 |
||
$this->dispatchExerciseSelectedEvent($eventDispatcher, $exercise); | ||
$exerciseRenderer->__invoke($menu); | ||
}, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpSchool\PhpWorkshop\Logger; | ||
|
||
use Colors\Color; | ||
use PhpSchool\PhpWorkshop\Output\OutputInterface; | ||
use Psr\Log\AbstractLogger; | ||
use Psr\Log\LoggerInterface; | ||
|
||
class ConsoleLogger extends AbstractLogger implements LoggerInterface | ||
{ | ||
/** | ||
* @var OutputInterface | ||
*/ | ||
private $output; | ||
|
||
/** | ||
* @var Color | ||
*/ | ||
private $color; | ||
|
||
public function __construct(OutputInterface $output, Color $color) | ||
{ | ||
$this->output = $output; | ||
$this->color = $color; | ||
} | ||
|
||
public function log($level, $message, array $context = []): void | ||
{ | ||
$parts = [ | ||
sprintf( | ||
'%s - %s - %s', | ||
$this->color->fg('yellow', (new \DateTime())->format('H:i:s')), | ||
$this->color->bg('red', strtoupper($level)), | ||
$this->color->fg('red', $message) | ||
), | ||
json_encode($context, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT) | ||
]; | ||
|
||
$this->output->writeLine(implode("\n", $parts)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace PhpSchool\PhpWorkshopTest\Logger; | ||
|
||
use PhpSchool\CliMenu\Util\StringUtil; | ||
use PhpSchool\PhpWorkshop\Logger\ConsoleLogger; | ||
use PhpSchool\PhpWorkshop\Utils\StringUtils; | ||
use PhpSchool\PhpWorkshopTest\ContainerAwareTest; | ||
use Psr\Log\LoggerInterface; | ||
|
||
class ConsoleLoggerTest extends ContainerAwareTest | ||
{ | ||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->container->set('phpschoolGlobalDir', $this->getTemporaryDirectory()); | ||
$this->container->set('appName', 'my-workshop'); | ||
$this->container->set('debugMode', true); | ||
} | ||
|
||
public function testConsoleLoggerIsCreatedIfDebugModeEnable(): void | ||
{ | ||
$this->assertInstanceOf(ConsoleLogger::class, $this->container->get(LoggerInterface::class)); | ||
} | ||
|
||
public function testLoggerWithContext(): void | ||
{ | ||
$logger = $this->container->get(LoggerInterface::class); | ||
$logger->critical('Failed to copy file', ['exercise' => 'my-exercise']); | ||
|
||
$out = StringUtil::stripAnsiEscapeSequence($this->getActualOutputForAssertion()); | ||
|
||
$match = '/\d{2}\:\d{2}\:\d{2} - CRITICAL - Failed to copy file\n{\n "exercise": "my-exercise"\n}/'; | ||
$this->assertMatchesRegularExpression($match, $out); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just wondering why 👀