Skip to content
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

LogManager returns new instance of Logger at each call instead of returning the same instance #23117

Closed
CyrilMazur opened this issue Feb 11, 2018 · 1 comment

Comments

@CyrilMazur
Copy link
Contributor

  • Laravel Version: 5.6.3
  • PHP Version: 7.1.13
  • Database Driver & Version: n/a

Description:

Illuminate\Log\LogManager returns a new Logger instance every time instead of making one instance and returning the same instance in subsequent calls.

According to the source (see

/**
* Attempt to get the log from the local cache.
*
* @param string $name
* @return \Psr\Log\LoggerInterface
*/
protected function get($name)
{
try {
return $this->channels[$name] ?? with($this->resolve($name), function ($logger) use ($name) {
return $this->tap($name, new Logger($logger, $this->app['events']));
});
} catch (Throwable $e) {
return tap($this->createEmergencyLogger(), function ($logger) use ($e) {
$logger->emergency('Unable to create configured logger. Using emergency logger.', [
'exception' => $e,
]);
});
}
}
), it is not intended to return a new instance every time, but instead get an existing instance from the "local cache", which is the $channels property. From my tests, the $channels property remains always empty, no matter how many times I request the same log channel.

Steps To Reproduce:

dd(
    app('log')->channel('stack')->getLogger(),
    app('log')->channel('stack')->getLogger(), // different instance from above
    app('log')->channel('stack')->getLogger() // yet another instance
);
@CyrilMazur
Copy link
Contributor Author

I submitted a PR to show how I'd fix this: #23118

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant