You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
83 lines
2.4 KiB
83 lines
2.4 KiB
<?php
|
|
|
|
namespace App\Utilities\Logger;
|
|
|
|
use DateTimeZone;
|
|
use InvalidArgumentException;
|
|
use Monolog\Logger as Monolog;
|
|
use Monolog\Handler\HandlerInterface;
|
|
use App\HiLib\Logger\LogServiceRecordProcessor;
|
|
|
|
class CreateCustomLogger
|
|
{
|
|
protected $levels = [
|
|
'debug' => Monolog::DEBUG,
|
|
'info' => Monolog::INFO,
|
|
'notice' => Monolog::NOTICE,
|
|
'warning' => Monolog::WARNING,
|
|
'error' => Monolog::ERROR,
|
|
'critical' => Monolog::CRITICAL,
|
|
'alert' => Monolog::ALERT,
|
|
'emergency' => Monolog::EMERGENCY,
|
|
];
|
|
|
|
public function __invoke(array $config)
|
|
{
|
|
if (!is_a($config['handler'], HandlerInterface::class, true)) {
|
|
throw new InvalidArgumentException(
|
|
$config['handler'] . ' must be an instance of ' . HandlerInterface::class
|
|
);
|
|
}
|
|
|
|
$with = array_merge(
|
|
['level' => $this->level($config)],
|
|
$config['with'] ?? [],
|
|
$config['handler_with'] ?? []
|
|
);
|
|
|
|
$handlers = [
|
|
$this->prepareHandler(app()->make($config['handler'], $with), $config)
|
|
];
|
|
|
|
$processors = [
|
|
new LogServiceRecordProcessor,
|
|
];
|
|
|
|
return new Monolog('custom', $handlers, $processors, new DateTimeZone('Asia/Tehran'));
|
|
}
|
|
|
|
protected function getFallbackChannelName()
|
|
{
|
|
return app()->bound('env') ? app()->environment() : 'production';
|
|
}
|
|
|
|
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 ($isHandlerFormattable && !isset($config['formatter'])) {
|
|
$handler->setFormatter($this->formatter());
|
|
} elseif ($isHandlerFormattable && $config['formatter'] !== 'default') {
|
|
$handler->setFormatter(app()->make($config['formatter'], $config['formatter_with'] ?? []));
|
|
}
|
|
|
|
return $handler;
|
|
}
|
|
|
|
protected function level(array $config)
|
|
{
|
|
$level = $config['level'] ?? 'debug';
|
|
|
|
if (isset($this->levels[$level])) {
|
|
return $this->levels[$level];
|
|
}
|
|
|
|
throw new InvalidArgumentException('Invalid log level.');
|
|
}
|
|
}
|