feat: 日志级别控制更详细、新增实例信息回显.
This commit is contained in:
@@ -5,6 +5,7 @@ import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
|
||||
import com.serliunx.ddns.constant.InstanceSource;
|
||||
import com.serliunx.ddns.constant.InstanceType;
|
||||
import com.serliunx.ddns.support.InstanceContextHolder;
|
||||
import com.serliunx.ddns.support.NetworkContextHolder;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -22,6 +23,7 @@ import static com.serliunx.ddns.constant.SystemConstants.XML_ROOT_INSTANCE_NAME;
|
||||
public abstract class AbstractInstance implements Instance {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(AbstractInstance.class);
|
||||
|
||||
/**
|
||||
* 实例名称
|
||||
* <li> 全局唯一
|
||||
@@ -69,20 +71,28 @@ public abstract class AbstractInstance implements Instance {
|
||||
public void run() {
|
||||
if (isPause()) // 暂停态检查, 已暂停则不继续进行.
|
||||
return;
|
||||
// 设置实例信息
|
||||
InstanceContextHolder.setInstance(this);
|
||||
log.debug("正在尝试查询IP信息.");
|
||||
value = query();
|
||||
final String ipAddress = NetworkContextHolder.getIpAddress();
|
||||
try {
|
||||
if (value != null && !value.isEmpty()
|
||||
&& ipAddress != null && !ipAddress.isEmpty()) {
|
||||
if (value.equals(ipAddress))
|
||||
if (value.equals(ipAddress)) {
|
||||
log.debug("最新记录的IP与当前IP地址一致, 无需更新.");
|
||||
return;
|
||||
}
|
||||
}
|
||||
log.debug("正在尝试将记录旧IP: {} 更新为: {}", value, ipAddress);
|
||||
value = ipAddress;
|
||||
run0();
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage());
|
||||
} finally {
|
||||
this.value = null;
|
||||
// 移除实例信息
|
||||
InstanceContextHolder.clear();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -100,7 +100,7 @@ public class AliyunInstance extends AbstractInstance {
|
||||
.setEndpointOverride("alidns.cn-hangzhou.aliyuncs.com")
|
||||
)
|
||||
.build();
|
||||
debug("初始化完成.");
|
||||
log.debug("初始化完成.");
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -111,18 +111,18 @@ public class AliyunInstance extends AbstractInstance {
|
||||
.type(recordType)
|
||||
.value(value)
|
||||
.build();
|
||||
debug("正在更新解析记录.");
|
||||
log.debug("正在更新解析记录.");
|
||||
CompletableFuture<UpdateDomainRecordResponse> requestResponse = client.updateDomainRecord(request);
|
||||
try {
|
||||
requestResponse.whenComplete((v, t) -> {
|
||||
if (t != null) { //出现异常
|
||||
handleThrowable(t);
|
||||
log.error("出现异常 {}:", t.getCause(), t.getMessage());
|
||||
} else {
|
||||
String result = null;
|
||||
try {
|
||||
result = jsonMapper.writeValueAsString(v.getBody());
|
||||
} catch (JsonProcessingException ignored) {} finally {
|
||||
debug("操作结束, 结果: {}", result == null ? v : result);
|
||||
log.debug("操作结束, 结果: {}", result == null ? v : result);
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -146,10 +146,10 @@ public class AliyunInstance extends AbstractInstance {
|
||||
}
|
||||
return null;
|
||||
} catch (InterruptedException | ExecutionException e) {
|
||||
error("出现了不应该出现的异常 => {}", e);
|
||||
log.error("出现了不应该出现的异常 => {}", e);
|
||||
return null;
|
||||
} catch (TimeoutException e) {
|
||||
error("记录查询超时!");
|
||||
log.error("记录查询超时!");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -235,23 +235,4 @@ public class AliyunInstance extends AbstractInstance {
|
||||
", value='" + value + '\'' +
|
||||
'}';
|
||||
}
|
||||
|
||||
private void handleThrowable(Throwable t){
|
||||
error("出现异常 {}:", t.getCause(), t.getMessage());
|
||||
}
|
||||
|
||||
@SuppressWarnings("all")
|
||||
private void log(String msg, Object...params) {
|
||||
log.info("[实例活动][" + name + "]" + msg, params);
|
||||
}
|
||||
|
||||
@SuppressWarnings("all")
|
||||
private void debug(String msg, Object...params) {
|
||||
log.debug("[实例活动][" + name + "]" + msg, params);
|
||||
}
|
||||
|
||||
@SuppressWarnings("all")
|
||||
private void error(String msg, Object...params) {
|
||||
log.error("[实例异常][" + name + "]" + msg, params);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.serliunx.ddns.support;
|
||||
|
||||
import com.serliunx.ddns.core.instance.Instance;
|
||||
|
||||
/**
|
||||
* 实例信息上下文
|
||||
* @author <a href="mailto:serliunx@yeah.net">SerLiunx</a>
|
||||
* @version 1.0.0
|
||||
* @since 2024/6/15
|
||||
*/
|
||||
public final class InstanceContextHolder {
|
||||
|
||||
private static final ThreadLocal<Instance> INSTANCE_THREAD_LOCAL = new ThreadLocal<>();
|
||||
|
||||
private InstanceContextHolder() {throw new UnsupportedOperationException();}
|
||||
|
||||
/**
|
||||
* 设置当前线程的实例信息
|
||||
* @param instance 实例
|
||||
*/
|
||||
public static void setInstance(Instance instance) {
|
||||
INSTANCE_THREAD_LOCAL.set(instance);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前线程的实例信息
|
||||
* @return 实例信息
|
||||
*/
|
||||
public static Instance getInstance() {
|
||||
return INSTANCE_THREAD_LOCAL.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除当前线程的实例信息
|
||||
*/
|
||||
public static void clear() {
|
||||
INSTANCE_THREAD_LOCAL.remove();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.serliunx.ddns.support.log;
|
||||
|
||||
import ch.qos.logback.classic.pattern.ClassicConverter;
|
||||
import ch.qos.logback.classic.spi.ILoggingEvent;
|
||||
import com.serliunx.ddns.core.instance.Instance;
|
||||
import com.serliunx.ddns.support.InstanceContextHolder;
|
||||
|
||||
/**
|
||||
* 获取当前任何线程的实例信息
|
||||
* @author <a href="mailto:serliunx@yeah.net">SerLiunx</a>
|
||||
* @version 1.0.0
|
||||
* @since 2024/6/15
|
||||
*/
|
||||
public class InstanceNameConverter extends ClassicConverter {
|
||||
|
||||
@Override
|
||||
public String convert(ILoggingEvent event) {
|
||||
Instance instance = InstanceContextHolder.getInstance();
|
||||
if (instance != null)
|
||||
return instance.getName();
|
||||
return "----";
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,8 @@ package com.serliunx.ddns.support.log;
|
||||
import ch.qos.logback.classic.pattern.ClassicConverter;
|
||||
import ch.qos.logback.classic.spi.ILoggingEvent;
|
||||
|
||||
import java.lang.management.ManagementFactory;
|
||||
|
||||
/**
|
||||
* 日志变量%pid(进程id) 转换器
|
||||
* @author <a href="mailto:serliunx@yeah.net">SerLiunx</a>
|
||||
@@ -11,7 +13,7 @@ import ch.qos.logback.classic.spi.ILoggingEvent;
|
||||
*/
|
||||
public class ProcessIdConverter extends ClassicConverter {
|
||||
|
||||
private static final String PROCESS_ID = java.lang.management.ManagementFactory.getRuntimeMXBean().getName().split("@")[0];
|
||||
private static final String PROCESS_ID = ManagementFactory.getRuntimeMXBean().getName().split("@")[0];
|
||||
|
||||
@Override
|
||||
public String convert(ILoggingEvent iLoggingEvent) {
|
||||
|
||||
Reference in New Issue
Block a user