Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Merge branch 'feature/83' into develop
Browse files Browse the repository at this point in the history
Close #83
  • Loading branch information
michalbundyra committed Aug 23, 2019
2 parents f436c68 + a6736ce commit 7eaa3c3
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ All notable changes to this project will be documented in this file, in reverse

- [#96](https://github.com/zendframework/zend-log/pull/96) adds support for PHP 7.3.

- [#83](https://github.com/zendframework/zend-log/pull/83) adds
ability to define custom ignored namespaces in addition to default `Zend\Log`
in `Backtrace` processor.

### Changed

- Nothing.
Expand Down
18 changes: 18 additions & 0 deletions docs/book/processors.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,24 @@ $event = [
];
```

By default, classes under the `Zend\Log` namespace are excluded from the backtrace
that is logged so that the actual application code triggering the log event can be
identified. You can add your own excluded namespaces to the backtrace processor by
passing options into the constructor (note the required escaping of the `\`):

```php
$processor = new Zend\Log\Processor\Backtrace(['ignoredNamespaces' => ['Foo\\Log']]);
$logger->addProcessor($processor);
```

Alternatively, if not separately instantiating the processor, these options can be
passed as the third argument to the logger's `addProcessor()` function:

```php
// Assuming the default processor priority of 1
$logger->addProcessor('backtrace', 1, ['ignoredNamespaces' => ['Foo\\Log']]);
```

### PsrPlaceholder

`Zend\Log\Processor\PsrPlaceholder` replaces [PSR-3](http://www.php-fig.org/psr/psr-3/)-formatted
Expand Down
48 changes: 44 additions & 4 deletions src/Processor/Backtrace.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,23 @@ class Backtrace implements ProcessorInterface
protected $traceLimit = 10;

/**
* Classes within this namespace in the stack are ignored
* @var string
* Classes within these namespaces in the stack are ignored
* @var array
*/
protected $ignoredNamespace = 'Zend\\Log';
protected $ignoredNamespaces = ['Zend\\Log'];

/**
* Set options for a backtrace processor. Accepted options are:
* - ignoredNamespaces: array of namespaces to be excluded from the logged backtrace
*
* @param array|null $options
*/
public function __construct(array $options = null)
{
if (! empty($options['ignoredNamespaces'])) {
$this->ignoredNamespaces = array_merge($this->ignoredNamespaces, (array) $options['ignoredNamespaces']);
}
}

/**
* Adds the origin of the log() call to the event extras
Expand All @@ -38,7 +51,7 @@ public function process(array $event)

$i = 0;
while (isset($trace[$i]['class'])
&& false !== strpos($trace[$i]['class'], $this->ignoredNamespace)
&& $this->shouldIgnoreFrame($trace[$i]['class'])
) {
$i++;
}
Expand All @@ -59,6 +72,16 @@ public function process(array $event)
return $event;
}

/**
* Get all ignored namespaces
*
* @return array
*/
public function getIgnoredNamespaces()
{
return $this->ignoredNamespaces;
}

/**
* Provide backtrace as slim as possible
*
Expand All @@ -68,4 +91,21 @@ protected function getBacktrace()
{
return debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, $this->traceLimit);
}

/**
* Determine whether the current frame in the backtrace should be ignored based on the class name
*
* @param string $class
* @return bool
*/
protected function shouldIgnoreFrame($class)
{
foreach ($this->ignoredNamespaces as $ignoredNamespace) {
if (false !== strpos($class, $ignoredNamespace)) {
return true;
}
}

return false;
}
}
19 changes: 16 additions & 3 deletions test/Processor/BacktraceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,15 @@

class BacktraceTest extends TestCase
{
public function testProcess()
private $processor;

protected function setUp()
{
$processor = new Backtrace();
$this->processor = new Backtrace();
}

public function testProcess()
{
$event = [
'timestamp' => '',
'priority' => 1,
Expand All @@ -26,11 +31,19 @@ public function testProcess()
'extra' => []
];

$event = $processor->process($event);
$event = $this->processor->process($event);

$this->assertArrayHasKey('file', $event['extra']);
$this->assertArrayHasKey('line', $event['extra']);
$this->assertArrayHasKey('class', $event['extra']);
$this->assertArrayHasKey('function', $event['extra']);
}

public function testConstructorAcceptsOptionalIgnoredNamespaces()
{
$this->assertSame(['Zend\\Log'], $this->processor->getIgnoredNamespaces());

$processor = new Backtrace(['ignoredNamespaces' => ['Foo\\Bar']]);
$this->assertSame(['Zend\\Log', 'Foo\\Bar'], $processor->getIgnoredNamespaces());
}
}

0 comments on commit 7eaa3c3

Please sign in to comment.