This commit is contained in:
Paolo A
2024-08-13 13:44:16 +00:00
parent 1bbb23088d
commit e796d76612
4001 changed files with 30101 additions and 40075 deletions

View File

@@ -7,6 +7,7 @@ use Illuminate\Support\Str;
use InvalidArgumentException;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\ErrorLogHandler;
use Monolog\Handler\FingersCrossedHandler;
use Monolog\Handler\FormattableHandlerInterface;
use Monolog\Handler\HandlerInterface;
use Monolog\Handler\RotatingFileHandler;
@@ -61,6 +62,19 @@ class LogManager implements LoggerInterface
$this->app = $app;
}
/**
* Build an on-demand log channel.
*
* @param array $config
* @return \Psr\Log\LoggerInterface
*/
public function build(array $config)
{
unset($this->channels['ondemand']);
return $this->get('ondemand', $config);
}
/**
* Create a new, on-demand aggregate logger instance.
*
@@ -95,27 +109,20 @@ class LogManager implements LoggerInterface
*/
public function driver($driver = null)
{
return $this->get($driver ?? $this->getDefaultDriver());
}
/**
* @return array
*/
public function getChannels()
{
return $this->channels;
return $this->get($this->parseDriver($driver));
}
/**
* Attempt to get the log from the local cache.
*
* @param string $name
* @param array|null $config
* @return \Psr\Log\LoggerInterface
*/
protected function get($name)
protected function get($name, ?array $config = null)
{
try {
return $this->channels[$name] ?? with($this->resolve($name), function ($logger) use ($name) {
return $this->channels[$name] ?? with($this->resolve($name, $config), function ($logger) use ($name) {
return $this->channels[$name] = $this->tap($name, new Logger($logger, $this->app['events']));
});
} catch (Throwable $e) {
@@ -180,13 +187,14 @@ class LogManager implements LoggerInterface
* Resolve the given log instance by name.
*
* @param string $name
* @param array|null $config
* @return \Psr\Log\LoggerInterface
*
* @throws \InvalidArgumentException
*/
protected function resolve($name)
protected function resolve($name, ?array $config = null)
{
$config = $this->configurationFor($name);
$config = $config ?? $this->configurationFor($name);
if (is_null($config)) {
throw new InvalidArgumentException("Log [{$name}] is not defined.");
@@ -237,15 +245,27 @@ class LogManager implements LoggerInterface
*/
protected function createStackDriver(array $config)
{
if (is_string($config['channels'])) {
$config['channels'] = explode(',', $config['channels']);
}
$handlers = collect($config['channels'])->flatMap(function ($channel) {
return $this->channel($channel)->getHandlers();
return $channel instanceof LoggerInterface
? $channel->getHandlers()
: $this->channel($channel)->getHandlers();
})->all();
$processors = collect($config['channels'])->flatMap(function ($channel) {
return $channel instanceof LoggerInterface
? $channel->getProcessors()
: $this->channel($channel)->getProcessors();
})->all();
if ($config['ignore_exceptions'] ?? false) {
$handlers = [new WhatFailureGroupHandler($handlers)];
}
return new Monolog($this->parseChannel($config), $handlers);
return new Monolog($this->parseChannel($config), $handlers, $processors);
}
/**
@@ -389,17 +409,17 @@ class LogManager implements LoggerInterface
*/
protected function prepareHandler(HandlerInterface $handler, array $config = [])
{
$isHandlerFormattable = false;
if (Monolog::API === 1) {
$isHandlerFormattable = true;
} elseif (Monolog::API === 2 && $handler instanceof FormattableHandlerInterface) {
$isHandlerFormattable = true;
if (isset($config['action_level'])) {
$handler = new FingersCrossedHandler($handler, $this->actionLevel($config));
}
if ($isHandlerFormattable && ! isset($config['formatter'])) {
if (Monolog::API !== 1 && (Monolog::API !== 2 || ! $handler instanceof FormattableHandlerInterface)) {
return $handler;
}
if (! isset($config['formatter'])) {
$handler->setFormatter($this->formatter());
} elseif ($isHandlerFormattable && $config['formatter'] !== 'default') {
} elseif ($config['formatter'] !== 'default') {
$handler->setFormatter($this->app->make($config['formatter'], $config['formatter_with'] ?? []));
}
@@ -442,7 +462,7 @@ class LogManager implements LoggerInterface
/**
* Get the default log driver name.
*
* @return string
* @return string|null
*/
public function getDefaultDriver()
{
@@ -482,13 +502,40 @@ class LogManager implements LoggerInterface
*/
public function forgetChannel($driver = null)
{
$driver = $driver ?? $this->getDefaultDriver();
$driver = $this->parseDriver($driver);
if (isset($this->channels[$driver])) {
unset($this->channels[$driver]);
}
}
/**
* Parse the driver name.
*
* @param string|null $driver
* @return string|null
*/
protected function parseDriver($driver)
{
$driver = $driver ?? $this->getDefaultDriver();
if ($this->app->runningUnitTests()) {
$driver = $driver ?? 'null';
}
return $driver;
}
/**
* Get all of the resolved log channels.
*
* @return array
*/
public function getChannels()
{
return $this->channels;
}
/**
* System is unusable.
*