Skip to content

Commit

Permalink
Merge pull request #3 from wshafer/more-handlers
Browse files Browse the repository at this point in the history
More handlers and dedup code
  • Loading branch information
Westin Shafer authored Aug 29, 2017
2 parents fbc01cc + bb7afaf commit 6057b3b
Show file tree
Hide file tree
Showing 31 changed files with 504 additions and 472 deletions.
107 changes: 105 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@
- [BufferHandler](#bufferhandler)
- [GroupHandler](#grouphandler)
- [FilterHandler](#filterhandler)
- [SamplingHandler](#samplinghandler)
- [NullHandler](#nullhandler)
- [PsrHandler](#psrhandler)
- [TestHandler](#testhandler)
- [Formatters](#formatters)
- [LineFomatter](#linefomatter)
- [HtmlFormatter](#htmlformatter)
Expand Down Expand Up @@ -1661,7 +1665,7 @@ return [
],
];
```
Monolog Docs: [DeduplicationHandler](https://github.com/Seldaek/monolog/blob/master/src/Monolog/Handler/WhatFailureGroupHandler.php)
Monolog Docs: [WhatFailureGroupHandler](https://github.com/Seldaek/monolog/blob/master/src/Monolog/Handler/WhatFailureGroupHandler.php)


#### BufferHandler
Expand Down Expand Up @@ -1713,7 +1717,7 @@ return [
],
];
```
Monolog Docs: [DeduplicationHandler](https://github.com/Seldaek/monolog/blob/master/src/Monolog/Handler/GroupHandler.php)
Monolog Docs: [GroupHandler](https://github.com/Seldaek/monolog/blob/master/src/Monolog/Handler/GroupHandler.php)


#### FilterHandler
Expand Down Expand Up @@ -1741,6 +1745,105 @@ return [
Monolog Docs: [FilterHandler](https://github.com/Seldaek/monolog/blob/master/src/Monolog/Handler/FilterHandler.php)


#### SamplingHandler
A sampled event stream can be useful for logging high frequency events in
a production environment where you only need an idea of what is happening
and are not concerned with capturing every occurrence. Since the decision to
handle or not handle a particular event is determined randomly, the
resulting sampled log is not guaranteed to contain 1/N of the events that
occurred in the application, but based on the Law of large numbers, it will
tend to be close to this ratio with a large number of attempts.

```php
<?php

return [
'monolog' => [
'handlers' => [
'myHandlerName' => [
'type' => 'sampling',
'options' => [
'handler' => 'my-handler', // Required: Registered Handler to wrap
'factor' => 5, // Required: Sample factor
],
],
],
],
];
```
Monolog Docs: [SamplingHandler](https://github.com/Seldaek/monolog/blob/master/src/Monolog/Handler/SamplingHandler.php)


#### NullHandler
Any record it can handle will be thrown away. This can be used
to put on top of an existing stack to override it temporarily.

```php
<?php

return [
'monolog' => [
'handlers' => [
'myHandlerName' => [
'type' => 'noop',
'options' => [
'level' => \Monolog\Logger::DEBUG, // Optional: The minimum logging level at which this handler will be triggered
],
],
],
],
];
```
Monolog Docs: [NullHandler](https://github.com/Seldaek/monolog/blob/master/src/Monolog/Handler/NullHandler.php)


#### PsrHandler
Can be used to forward log records to an existing PSR-3 logger

```php
<?php

return [
'monolog' => [
'handlers' => [
'myHandlerName' => [
'type' => 'psr',
'options' => [
'logger' => 'loggerService', // Required: Logger Service to wrap from the container
'level' => \Monolog\Logger::DEBUG, // Optional: The minimum logging level at which this handler will be triggered
'bubble' => true, // Optional: Whether the messages that are handled can bubble up the stack or not
],
],
],
],
];
```
Monolog Docs: [PsrHandler](https://github.com/Seldaek/monolog/blob/master/src/Monolog/Handler/PsrHandler.php)


#### TestHandler
Used for testing, it records everything that is sent to it and has accessors to read out the information.

```php
<?php

return [
'monolog' => [
'handlers' => [
'myHandlerName' => [
'type' => 'test',
'options' => [
'level' => \Monolog\Logger::DEBUG, // Optional: The minimum logging level at which this handler will be triggered
'bubble' => true, // Optional: Whether the messages that are handled can bubble up the stack or not
],
],
],
],
];
```
Monolog Docs: [TestHandler](https://github.com/Seldaek/monolog/blob/master/src/Monolog/Handler/TestHandler.php)


## Formatters

### LineFomatter
Expand Down
28 changes: 0 additions & 28 deletions src/ClientTrait.php

This file was deleted.

30 changes: 3 additions & 27 deletions src/Handler/AmqpHandlerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,17 @@

use Monolog\Handler\AmqpHandler;
use Monolog\Logger;
use PhpAmqpLib\Channel\AMQPChannel;
use WShafer\PSR11MonoLog\ContainerAwareInterface;
use WShafer\PSR11MonoLog\ContainerTrait;
use WShafer\PSR11MonoLog\Exception\MissingConfigException;
use WShafer\PSR11MonoLog\Exception\MissingServiceException;
use WShafer\PSR11MonoLog\FactoryInterface;
use WShafer\PSR11MonoLog\ServiceTrait;

class AmqpHandlerFactory implements FactoryInterface, ContainerAwareInterface
{
use ContainerTrait;
use ServiceTrait;

public function __invoke(array $options)
{
$exchange = $this->getAmqpExchange($options);
$exchange = $this->getService($options['exchange'] ?? null);
$exchangeName = (string) ($options['exchangeName'] ?? 'log');
$level = (int) ($options['level'] ?? Logger::DEBUG);
$bubble = (boolean) ($options['bubble'] ?? true);
Expand All @@ -30,25 +27,4 @@ public function __invoke(array $options)
$bubble
);
}

/**
* @param $options
* @return \AMQPExchange|AMQPChannel
*/
public function getAmqpExchange($options)
{
if (empty($options['exchange'])) {
throw new MissingConfigException(
'No exchange service name'
);
}

if (!$this->getContainer()->has($options['exchange'])) {
throw new MissingServiceException(
'No exchange service found for :'.$options['exchange']
);
}

return $this->getContainer()->get($options['exchange']);
}
}
6 changes: 3 additions & 3 deletions src/Handler/DoctrineCouchDBHandlerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@

use Monolog\Handler\DoctrineCouchDBHandler;
use Monolog\Logger;
use WShafer\PSR11MonoLog\ClientTrait;
use WShafer\PSR11MonoLog\ContainerAwareInterface;
use WShafer\PSR11MonoLog\FactoryInterface;
use WShafer\PSR11MonoLog\ServiceTrait;

class DoctrineCouchDBHandlerFactory implements FactoryInterface, ContainerAwareInterface
{
use ClientTrait;
use ServiceTrait;

public function __invoke(array $options)
{
$client = $this->getClient($options);
$client = $this->getService($options['client'] ?? null);
$level = (int) ($options['level'] ?? Logger::DEBUG);
$bubble = (boolean) ($options['bubble'] ?? true);

Expand Down
6 changes: 3 additions & 3 deletions src/Handler/DynamoDbHandlerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@

use Monolog\Handler\DynamoDbHandler;
use Monolog\Logger;
use WShafer\PSR11MonoLog\ClientTrait;
use WShafer\PSR11MonoLog\ContainerAwareInterface;
use WShafer\PSR11MonoLog\FactoryInterface;
use WShafer\PSR11MonoLog\ServiceTrait;

class DynamoDbHandlerFactory implements FactoryInterface, ContainerAwareInterface
{
use ClientTrait;
use ServiceTrait;

public function __invoke(array $options)
{
$client = $this->getClient($options);
$client = $this->getService($options['client'] ?? null);
$table = $options['table'] ?? null;
$level = (int) ($options['level'] ?? Logger::DEBUG);
$bubble = (boolean) ($options['bubble'] ?? true);
Expand Down
6 changes: 3 additions & 3 deletions src/Handler/ElasticSearchHandlerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@

use Monolog\Handler\ElasticSearchHandler;
use Monolog\Logger;
use WShafer\PSR11MonoLog\ClientTrait;
use WShafer\PSR11MonoLog\ContainerAwareInterface;
use WShafer\PSR11MonoLog\FactoryInterface;
use WShafer\PSR11MonoLog\ServiceTrait;

class ElasticSearchHandlerFactory implements FactoryInterface, ContainerAwareInterface
{
use ClientTrait;
use ServiceTrait;

public function __invoke(array $options)
{
$client = $this->getClient($options);
$client = $this->getService($options['client'] ?? null);
$index = (string) ($options['index'] ?? 'monolog');
$type = (string) ($options['type'] ?? 'record');
$ignoreError = (boolean) ($options['ignoreError'] ?? false);
Expand Down
27 changes: 3 additions & 24 deletions src/Handler/GelfHandlerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@
use Monolog\Handler\GelfHandler;
use Monolog\Logger;
use WShafer\PSR11MonoLog\ContainerAwareInterface;
use WShafer\PSR11MonoLog\ContainerTrait;
use WShafer\PSR11MonoLog\Exception\MissingConfigException;
use WShafer\PSR11MonoLog\Exception\MissingServiceException;
use WShafer\PSR11MonoLog\FactoryInterface;
use WShafer\PSR11MonoLog\ServiceTrait;

class GelfHandlerFactory implements FactoryInterface, ContainerAwareInterface
{
use ContainerTrait;
use ServiceTrait;

public function __invoke(array $options)
{
$publisher = $this->getPublisher($options);
$publisher = $this->getService($options['publisher'] ?? mull);
$level = (int) ($options['level'] ?? Logger::DEBUG);
$bubble = (boolean) ($options['bubble'] ?? true);

Expand All @@ -28,25 +28,4 @@ public function __invoke(array $options)
$bubble
);
}

/**
* @param $options
* @return PublisherInterface
*/
public function getPublisher($options)
{
if (empty($options['publisher'])) {
throw new MissingConfigException(
'No exchange service name'
);
}

if (!$this->getContainer()->has($options['publisher'])) {
throw new MissingServiceException(
'No publisher service found for :'.$options['publisher']
);
}

return $this->getContainer()->get($options['publisher']);
}
}
8 changes: 8 additions & 0 deletions src/Handler/HandlerMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ public function map(string $type)
return GroupHandlerFactory::class;
case 'filter':
return FilterHandlerFactory::class;
case 'sampling':
return SamplingHandlerFactory::class;
case 'null':
return NullHandlerFactory::class;
case 'psr':
return PsrHandlerFactory::class;
case 'test':
return TestHandlerFactory::class;
}

return null;
Expand Down
6 changes: 3 additions & 3 deletions src/Handler/MongoDBHandlerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@

use Monolog\Handler\MongoDBHandler;
use Monolog\Logger;
use WShafer\PSR11MonoLog\ClientTrait;
use WShafer\PSR11MonoLog\ContainerAwareInterface;
use WShafer\PSR11MonoLog\FactoryInterface;
use WShafer\PSR11MonoLog\ServiceTrait;

class MongoDBHandlerFactory implements FactoryInterface, ContainerAwareInterface
{
use ClientTrait;
use ServiceTrait;

public function __invoke(array $options)
{
$client = $this->getClient($options);
$client = $this->getService($options['client'] ?? null);
$database = (string) ($options['database'] ?? '');
$collection = (string) ($options['collection'] ?? '');
$level = (int) ($options['level'] ?? Logger::DEBUG);
Expand Down
18 changes: 18 additions & 0 deletions src/Handler/NullHandlerFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
declare(strict_types=1);

namespace WShafer\PSR11MonoLog\Handler;

use Monolog\Handler\NullHandler;
use Monolog\Logger;
use WShafer\PSR11MonoLog\FactoryInterface;

class NullHandlerFactory implements FactoryInterface
{
public function __invoke(array $options)
{
$level = (int) ($options['level'] ?? Logger::DEBUG);

return new NullHandler($level);
}
}
Loading

0 comments on commit 6057b3b

Please sign in to comment.