Skip to content

Commit

Permalink
LogManager - If setting alternate channel, then it should use a clone
Browse files Browse the repository at this point in the history
Consider this example:

```php
class Foo {
  private $log;
  public function __construct() { $this->log = Civi::log('foo'); }
  public function fooify() { $this->info("Hello from Foo"); }
}

class Bar {
  private $log;
  public function __construct() { $this->log = Civi::log('bar'); }
  public function barbecueTofu() { $this->info("Hello from Bar"); }
}

$foo = new Foo();
$bar = new Bar();
$foo->fooify();
$bar->barbecueTofu();
```

If `Foo` and `Bar` reference the same shared `CRM_Core_Error_Log $log`, then
one or the other will route to the wrong place. Instead, they should have
separate instances.
  • Loading branch information
totten committed Apr 27, 2021
1 parent aaa5d21 commit feb6ed8
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions Civi/Core/LogManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,17 @@ public function getLog($channel = 'default') {
if (!isset($this->channels[$channel])) {
$c = \Civi::container();
$svc = "log." . $channel;
$this->channels[$channel] = $c->has($svc) ? $c->get($svc) : $c->get(self::DEFAULT_LOGGER);
if ($channel !== 'default' && method_exists($this->channels[$channel], 'setChannel')) {
$this->channels[$channel]->setChannel($channel);
if ($c->has($svc)) {
$log = $c->get($svc);
}
else {
$log = $c->get(self::DEFAULT_LOGGER);;
if (is_callable([$log, 'setChannel'])) {
$log = clone $log;
$log->setChannel($channel);
}
}
$this->channels[$channel] = $log;
}
return $this->channels[$channel];
}
Expand Down

0 comments on commit feb6ed8

Please sign in to comment.