add logging config (#127)

This commit is contained in:
sunxyw
2022-05-16 23:28:02 +08:00
committed by GitHub
parent 87bbffefc3
commit 5cbeee6e86
3 changed files with 34 additions and 1 deletions

21
config/logging.php Normal file
View File

@@ -0,0 +1,21 @@
<?php
declare(strict_types=1);
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use ZM\Logger\ConsoleLogger;
return [
'level' => 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;
},
];

View File

@@ -9,6 +9,7 @@ namespace ZM\Event\SwooleEvent;
use Error; use Error;
use Exception; use Exception;
use PDO; use PDO;
use Psr\Log\LoggerInterface;
use ReflectionException; use ReflectionException;
use Swoole\Coroutine; use Swoole\Coroutine;
use Swoole\Database\PDOConfig; use Swoole\Database\PDOConfig;
@@ -316,6 +317,8 @@ class OnWorkerStart implements SwooleEvent
$container->instance('worker_id', $server->worker_id); $container->instance('worker_id', $server->worker_id);
$container->singleton(AdapterInterface::class, OneBot11Adapter::class); $container->singleton(AdapterInterface::class, OneBot11Adapter::class);
$container->singleton(LoggerInterface::class, ZMConfig::get('logging.logger'));
} }
private function gatherWorkerStartStatus() private function gatherWorkerStartStatus()

View File

@@ -2,6 +2,7 @@
declare(strict_types=1); declare(strict_types=1);
use Psr\Log\LoggerInterface;
use Swoole\Atomic; use Swoole\Atomic;
use Swoole\Coroutine; use Swoole\Coroutine;
use Swoole\Coroutine as Co; use Swoole\Coroutine as Co;
@@ -589,7 +590,7 @@ function zm_error($obj): void
* *
* @return mixed * @return mixed
*/ */
function zm_config(string $name, ?string $key = null) function zm_config(string $name, string $key = '')
{ {
return ZMConfig::get($name, $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); return !empty($array) && array_keys($array) !== range(0, count($array) - 1);
} }
/**
* 返回 Logger 实例
*/
function logger(): LoggerInterface
{
return resolve(LoggerInterface::class);
}
/** /**
* 以下为废弃的函数,将于未来移除 * 以下为废弃的函数,将于未来移除
*/ */