upd: 日志统一

This commit is contained in:
骑着蜗牛追导弹 2024-12-06 21:55:11 +08:00
parent 20f3a367a2
commit e187a378d5
5 changed files with 37 additions and 43 deletions

View File

@ -121,7 +121,7 @@ public class ClientConfigLoader {
StandardCharsets.UTF_8);
} catch (Exception e) {
logger.error(
"ClientConfigLoader -> 应用 {},环境 {},缓存配置文件 {} 失败",
"应用 {},环境 {},缓存配置文件 {} 失败",
clientInfo.getDataId(),
clientInfo.getEnv(),
kve.getKey(),
@ -141,7 +141,7 @@ public class ClientConfigLoader {
throws BeansException {
String defaultCacheDir = getDefaultCacheDir();
initClientInfo(defaultCacheDir, environment);
logger.info("ClientConfigLoader -> 客户端属性: {}", clientInfo);
logger.info("客户端属性: {}", clientInfo);
validateCacheDirPath(defaultCacheDir, clientInfo.getCacheDir());
createCacheDir(clientInfo.getCacheDir());
fixedTimeFlushConfigFileThread.start();
@ -150,7 +150,7 @@ public class ClientConfigLoader {
try {
ConfigClient.getInstance().start(clientInfo.getServer(), clientInfo.getPort());
} catch (InterruptedException e) {
logger.error("ClientConfigLoader -> Netty客户端启动失败", e);
logger.error("Netty客户端启动失败", e);
throw new RuntimeException(e);
}
});
@ -164,12 +164,12 @@ public class ClientConfigLoader {
Thread currentThread = Thread.currentThread();
String currentThreadName = currentThread.getName();
currentThread.interrupt();
logger.error("ClientConfigLoader -> 中断线程: {}", currentThreadName, e);
logger.error("中断线程: {}", currentThreadName, e);
}
}
// 判断配置中心服务是否处于离线状态
if (isServerOffline) {
logger.info("ClientConfigLoader -> 配置中心离线,尝试从本地缓存加载配置文件");
logger.info("配置中心离线,尝试从本地缓存加载配置文件");
String cacheDir = clientInfo.getCacheDir();
FileUtil.walkFiles(
FileUtil.file(cacheDir),
@ -197,7 +197,7 @@ public class ClientConfigLoader {
lastConfigs.put(fileName, tempMap);
}
} catch (Exception e) {
logger.info("ClientConfigLoader -> 配置文件转map失败", e);
logger.info("配置文件转map失败", e);
}
}));
}

View File

