mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-07-22 16:15:34 +08:00
replace console with logger
This commit is contained in:
@@ -19,7 +19,7 @@ class OnBeforeReload implements SwooleEvent
|
||||
{
|
||||
public function onCall(Server $server)
|
||||
{
|
||||
Console::info(Console::setColor('Reloading server...', 'gold'));
|
||||
logger()->info(Console::setColor('Reloading server...', 'gold'));
|
||||
for ($i = 0; $i < ZM_WORKER_NUM; ++$i) {
|
||||
Process::kill(zm_atomic('_#worker_' . $i)->get(), SIGUSR1);
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ class OnClose implements SwooleEvent
|
||||
public function onCall($server, $fd)
|
||||
{
|
||||
unset(Context::$context[Coroutine::getCid()]);
|
||||
Console::debug('Calling Swoole "close" event from fd=' . $fd);
|
||||
logger()->debug('Calling Swoole "close" event from fd=' . $fd);
|
||||
$conn = ManagerGM::get($fd);
|
||||
if ($conn === null) {
|
||||
return;
|
||||
|
||||
@@ -42,7 +42,7 @@ class OnManagerStart implements SwooleEvent
|
||||
|
||||
public function onCall(Server $server)
|
||||
{
|
||||
Console::debug('Calling onManagerStart event(1)');
|
||||
logger()->debug('Calling onManagerStart event(1)');
|
||||
if (!Framework::$argv['disable-safe-exit']) {
|
||||
SignalListener::signalManager();
|
||||
}
|
||||
@@ -54,17 +54,17 @@ class OnManagerStart implements SwooleEvent
|
||||
});
|
||||
if (Framework::$argv['watch']) {
|
||||
if (extension_loaded('inotify')) {
|
||||
Console::info('Enabled File watcher, framework will reload automatically.');
|
||||
logger()->info('Enabled File watcher, framework will reload automatically.');
|
||||
$fd = inotify_init();
|
||||
$this->addWatcher(DataProvider::getSourceRootDir() . '/src', $fd);
|
||||
Event::add($fd, function () use ($fd) {
|
||||
$r = inotify_read($fd);
|
||||
Console::verbose('File updated: ' . $r[0]['name']);
|
||||
logger()->debug('File updated: ' . $r[0]['name']);
|
||||
ZMUtil::reload();
|
||||
});
|
||||
Framework::$argv['polling-watch'] = false; // 如果开启了inotify则关闭轮询热更新
|
||||
} else {
|
||||
Console::warning(zm_internal_errcode('E00024') . '你还没有安装或启用 inotify 扩展,将默认使用轮询检测模式开启热更新!');
|
||||
logger()->warning(zm_internal_errcode('E00024') . '你还没有安装或启用 inotify 扩展,将默认使用轮询检测模式开启热更新!');
|
||||
Framework::$argv['polling-watch'] = true;
|
||||
}
|
||||
}
|
||||
@@ -84,7 +84,7 @@ class OnManagerStart implements SwooleEvent
|
||||
});
|
||||
}
|
||||
if (Framework::$argv['interact']) {
|
||||
Console::info('Interact mode');
|
||||
logger()->info('Interact mode');
|
||||
ZMBuf::$terminal = $r = STDIN;
|
||||
Event::add($r, function () use ($r) {
|
||||
$fget = fgets($r);
|
||||
@@ -116,7 +116,7 @@ class OnManagerStart implements SwooleEvent
|
||||
});
|
||||
$dispatcher->dispatchEvents($server);
|
||||
*/
|
||||
Console::verbose('进程 Manager 已启动');
|
||||
logger()->debug('进程 Manager 已启动');
|
||||
}
|
||||
|
||||
private function addWatcher($maindir, $fd)
|
||||
@@ -127,7 +127,7 @@ class OnManagerStart implements SwooleEvent
|
||||
}
|
||||
foreach ($dir as $subdir) {
|
||||
if (is_dir($maindir . '/' . $subdir)) {
|
||||
Console::debug('添加监听目录:' . $maindir . '/' . $subdir);
|
||||
logger()->debug('添加监听目录:' . $maindir . '/' . $subdir);
|
||||
inotify_add_watch($fd, $maindir . '/' . $subdir, IN_ATTRIB | IN_ISDIR);
|
||||
$this->addWatcher($maindir . '/' . $subdir, $fd);
|
||||
}
|
||||
|
||||
@@ -6,7 +6,6 @@ namespace ZM\Event\SwooleEvent;
|
||||
|
||||
use Swoole\Process;
|
||||
use ZM\Annotation\Swoole\SwooleHandler;
|
||||
use ZM\Console\Console;
|
||||
use ZM\Event\SwooleEvent;
|
||||
use ZM\Utils\Manager\ProcessManager;
|
||||
|
||||
@@ -23,7 +22,7 @@ class OnManagerStop implements SwooleEvent
|
||||
Process::kill($v->pid, SIGTERM);
|
||||
}
|
||||
}
|
||||
Console::verbose('进程 Manager 已停止!');
|
||||
logger()->debug('进程 Manager 已停止!');
|
||||
ProcessManager::removeProcessState(ZM_PROCESS_MANAGER);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ class OnMessage implements SwooleEvent
|
||||
{
|
||||
public function onCall($server, Frame $frame)
|
||||
{
|
||||
Console::debug('Calling Swoole "message" from fd=' . $frame->fd . ': ' . TermColor::ITALIC . $frame->data . TermColor::RESET);
|
||||
logger()->debug('Calling Swoole "message" from fd=' . $frame->fd . ': ' . TermColor::ITALIC . $frame->data . TermColor::RESET);
|
||||
unset(Context::$context[Coroutine::getCid()]);
|
||||
$conn = ManagerGM::get($frame->fd);
|
||||
set_coroutine_params(['server' => $server, 'frame' => $frame, 'connection' => $conn]);
|
||||
|
||||
@@ -29,7 +29,7 @@ class OnOpen implements SwooleEvent
|
||||
{
|
||||
public function onCall($server, Request $request)
|
||||
{
|
||||
Console::debug('Calling Swoole "open" event from fd=' . $request->fd);
|
||||
logger()->debug('Calling Swoole "open" event from fd=' . $request->fd);
|
||||
unset(Context::$context[Coroutine::getCid()]);
|
||||
|
||||
$type = strtolower($request->header['x-client-role'] ?? $request->get['type'] ?? '');
|
||||
@@ -38,13 +38,13 @@ class OnOpen implements SwooleEvent
|
||||
if ($token instanceof Closure) {
|
||||
if (!$token($access_token)) {
|
||||
$server->close($request->fd);
|
||||
Console::warning(zm_internal_errcode('E00018') . 'Unauthorized access_token: ' . $access_token);
|
||||
logger()->warning(zm_internal_errcode('E00018') . 'Unauthorized access_token: ' . $access_token);
|
||||
return;
|
||||
}
|
||||
} elseif (is_string($token)) {
|
||||
if ($access_token !== $token && $token !== '') {
|
||||
$server->close($request->fd);
|
||||
Console::warning(zm_internal_errcode('E00019') . 'Unauthorized access_token: ' . $access_token);
|
||||
logger()->warning(zm_internal_errcode('E00019') . 'Unauthorized access_token: ' . $access_token);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,7 +35,7 @@ class OnRequest implements SwooleEvent
|
||||
$response->setHeader($k, $v);
|
||||
}
|
||||
unset(Context::$context[Coroutine::getCid()]);
|
||||
Console::debug('Calling Swoole "request" event from fd=' . $request->fd);
|
||||
logger()->debug('Calling Swoole "request" event from fd=' . $request->fd);
|
||||
set_coroutine_params(['request' => $request, 'response' => $response]);
|
||||
|
||||
resolve(ContainerServicesProvider::class)->registerServices('request');
|
||||
@@ -83,7 +83,7 @@ class OnRequest implements SwooleEvent
|
||||
// do nothing
|
||||
} catch (Exception $e) {
|
||||
$response->status(500);
|
||||
Console::info(
|
||||
logger()->info(
|
||||
$request->server['remote_addr'] . ':' . $request->server['remote_port'] .
|
||||
' [' . $response->getStatusCode() . '] ' . $request->server['request_uri']
|
||||
);
|
||||
@@ -98,7 +98,7 @@ class OnRequest implements SwooleEvent
|
||||
Console::log($e->getTraceAsString(), 'gray');
|
||||
} catch (Error $e) {
|
||||
$response->status(500);
|
||||
Console::info(
|
||||
logger()->info(
|
||||
$request->server['remote_addr'] . ':' . $request->server['remote_port'] .
|
||||
' [' . $response->getStatusCode() . '] ' . $request->server['request_uri']
|
||||
);
|
||||
|
||||
@@ -8,7 +8,6 @@ namespace ZM\Event\SwooleEvent;
|
||||
|
||||
use Swoole\Server;
|
||||
use ZM\Annotation\Swoole\SwooleHandler;
|
||||
use ZM\Console\Console;
|
||||
use ZM\Event\SwooleEvent;
|
||||
use ZM\Utils\DataProvider;
|
||||
use ZM\Utils\Manager\ProcessManager;
|
||||
@@ -21,7 +20,7 @@ class OnShutdown implements SwooleEvent
|
||||
{
|
||||
public function onCall(Server $server)
|
||||
{
|
||||
Console::verbose('正在关闭 Master 进程,pid=' . posix_getpid());
|
||||
logger()->debug('正在关闭 Master 进程,pid=' . posix_getpid());
|
||||
ProcessManager::removeProcessState(ZM_PROCESS_MASTER);
|
||||
if (DataProvider::scanDirFiles(_zm_pid_dir()) == []) {
|
||||
rmdir(_zm_pid_dir());
|
||||
|
||||
@@ -7,7 +7,6 @@ namespace ZM\Event\SwooleEvent;
|
||||
use Swoole\Server;
|
||||
use ZM\Annotation\Swoole\SwooleHandler;
|
||||
use ZM\Config\ZMConfig;
|
||||
use ZM\Console\Console;
|
||||
use ZM\Event\SwooleEvent;
|
||||
use ZM\Framework;
|
||||
use ZM\Utils\Manager\ProcessManager;
|
||||
@@ -21,7 +20,7 @@ class OnStart implements SwooleEvent
|
||||
{
|
||||
public function onCall(Server $server)
|
||||
{
|
||||
Console::debug('Calling onStart event(1)');
|
||||
logger()->debug('Calling onStart event(1)');
|
||||
if (!Framework::$argv['disable-safe-exit']) {
|
||||
SignalListener::signalMaster($server);
|
||||
}
|
||||
|
||||
@@ -10,7 +10,6 @@ use Swoole\Coroutine;
|
||||
use Swoole\Server;
|
||||
use Swoole\Timer;
|
||||
use ZM\Annotation\Swoole\SwooleHandler;
|
||||
use ZM\Console\Console;
|
||||
use ZM\Event\SwooleEvent;
|
||||
use ZM\Store\LightCacheInside;
|
||||
|
||||
@@ -28,6 +27,6 @@ class OnWorkerExit implements SwooleEvent
|
||||
Coroutine::resume($v['coroutine']);
|
||||
}
|
||||
}
|
||||
Console::verbose('正在结束 Worker #' . $worker_id . ',进程内可能有事务在运行...');
|
||||
logger()->debug('正在结束 Worker #' . $worker_id . ',进程内可能有事务在运行...');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,7 +50,7 @@ class OnWorkerStart implements SwooleEvent
|
||||
{
|
||||
public function onCall(Server $server, int $worker_id)
|
||||
{
|
||||
Console::debug('Calling onWorkerStart event(1)');
|
||||
logger()->debug('Calling onWorkerStart event(1)');
|
||||
if (!Framework::$argv['disable-safe-exit']) {
|
||||
SignalListener::signalWorker($server, $worker_id);
|
||||
}
|
||||
@@ -79,7 +79,7 @@ class OnWorkerStart implements SwooleEvent
|
||||
$server->shutdown();
|
||||
});
|
||||
|
||||
Console::verbose("Worker #{$server->worker_id} starting");
|
||||
logger()->debug("Worker #{$server->worker_id} starting");
|
||||
// ZMBuf::resetCache(); //清空变量缓存
|
||||
// ZMBuf::set("wait_start", []); //添加队列,在workerStart运行完成前先让其他协程等待执行
|
||||
|
||||
@@ -107,11 +107,11 @@ class OnWorkerStart implements SwooleEvent
|
||||
});
|
||||
$dispatcher->dispatchEvents($server, $worker_id);
|
||||
if ($dispatcher->status === EventDispatcher::STATUS_NORMAL) {
|
||||
Console::debug('@OnStart 执行完毕');
|
||||
logger()->debug('@OnStart 执行完毕');
|
||||
} else {
|
||||
Console::warning('@OnStart 执行异常!');
|
||||
logger()->warning('@OnStart 执行异常!');
|
||||
}
|
||||
Console::verbose('Worker #' . $worker_id . ' started');
|
||||
logger()->debug('Worker #' . $worker_id . ' started');
|
||||
$this->gatherWorkerStartStatus();
|
||||
} catch (Exception $e) {
|
||||
if ($e->getMessage() === 'swoole exit') {
|
||||
@@ -134,7 +134,7 @@ class OnWorkerStart implements SwooleEvent
|
||||
ProcessManager::saveProcessState(ZM_PROCESS_TASKWORKER, $server->worker_pid, ['worker_id' => $worker_id]);
|
||||
try {
|
||||
$this->loadAnnotations();
|
||||
Console::verbose('TaskWorker #' . $server->worker_id . ' started');
|
||||
logger()->debug('TaskWorker #' . $server->worker_id . ' started');
|
||||
} catch (Exception $e) {
|
||||
Console::error('TaskWorker #' . $server->worker_id . ' 加载出错!停止服务!');
|
||||
Console::error(zm_internal_errcode('E00030') . $e->getMessage() . "\n" . $e->getTraceAsString());
|
||||
@@ -159,7 +159,7 @@ class OnWorkerStart implements SwooleEvent
|
||||
goto skip;
|
||||
}
|
||||
// 加载各个模块的注解类,以及反射
|
||||
Console::debug('Mapping annotations');
|
||||
logger()->debug('Mapping annotations');
|
||||
$parser = new AnnotationParser();
|
||||
$composer = json_decode(file_get_contents(DataProvider::getSourceRootDir() . '/composer.json'), true);
|
||||
$merge = array_merge($composer['autoload']['psr-4'] ?? [], $composer['autoload-dev']['psr-4'] ?? []);
|
||||
@@ -182,7 +182,7 @@ class OnWorkerStart implements SwooleEvent
|
||||
$list = ModuleManager::getPackedModules();
|
||||
foreach ($list as $k => $v) {
|
||||
if (\server()->worker_id === 0) {
|
||||
Console::info('Loading packed module: ' . $k);
|
||||
logger()->info('Loading packed module: ' . $k);
|
||||
}
|
||||
require_once $v['phar-path'];
|
||||
$func = 'loader' . $v['generated-id'];
|
||||
@@ -195,7 +195,7 @@ class OnWorkerStart implements SwooleEvent
|
||||
$list = ModuleManager::getComposerModules();
|
||||
foreach ($list as $k => $v) {
|
||||
if (\server()->worker_id === 0) {
|
||||
Console::info('Loading composer module: ' . $k);
|
||||
logger()->info('Loading composer module: ' . $k);
|
||||
}
|
||||
$parser->addRegisterPath($v['module-path'], $v['namespace']);
|
||||
}
|
||||
@@ -205,7 +205,7 @@ class OnWorkerStart implements SwooleEvent
|
||||
|
||||
skip:
|
||||
// 加载自定义的全局函数
|
||||
Console::debug('Loading context class...');
|
||||
logger()->debug('Loading context class...');
|
||||
$context_class = ZMConfig::get('global', 'context_class');
|
||||
if (!is_a($context_class, ContextInterface::class, true)) {
|
||||
throw new ZMKnownException('E00032', 'Context class must implemented from ContextInterface!');
|
||||
@@ -215,7 +215,7 @@ class OnWorkerStart implements SwooleEvent
|
||||
ZMConfig::get('global', 'modules')['onebot'] ??
|
||||
['status' => true, 'single_bot_mode' => false, 'message_level' => 99999];
|
||||
if ($obb_onebot['status']) {
|
||||
Console::debug('OneBot support enabled, listening OneBot event(3).');
|
||||
logger()->debug('OneBot support enabled, listening OneBot event(3).');
|
||||
$obj = new OnMessageEvent();
|
||||
$obj->connect_type = 'qq';
|
||||
$obj->class = AdapterInterface::class;
|
||||
@@ -240,8 +240,8 @@ class OnWorkerStart implements SwooleEvent
|
||||
if (isset(ZMConfig::get('global', 'sql_config')['sql_host'])) {
|
||||
if (ZMConfig::get('global', 'sql_config')['sql_host'] != '') {
|
||||
if (\server()->worker_id === 0) {
|
||||
Console::warning("使用 'sql_config' 配置项和 DB 数据库查询构造器进行查询数据库可能会在下一个大版本中废弃,请使用 'mysql_config' 搭配 doctrine dbal 使用!");
|
||||
Console::warning('详见: `https://framework.zhamao.xin/`');
|
||||
logger()->warning("使用 'sql_config' 配置项和 DB 数据库查询构造器进行查询数据库可能会在下一个大版本中废弃,请使用 'mysql_config' 搭配 doctrine dbal 使用!");
|
||||
logger()->warning('详见: `https://framework.zhamao.xin/`');
|
||||
}
|
||||
$origin_conf = ZMConfig::get('global', 'sql_config');
|
||||
$real_conf = [
|
||||
@@ -263,7 +263,7 @@ class OnWorkerStart implements SwooleEvent
|
||||
}
|
||||
}
|
||||
if (!empty($real_conf)) {
|
||||
Console::info('Connecting to MySQL pool');
|
||||
logger()->info('Connecting to MySQL pool');
|
||||
ob_start();
|
||||
phpinfo(); // 这个phpinfo是有用的,不能删除
|
||||
$str = ob_get_clean();
|
||||
@@ -305,7 +305,7 @@ class OnWorkerStart implements SwooleEvent
|
||||
SpinLock::unlock('worker_start_status');
|
||||
$used = round((microtime(true) - LightCacheInside::get('tmp_kv', 'start_time')) * 1000, 3);
|
||||
$worker_count = \server()->setting['worker_num'];
|
||||
Console::success("{$worker_count} 个工作进程成功启动,共用时 {$used} ms");
|
||||
logger()->info("{$worker_count} 个工作进程成功启动,共用时 {$used} ms");
|
||||
} else {
|
||||
LightCacheInside::set('tmp_kv', 'worker_start_status', $ls);
|
||||
SpinLock::unlock('worker_start_status');
|
||||
|
||||
@@ -7,7 +7,6 @@ namespace ZM\Event\SwooleEvent;
|
||||
use Swoole\Server;
|
||||
use ZM\Annotation\Swoole\SwooleHandler;
|
||||
use ZM\Config\ZMConfig;
|
||||
use ZM\Console\Console;
|
||||
use ZM\Event\SwooleEvent;
|
||||
use ZM\Store\LightCache;
|
||||
use ZM\Utils\Manager\ProcessManager;
|
||||
@@ -23,7 +22,7 @@ class OnWorkerStop implements SwooleEvent
|
||||
if ($worker_id == (ZMConfig::get('global.worker_cache.worker') ?? 0)) {
|
||||
LightCache::savePersistence();
|
||||
}
|
||||
Console::verbose(($server->taskworker ? 'Task' : '') . "Worker #{$worker_id} 已停止 (Worker 状态码: " . $server->getWorkerStatus($worker_id) . ')');
|
||||
logger()->debug('{worker}#{worker_id} 已停止(状态码:{status})', ['worker' => ($server->taskworker ? 'Task' : '') . 'Worker', 'worker_id' => $worker_id, 'status' => $server->getWorkerStatus($worker_id)]);
|
||||
ProcessManager::removeProcessState($server->taskworker ? ZM_PROCESS_TASKWORKER : ZM_PROCESS_WORKER, $worker_id);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user