diff --git a/config/logging.php b/config/logging.php new file mode 100644 index 00000000..5518c548 --- /dev/null +++ b/config/logging.php @@ -0,0 +1,21 @@ + LogLevel::DEBUG, + 'logger' => static function (): LoggerInterface { + $worker_id = app('worker_id'); + + $logger = new ConsoleLogger(zm_config('logging.level')); + $logger::$format = "[%date%] [%level%] [#{$worker_id}] %body%"; + $logger::$date_format = 'Y-m-d H:i:s'; + // 如果你喜欢旧版的日志格式,请取消下行注释 +// $logger::$date_format = 'm-d H:i:s'; + return $logger; + }, +]; diff --git a/src/ZM/Event/SwooleEvent/OnWorkerStart.php b/src/ZM/Event/SwooleEvent/OnWorkerStart.php index fb725c7d..30a00597 100644 --- a/src/ZM/Event/SwooleEvent/OnWorkerStart.php +++ b/src/ZM/Event/SwooleEvent/OnWorkerStart.php @@ -9,6 +9,7 @@ namespace ZM\Event\SwooleEvent; use Error; use Exception; use PDO; +use Psr\Log\LoggerInterface; use ReflectionException; use Swoole\Coroutine; use Swoole\Database\PDOConfig; @@ -316,6 +317,8 @@ class OnWorkerStart implements SwooleEvent $container->instance('worker_id', $server->worker_id); $container->singleton(AdapterInterface::class, OneBot11Adapter::class); + + $container->singleton(LoggerInterface::class, ZMConfig::get('logging.logger')); } private function gatherWorkerStartStatus() diff --git a/src/ZM/global_functions.php b/src/ZM/global_functions.php index ae23087f..a575d7a7 100644 --- a/src/ZM/global_functions.php +++ b/src/ZM/global_functions.php @@ -2,6 +2,7 @@ declare(strict_types=1); +use Psr\Log\LoggerInterface; use Swoole\Atomic; use Swoole\Coroutine; use Swoole\Coroutine as Co; @@ -589,7 +590,7 @@ function zm_error($obj): void * * @return mixed */ -function zm_config(string $name, ?string $key = null) +function zm_config(string $name, string $key = '') { return ZMConfig::get($name, $key); } @@ -781,6 +782,14 @@ function is_assoc_array(array $array): bool return !empty($array) && array_keys($array) !== range(0, count($array) - 1); } +/** + * 返回 Logger 实例 + */ +function logger(): LoggerInterface +{ + return resolve(LoggerInterface::class); +} + /** * 以下为废弃的函数,将于未来移除 */