@ -43,7 +43,7 @@ public class ConfigClient {
private static volatile ConfigClient instance;
/** 最大重试次数 */
private static final int MAX_RETRY_COUNT = 2;
private static final int MAX_RETRY_COUNT = 5;
/** 当前重试次数 */
private static int retryCount = 0;
@ -90,7 +90,7 @@ public class ConfigClient {
}
private void doConnect(String server, Integer port) throws InterruptedException {
logger.info("ConfigClient -> Start");
logger.info("开始连接配置中心服务...");
if (channel != null && channel.isActive()) {
return;
}
@ -107,11 +107,11 @@ public class ConfigClient {
channel = channelFuture.channel();
// 重置重试次数
retryCount = 0;
logger.info("ConfigClient -> Connect success {}:{}", serverIp, serverPort);
logger.info("连接成功 {}:{}", serverIp, serverPort);
} else {
// 失败后delaySecond秒默认是5秒重连周期性delaySecond秒的重连
retryCount++;
logger.info("ConfigClient -> 当前重试次数: {}", retryCount);
logger.info("当前重试次数: {}", retryCount);
if (retryCount >= MAX_RETRY_COUNT) {
ClientConfigLoader.isConfigLoaded = true;
ClientConfigLoader.isServerOffline = true;
@ -133,13 +133,11 @@ public class ConfigClient {
/** 重新连接 */
protected void reConnect() {
try {
logger.info(
"ConfigClient -> Start reconnect to server.{}:{}", this.serverIp, this.serverPort);
logger.info("重连配置中心服务 {}:{}", this.serverIp, this.serverPort);
ClientConfigLoader.isConfigLoaded = false;
ClientConfigLoader.isServerOffline = false;
if (channel != null && channel.isOpen()) {
logger.info(
"ConfigClient -> Server [{}] channel is active, close it and reconnect", this.serverIp);
logger.info("Channel已激活, 关闭且重启中...");
channel.close();
}
bootstrap
@ -148,11 +146,7 @@ public class ConfigClient {
.sync()
.channel();
} catch (Exception e) {
logger.error(
"ConfigClient -> ReConnect to server failure. server={}:{}:{}",
this.serverIp,
this.serverPort,
e.getMessage());
logger.error("重连配置中心服务失败 {}:{}", this.serverIp, this.serverPort, e);
}
}
}

View File

@ -29,17 +29,17 @@ public class ConfigClientHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
logger.info("ConfigClientHandler -> 当Channel已经注册到它的EventLoop并且能够处理I/O时被调用");
logger.info("当Channel已经注册到它的EventLoop并且能够处理I/O时被调用");
}
@Override
public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
logger.info("ConfigClientHandler -> 当Channel从它的EventLoop注销并且无法处理任何I/O时被调用");
logger.info("当Channel从它的EventLoop注销并且无法处理任何I/O时被调用");
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
logger.info("ConfigClientHandler -> 当Channel处于活动状态已经连接到它的远程节点时被调用, 注册客户端");
logger.info("当Channel处于活动状态已经连接到它的远程节点时被调用, 注册客户端");
SmallMessage smallMessage = new SmallMessage();
smallMessage.setType(TransferMessageType.REGISTER);
smallMessage.setResp(SmallMessage.Response.ok(ClientConfigLoader.clientInfo));
@ -48,22 +48,22 @@ public class ConfigClientHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
logger.info("ConfigClientHandler -> 当Channel离开活动状态并且不再连接到它的远程节点时被调用");
logger.info("当Channel离开活动状态并且不再连接到它的远程节点时被调用");
/// this.configClient.reConnect();
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
/// logger.info("ConfigClientHandler -> 当从Channel读取数据时被调用, 收到服务器消息");
logger.info("ConfigClientHandler -> 从服务端读取到Object");
/// logger.info("当从Channel读取数据时被调用, 收到服务器消息");
logger.info("从服务端读取到Object");
SmallMessage smallMessage = MessageUtil.getMessage(msg);
SmallMessage.Response resp = smallMessage.getResp();
switch (smallMessage.getType()) {
case REGISTER:
if (!resp.getSuccess()) {
logger.info("ConfigClientHandler -> 注册失败, {}", resp.getErrorMessage());
logger.info("注册失败, {}", resp.getErrorMessage());
} else {
logger.info("ConfigClientHandler -> 注册成功, 给服务端发信号, 表明准备好可以拉取配置了");
logger.info("注册成功, 给服务端发信号, 表明准备好可以拉取配置了");
// 准备拉取配置给服务端发信号
SmallMessage pullConfigMessage = new SmallMessage();
pullConfigMessage.setType(TransferMessageType.PULL_CONFIG);
@ -76,7 +76,7 @@ public class ConfigClientHandler extends ChannelInboundHandlerAdapter {
throw new RuntimeException(resp.getErrorMessage());
}
List<ConfigFileInfo> configFileInfos = MessageUtil.toConfigFileInfoList(resp.getData());
logger.info("ConfigClientHandler -> 收到来自服务端推送的配置信息");
logger.info("收到来自服务端推送的配置信息");
Map<String, String> originConfigs = new HashMap<>(1);
Map<String, Map<String, Object>> lastConfigs = new HashMap<>(1);
try {
@ -101,7 +101,7 @@ public class ConfigClientHandler extends ChannelInboundHandlerAdapter {
lastConfigs.put(fileName, tempMap);
}
}
logger.info("ConfigClientHandler -> 配置文件转map成功");
logger.info("配置文件转map成功");
ClientConfigLoader.originConfigs = originConfigs;
ClientConfigLoader.lastConfigs = lastConfigs;
ClientConfigLoader.isConfigLoaded = true;
@ -110,7 +110,7 @@ public class ConfigClientHandler extends ChannelInboundHandlerAdapter {
ClientConfigLoader.clientInfo.notifyAll();
}
} catch (IOException e) {
logger.info("ConfigClientHandler -> 配置文件转map失败", e);
logger.info("配置文件转map失败", e);
}
break;
default:

View File

@ -56,7 +56,7 @@ public class ConfigClientManage {
public static void register(String env, String dataId, ChannelHandlerContext ctx) {
String envClientKey = String.format("%s_%s_%s", env, dataId, ChannelUtil.getId(ctx));
CLIENT.put(envClientKey, ctx.channel());
log.info("ConfigClientManage -> 客户端 {} 注册成功", envClientKey);
log.info("客户端 {} 注册成功", envClientKey);
}
/**
@ -76,7 +76,7 @@ public class ConfigClientManage {
channel.closeFuture();
}
CLIENT.remove(envClientKey);
log.info("ConfigClientManage -> 客户端 {} 注销成功", envClientKey);
log.info("客户端 {} 注销成功", envClientKey);
}
}
}

View File

@ -29,9 +29,9 @@ public class ConfigServerHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
/// log.info("ServerHandler -> 当从Channel读取数据时被调用");
/// log.info("当从Channel读取数据时被调用");
SmallMessage smallMessage = MessageUtil.getMessage(msg);
log.info("ServerHandler -> 从客户端读取到Object{}", smallMessage);
log.info("从客户端读取到Object{}", smallMessage);
SmallMessage.Response resp = smallMessage.getResp();
switch (smallMessage.getType()) {
case REGISTER:
@ -53,16 +53,16 @@ public class ConfigServerHandler extends ChannelInboundHandlerAdapter {
break;
case PULL_CONFIG:
if (!resp.getSuccess()) {
log.info("ServerHandler -> 客户端说它没有准备好拉取配置");
log.info("客户端说它没有准备好拉取配置");
break;
}
log.info("ServerHandler -> 客户端说它准备好拉取配置了");
log.info("客户端说它准备好拉取配置了");
String[] envDataId = ConfigClientManage.getEnvDataId(ctx.channel().id());
String env = envDataId[0];
String dataId = envDataId[1];
List<Channel> channels = ConfigClientManage.queryChannels(env, dataId);
if (channels.isEmpty()) {
log.info("ServerHandler -> 没有在线的客户端");
log.info("没有在线的客户端");
break;
}
List<ConfigFileInfo> fileList = configFileService.getFileList(env, dataId);
@ -74,7 +74,7 @@ public class ConfigServerHandler extends ChannelInboundHandlerAdapter {
MessageUtil.toPushConfigBad(String.format("应用 %s 没有环境编码为 %s 的配置", dataId, env)));
} catch (Exception e) {
log.error(
"ServerHandler -> 推送无法获取配置的消息到客户端失败, env: {}, dataId={}, channelId={}",
"推送无法获取配置的消息到客户端失败, env: {}, dataId={}, channelId={}",
env,
dataId,
ChannelUtil.getId(channel.id()),
@ -87,13 +87,13 @@ public class ConfigServerHandler extends ChannelInboundHandlerAdapter {
try {
ctx.writeAndFlush(MessageUtil.toPushConfigOk(fileList));
log.info(
"ServerHandler -> 推送配置到客户端成功, env: {}, dataId={}, channelId={}",
"推送配置到客户端成功, env: {}, dataId={}, channelId={}",
env,
dataId,
ChannelUtil.getId(channel.id()));
} catch (Exception e) {
log.error(
"ServerHandler -> 推送配置到客户端失败, env: {}, dataId={}, channelId={}",
"推送配置到客户端失败, env: {}, dataId={}, channelId={}",
env,
dataId,
ChannelUtil.getId(channel.id()),
@ -109,7 +109,7 @@ public class ConfigServerHandler extends ChannelInboundHandlerAdapter {
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
log.error("ServerHandler -> 当Channel发生异常被调用", cause);
log.error("当Channel发生异常被调用", cause);
/// cause.printStackTrace();
ConfigClientManage.unregister(ctx.channel().id());
ctx.close();
@ -117,13 +117,13 @@ public class ConfigServerHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
log.info("ServerHandler -> 当Channel处于非活动状态已经连接到它的远程节点时被调用");
log.info("当Channel处于非活动状态已经连接到它的远程节点时被调用");
ConfigClientManage.unregister(ctx.channel().id());
}
@Override
public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
log.info("ServerHandler -> 当Channel从它的EventLoop注销并且不能够处理I/O时被调用");
log.info("当Channel从它的EventLoop注销并且不能够处理I/O时被调用");
ConfigClientManage.unregister(ctx.channel().id());
}
}