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.
42 lines
1.2 KiB
42 lines
1.2 KiB
3 weeks ago
|
<?php
|
||
|
|
||
|
declare(strict_types=1);
|
||
|
|
||
|
namespace App\Utils;
|
||
|
|
||
|
use Monolog\Formatter\LineFormatter;
|
||
|
use Monolog\Handler\RotatingFileHandler;
|
||
|
use Monolog\Level;
|
||
|
use Monolog\Logger;
|
||
|
|
||
|
class GeneralDailyLogger
|
||
|
{
|
||
|
/**
|
||
|
* 创建一个通用的每日的日志通道
|
||
|
*
|
||
|
* @param array<string, int|string|level> $config
|
||
|
*
|
||
|
* @return Logger
|
||
|
*/
|
||
|
public function __invoke(array $config): Logger {
|
||
|
$service_type = is_string($config['service_type']) ? $config['service_type'] : 'default';
|
||
|
$level = $config['level'] instanceof Level ? $config['level'] : Level::Debug;
|
||
|
$path = sprintf(storage_path(). "/logs/%s/%s.log", $service_type, $level->toPsrLogLevel());
|
||
|
$max_file = is_numeric($config['max_files']) ? (int) $config['max_files'] : 0;
|
||
|
|
||
|
// Set Handler
|
||
|
$handler = new RotatingFileHandler($path, $max_file, $level);
|
||
|
$handler->setFormatter(
|
||
|
new LineFormatter(
|
||
|
"[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n\n",
|
||
|
'Y-m-d H:i:s.u',
|
||
|
true,
|
||
|
true,
|
||
|
true
|
||
|
)
|
||
|
);
|
||
|
|
||
|
return new Logger($service_type, [$handler]);
|
||
|
}
|
||
|
}
|