8000 [MonologBridge] Add monolog processors adding route and command info by trakos · Pull Request #28330 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[MonologBridge] Add monolog processors adding route and command info #28330

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

Merged
merged 1 commit into from
Mar 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Symfony/Bridge/Monolog/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
CHANGELOG
=========

4.3.0
-----

* added `ConsoleCommandProcessor`: monolog processor that adds command name and arguments
* added `RouteProcessor`: monolog processor that adds route name, controller::action and route params

4.2.0
-----

Expand Down
69 changes: 69 additions & 0 deletions src/Symfony/Bridge/Monolog/Processor/ConsoleCommandProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\Monolog\Processor;

use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Contracts\Service\ResetInterface;

/**
* Adds the current console command information to the log entry.
*
* @author Piotr Stankowski <git@trakos.pl>
*/
class ConsoleCommandProcessor implements EventSubscriberInterface, ResetInterface
{
private $commandData;
private $includeArguments;
private $includeOptions;

public function __construct(bool $includeArguments = true, bool $includeOptions = false)
{
$this->includeArguments = $includeArguments;
$this->includeOptions = $includeOptions;
}

public function __invoke(array $records)
{
if (null !== $this->commandData && !isset($records['extra']['command'])) {
$records['extra']['command'] = $this->commandData;
}

return $records;
}

public function reset()
{
$this->commandData = null;
}

public function addCommandData(ConsoleEvent $event)
{
$this->commandData = array(
'name' => $event->getCommand()->getName(),
);
if ($this->includeArguments) {
$this->commandData['arguments'] = $event->getInput()->getArguments();
}
if ($this->includeOptions) {
$this->commandData['options'] = $event->getInput()->getOptions();
}
}

public static function getSubscribedEvents()
{
return array(
ConsoleEvents::COMMAND => array('addCommandData', 1),
);
}
}
86 changes: 86 additions & 0 deletions src/Symfony/Bridge/Monolog/Processor/RouteProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\Monolog\Processor;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Contracts\Service\ResetInterface;

/**
* Adds the current route information to the log entry.
*
* @author Piotr Stankowski <git@trakos.pl>
*/
class RouteProcessor implements EventSubscriberInterface, ResetInterface
{
private $routeData;
private $includeParams;

public function __construct(bool $includeParams = true)
{
$this->includeParams = $includeParams;
$this->reset();
}

public function __invoke(array $records)
{
if ($this->routeData && !isset($records['extra']['requests'])) {
$records['extra']['requests'] = array_values($this->routeData);
}

return $records;
}

public function reset()
{
$this->routeData = array();
}

public function addRouteData(GetResponseEvent $event)
{
if ($event->isMasterRequest()) {
$this->reset();
}

$request = $event->getRequest();
if (!$request->attributes->has('_controller')) {
return;
}

$currentRequestData = array(
'controller' => $request->attributes->get('_controller'),
'route' => $request->attributes->get('_route'),
);

if ($this->includeParams) {
$currentRequestData['route_params'] = $request->attributes->get('_route_params');
}

$this->routeData[spl_object_id($request)] = $currentRequestData;
}

public function removeRouteData(FinishRequestEvent $event)
{
$requestId = spl_object_id($event->getRequest());
unset($this->routeData[$requestId]);
}

public static function getSubscribedEvents()
{
return array(
KernelEvents::REQUEST => array('addRouteData', 1),
KernelEvents::FINISH_REQUEST => array('removeRouteData', 1),
);
}
}
8000
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\Monolog\Tests\Processor;

use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Monolog\Processor\ConsoleCommandProcessor;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Event\ConsoleEvent;
use Symfony\Component\Console\Input\InputInterface;

class ConsoleCommandProcessorTest extends TestCase
{
private const TEST_ARGUMENTS = array('test' => 'argument');
private const TEST_OPTIONS = array('test' => 'option');
private const TEST_NAME = 'some:test';

public function testProcessor()
{
$processor = new ConsoleCommandProcessor();
$processor->addCommandData($this->getConsoleEvent());

$record = $processor(array('extra' => array()));

$this->assertArrayHasKey('command', $record['extra']);
$this->assertEquals(
array('name' => self::TEST_NAME, 'arguments' => self::TEST_ARGUMENTS),
$record['extra']['command']
);
}

public function testProcessorWithOptions()
{
$processor = new ConsoleCommandProcessor(true, true);
$processor->addCommandData($this->getConsoleEvent());

$record = $processor(array('extra' => array()));

$this->assertArrayHasKey('command', $record['extra']);
$this->assertEquals(
array('name' => self::TEST_NAME, 'arguments' => self::TEST_ARGUMENTS, 'options' => self::TEST_OPTIONS),
$record['extra']['command']
);
}

public function testProcessorDoesNothingWhenNotInConsole()
{
$processor = new ConsoleCommandProcessor(true, true);

$record = $processor(array('extra' => array()));
$this->assertEquals(array('extra' => array()), $record);
}

private function getConsoleEvent(): ConsoleEvent
{
$input = $this->getMockBuilder(InputInterface::class)->getMock();
$input->method('getArguments')->willReturn(self::TEST_ARGUMENTS);
$input->method('getOptions')->willReturn(self::TEST_OPTIONS);
$command = $this->getMockBuilder(Command::class)->disableOriginalConstructor()->getMock();
$command->method('getName')->willReturn(self::TEST_NAME);
$consoleEvent = $this->getMockBuilder(ConsoleEvent::class)->disableOriginalConstructor()->getMock();
$consoleEvent->method('getCommand')->willReturn($command);
$consoleEvent->method('getInput')->willReturn($input);

return $consoleEvent;
}
}
Loading
0