Skip to content

Commit

Permalink
Merge pull request #26 from Mapudo/MAP-3396
Browse files Browse the repository at this point in the history
MAP-3396 Allow channels for LogMiddleware
  • Loading branch information
kobelobster authored Apr 12, 2019
2 parents ae5db15 + 0963265 commit d05b05b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All Notable changes to `mapudo/guzzle-bundle` will be documented in this file.

Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) principles.

## [2.3.0] - 2019-04-12
### Added
- Added functionality to define channels when registering LogMiddleware. This allows a user, to inject a specific logger into the LogMiddleware dependent on the channel.

## [2.2.1] - 2018-12-07
### Changed
- Fix `duration` in `LogMiddleware` parameter due to it needs to be passed by reference to a method to work properly
Expand Down
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,24 @@ After adding this handler you need to rebuild the assets so your Profiler is sty
Here you can select which client's request to be shown.
![Symfony Profiler](assets/images/profiler_screenshot.png)

#### Log into specific channels

Per default the LogMiddleware logs into the "guzzle" channel. If you want that a different handler is used (dependent on the channel) you can define a service tag for this.

YAML
```yaml
mapudo_frontend.mapudo_api.log_middleware:
class: Mapudo\Bundle\GuzzleBundle\Middleware\LogMiddleware
tags:
- { name: guzzle.middleware, method: log, client: mapudo_api, channel: timings }
```

As you can see, the tag now contain a "channel" node. If configured like this, the guzzle client `mapudo_api` will have the `LogMiddleware` as a middleware injected that uses a/multiple logger which log into the timings channel.

**Important**
> Please note, that the service id of the LogMiddleware must contain the name `log_middleware` since the compiler pass checks if the service definition contains this name to retrieve the channel.



#### Add your own Logging
This bundle uses Symfony's normalizer to normalize the request and response objects before passing them to the logger.
Expand Down
18 changes: 14 additions & 4 deletions src/DependencyInjection/Compiler/Builder/DefinitionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\ExpressionLanguage\Expression;
use function array_key_exists;
use function reset;
use function sprintf;
use function strpos;

/**
* Class DefinitionBuilder
Expand Down Expand Up @@ -34,9 +38,16 @@ public function getHandlerDefinition(
$handler = new Definition('%guzzle_http.handler_stack.class%');
$handler->setFactory(['%guzzle_http.handler_stack.class%', 'create']);

// The default channel we want to log to is guzzle, however the user can alter this
// by submitting the "channel" in the tags when registering the log middleware
$logChannel = 'guzzle';
foreach ($middleware as $id => $tags) {
$attributes = reset($tags);

// Currently untested code -> Must be tested in the code refactor PR
if (array_key_exists('channel', $attributes) && strpos($id, 'log_middleware') !== false) {
$logChannel = $attributes['channel'];
}
if (!empty($attributes['method'])) {
$middlewareExpression = new Expression(sprintf('service("%s").%s()', $id, $attributes['method']));
} else {
Expand All @@ -49,8 +60,7 @@ public function getHandlerDefinition(
$container->setDefinition($eventServiceName, $this->getEventMiddlewareDefinition($clientName));

$logServiceName = sprintf('guzzle_bundle.middleware.log.%s', $clientName);
$container->setDefinition($logServiceName, $this->getLogMiddlewareDefinition($clientName));

$container->setDefinition($logServiceName, $this->getLogMiddlewareDefinition($clientName, $logChannel));
$eventExpression = new Expression(sprintf('service("%s").dispatch()', $eventServiceName));
$logExpression = new Expression(sprintf('service("%s").log()', $logServiceName));

Expand Down Expand Up @@ -79,10 +89,10 @@ public function getEventMiddlewareDefinition(string $clientName): Definition
* @param string $clientName
* @return Definition
*/
public function getLogMiddlewareDefinition(string $clientName): Definition
public function getLogMiddlewareDefinition(string $clientName, string $logChannel = 'guzzle'): Definition
{
$logMiddleware = (new Definition('%mapudo.guzzle.middleware.log_middleware.class%'))
->addArgument(new Reference('monolog.logger.guzzle'))
->addArgument(new Reference(sprintf('monolog.logger.%s', $logChannel)))
->addArgument(new Reference('guzzle_bundle.formatter'))
->addArgument(new Reference('mapudo_bundle_guzzle.serializer'));

Expand Down

0 comments on commit d05b05b

Please sign in to comment.