This repository has been archived by the owner on Jan 17, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(logging): log collecting in the symfony profiler works
- Loading branch information
Showing
14 changed files
with
265 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
55 changes: 55 additions & 0 deletions
55
src/Bridge/Symfony/Bundle/DependencyInjection/CompilerPass/DebugLogProcessorPass.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace K911\Swoole\Bridge\Symfony\Bundle\DependencyInjection\CompilerPass; | ||
|
||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
|
||
/** | ||
* This is an override for Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddDebugLogProcessorPass | ||
* which removes debug processor from loggers when Symfony is run from CLI. | ||
* Without debug log processor, there are no logs being shown in the symfony profiler. This override | ||
* replaces the original configurator for 'monolog.logger_prototype' with a custom one, which removes | ||
* the debug log processor only when being run from PHPDBG. | ||
*/ | ||
final class DebugLogProcessorPass implements CompilerPassInterface | ||
{ | ||
public function process(ContainerBuilder $container): void | ||
{ | ||
if (!$container->hasDefinition('profiler')) { | ||
return; | ||
} | ||
if (!$container->hasDefinition('monolog.logger_prototype')) { | ||
return; | ||
} | ||
if (!$container->hasDefinition('debug.log_processor')) { | ||
return; | ||
} | ||
|
||
$definition = $container->getDefinition('monolog.logger_prototype'); | ||
$definition->setConfigurator([__CLASS__, 'configureLogger']); | ||
} | ||
|
||
public static function configureLogger(object $logger): void | ||
{ | ||
if (!\method_exists($logger, 'removeDebugLogger')) { | ||
return; | ||
} | ||
|
||
if (!\in_array(\PHP_SAPI, ['cli', 'phpdbg'], true)) { | ||
return; | ||
} | ||
|
||
if (\PHP_SAPI === 'cli') { | ||
foreach ($_SERVER['argv'] as $arg) { | ||
if (false !== \mb_strpos($arg, 'swoole:server:')) { | ||
return; | ||
} | ||
} | ||
} | ||
|
||
$logger->removeDebugLogger(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace K911\Swoole\Tests\Feature; | ||
|
||
use K911\Swoole\Client\HttpClient; | ||
use K911\Swoole\Tests\Fixtures\Symfony\TestBundle\Test\ServerTestCase; | ||
|
||
final class SymfonyProfilerTest extends ServerTestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
// problem with messenger support in symfony profiler in symfony 4.3 | ||
$this->markTestSkippedIfSymfonyVersionIsLoverThan('4.4.0'); | ||
$this->markTestSkippedIfXdebugEnabled(); | ||
} | ||
|
||
public function testAdvancedStaticFilesServerWithAutoRegistration(): void | ||
{ | ||
$serverRun = $this->createConsoleProcess([ | ||
'swoole:server:run', | ||
'--host=localhost', | ||
'--port=9999', | ||
], ['APP_ENV' => 'profiler']); | ||
|
||
$serverRun->setTimeout(10); | ||
$serverRun->start(); | ||
|
||
$this->runAsCoroutineAndWait(function (): void { | ||
$client = HttpClient::fromDomain('localhost', 9999, false); | ||
$this->assertTrue($client->connect()); | ||
|
||
$response = $client->send('/twig')['response']; | ||
|
||
$this->assertSame(200, $response['statusCode']); | ||
$this->assertNotEmpty($response['headers']['x-debug-token']); | ||
$debugToken = $response['headers']['x-debug-token']; | ||
|
||
$profilerResponse = $client->send('/_wdt/'.$debugToken)['response']; | ||
|
||
$this->assertStringContainsString('sf-toolbar-block-logger sf-toolbar-status-red', $profilerResponse['body']); | ||
}); | ||
|
||
$serverRun->stop(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
tests/Fixtures/Symfony/TestBundle/Controller/TwigController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace K911\Swoole\Tests\Fixtures\Symfony\TestBundle\Controller; | ||
|
||
use InvalidArgumentException; | ||
use Psr\Log\LoggerInterface; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\Routing\Annotation\Route; | ||
use Twig\Environment; | ||
use Twig\Error\LoaderError; | ||
use Twig\Error\RuntimeError; | ||
use Twig\Error\SyntaxError; | ||
|
||
class TwigController | ||
{ | ||
/** | ||
* @var Environment | ||
*/ | ||
private $environment; | ||
|
||
/** | ||
* @var LoggerInterface | ||
*/ | ||
private $logger; | ||
|
||
public function __construct(Environment $environment, LoggerInterface $logger) | ||
{ | ||
$this->environment = $environment; | ||
$this->logger = $logger; | ||
} | ||
|
||
/** | ||
* @Route("/twig") | ||
* | ||
* @throws InvalidArgumentException | ||
* @throws LoaderError | ||
* @throws RuntimeError | ||
* @throws SyntaxError | ||
*/ | ||
public function indexAction(): Response | ||
{ | ||
$this->logger->error('Profiler logging test.'); | ||
|
||
return new Response($this->environment->render('base.html.twig')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
framework: | ||
profiler: { only_exceptions: false } |
7 changes: 7 additions & 0 deletions
7
tests/Fixtures/Symfony/app/config/profiler/routing/routing.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
web_profiler_wdt: | ||
resource: '@WebProfilerBundle/Resources/config/routing/wdt.xml' | ||
prefix: /_wdt | ||
|
||
web_profiler_profiler: | ||
resource: '@WebProfilerBundle/Resources/config/routing/profiler.xml' | ||
prefix: /_profiler |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
web_profiler: | ||
toolbar: true | ||
intercept_redirects: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
services: | ||
# default configuration for services in *this* file | ||
_defaults: | ||
# automatically injects dependencies in your services | ||
autowire: true | ||
# automatically registers your services as commands, event subscribers, etc. | ||
autoconfigure: true | ||
# this means you cannot fetch services directly from the container via $container->get() | ||
# if you need to do this, you can override this setting on individual services | ||
public: false | ||
|
||
# makes classes in src/ available to be used as services | ||
# this creates a service per class whose id is the fully-qualified class name | ||
K911\Swoole\Tests\Fixtures\Symfony\TestBundle\: | ||
resource: '../../TestBundle/*' | ||
# you can exclude directories or files | ||
# but if a service is unused, it's removed anyway | ||
exclude: '../../TestBundle/{Message,Test,Controller}' | ||
|
||
# controllers are imported separately to make sure they | ||
# have the tag that allows actions to type-hint services | ||
K911\Swoole\Tests\Fixtures\Symfony\TestBundle\Controller\: | ||
resource: '../../TestBundle/Controller' | ||
# without this the HMR tests break, might be a bug in HMR | ||
exclude: '../../TestBundle/Controller/ReplacedContentTestController.php' | ||
tags: ['controller.service_arguments'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>Welcome!</title> | ||
</head> | ||
<body> | ||
<h1>Welcome to Swoole Server</h1> | ||
</body> | ||
</html> |