|
@@ -1,15 +1,18 @@
|
|
|
package cn.reghao.autodop.common.log;
|
|
package cn.reghao.autodop.common.log;
|
|
|
|
|
|
|
|
import ch.qos.logback.classic.spi.ILoggingEvent;
|
|
import ch.qos.logback.classic.spi.ILoggingEvent;
|
|
|
-import ch.qos.logback.classic.spi.LoggingEvent;
|
|
|
|
|
import ch.qos.logback.core.UnsynchronizedAppenderBase;
|
|
import ch.qos.logback.core.UnsynchronizedAppenderBase;
|
|
|
|
|
+import cn.reghao.autodop.common.message.AsyncMsg;
|
|
|
|
|
+import cn.reghao.autodop.common.message.MessageType;
|
|
|
|
|
+import cn.reghao.autodop.common.message.ops.DagentOps;
|
|
|
|
|
+import cn.reghao.autodop.common.mqtt.MosquittoProperties;
|
|
|
|
|
+import cn.reghao.autodop.common.utils.DateTimeConverter;
|
|
|
|
|
+import cn.reghao.autodop.common.utils.MachineId;
|
|
|
|
|
+import cn.reghao.autodop.common.utils.serializer.JsonConverter;
|
|
|
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
|
|
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
|
|
|
import org.eclipse.paho.client.mqttv3.MqttException;
|
|
import org.eclipse.paho.client.mqttv3.MqttException;
|
|
|
import org.eclipse.paho.client.mqttv3.MqttMessage;
|
|
import org.eclipse.paho.client.mqttv3.MqttMessage;
|
|
|
|
|
|
|
|
-import java.text.DateFormat;
|
|
|
|
|
-import java.text.SimpleDateFormat;
|
|
|
|
|
-import java.util.Date;
|
|
|
|
|
import java.util.UUID;
|
|
import java.util.UUID;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -17,25 +20,28 @@ import java.util.UUID;
|
|
|
* @date 2021-06-08 19:37:21
|
|
* @date 2021-06-08 19:37:21
|
|
|
*/
|
|
*/
|
|
|
public class MqttAppender extends UnsynchronizedAppenderBase<ILoggingEvent> {
|
|
public class MqttAppender extends UnsynchronizedAppenderBase<ILoggingEvent> {
|
|
|
- private static final String MQTT_URL = "tcp://localhost:1883";
|
|
|
|
|
- private static final String MQTT_CLIENT_ID = UUID.randomUUID().toString();
|
|
|
|
|
- private static final String MQTT_TOPIC = "mqttlogger";
|
|
|
|
|
- private static final String MQTT_USER = "dev";
|
|
|
|
|
- private static final String MQTT_PASSWORD = "Dev@123456";
|
|
|
|
|
-
|
|
|
|
|
|
|
+ private MosquittoProperties properties;
|
|
|
|
|
+ private String clientId = UUID.randomUUID().toString();
|
|
|
private InternalMqttClient client;
|
|
private InternalMqttClient client;
|
|
|
|
|
+ private String machineId;
|
|
|
|
|
+ private String machineIpv4;
|
|
|
|
|
+ private String appId;
|
|
|
|
|
|
|
|
- public MqttAppender() {
|
|
|
|
|
|
|
+ public MqttAppender(MosquittoProperties properties, String appId) {
|
|
|
|
|
+ this.properties = properties;
|
|
|
|
|
+ this.machineId = MachineId.id();
|
|
|
|
|
+ this.machineIpv4 = MachineId.ipv4();
|
|
|
|
|
+ this.appId = appId;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public void start() {
|
|
public void start() {
|
|
|
super.start();
|
|
super.start();
|
|
|
try {
|
|
try {
|
|
|
- client = new InternalMqttClient(MQTT_URL, MQTT_CLIENT_ID);
|
|
|
|
|
|
|
+ client = new InternalMqttClient(properties.getBroker(), clientId);
|
|
|
MqttConnectOptions options = new MqttConnectOptions();
|
|
MqttConnectOptions options = new MqttConnectOptions();
|
|
|
- options.setUserName(MQTT_USER);
|
|
|
|
|
- options.setPassword(MQTT_PASSWORD.toCharArray());
|
|
|
|
|
|
|
+ options.setUserName(properties.getUsername());
|
|
|
|
|
+ options.setPassword(properties.getPassword().toCharArray());
|
|
|
client.setCallback(client);
|
|
client.setCallback(client);
|
|
|
client.connect(options);
|
|
client.connect(options);
|
|
|
} catch (MqttException e) {
|
|
} catch (MqttException e) {
|
|
@@ -55,32 +61,30 @@ public class MqttAppender extends UnsynchronizedAppenderBase<ILoggingEvent> {
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
protected void append(ILoggingEvent event) {
|
|
protected void append(ILoggingEvent event) {
|
|
|
- String json = format(event);
|
|
|
|
|
|
|
+ String json = JsonConverter.objectToJson(runtimeLog(event));
|
|
|
|
|
+ AsyncMsg asyncMsg = AsyncMsg.asyncMsg(MessageType.dagentType.name(), DagentOps.dagentLog.name(), json);
|
|
|
|
|
+ String payload = JsonConverter.objectToJson(asyncMsg);
|
|
|
|
|
+
|
|
|
MqttMessage message = new MqttMessage();
|
|
MqttMessage message = new MqttMessage();
|
|
|
message.setQos(0);
|
|
message.setQos(0);
|
|
|
- message.setPayload(json.getBytes());
|
|
|
|
|
|
|
+ message.setPayload(payload.getBytes());
|
|
|
try {
|
|
try {
|
|
|
- client.publish(MQTT_TOPIC, message);
|
|
|
|
|
|
|
+ client.publish(properties.getTopic(), message);
|
|
|
} catch (MqttException e) {
|
|
} catch (MqttException e) {
|
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private final int DEFAULT_BUFFER_SIZE = 512;
|
|
|
|
|
- private DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SZ");
|
|
|
|
|
-
|
|
|
|
|
- private String format(ILoggingEvent event) {
|
|
|
|
|
- StringBuilder buf = new StringBuilder(DEFAULT_BUFFER_SIZE);
|
|
|
|
|
-
|
|
|
|
|
- buf.append(event.getFormattedMessage());
|
|
|
|
|
- buf.append("\t");
|
|
|
|
|
- buf.append(df.format(new Date(event.getTimeStamp())));
|
|
|
|
|
- buf.append("\t");
|
|
|
|
|
- buf.append(event.getLoggerName());
|
|
|
|
|
- buf.append("\t");
|
|
|
|
|
- buf.append(event.getThreadName());
|
|
|
|
|
- buf.append("\t");
|
|
|
|
|
- buf.append(event.getLevel().toString());
|
|
|
|
|
- return buf.toString();
|
|
|
|
|
|
|
+ private RunningLog runtimeLog(ILoggingEvent event) {
|
|
|
|
|
+ RunningLog runningLog = new RunningLog();
|
|
|
|
|
+ runningLog.setMachineId(machineId);
|
|
|
|
|
+ runningLog.setMachineIpv4(machineIpv4);
|
|
|
|
|
+ runningLog.setAppId(appId);
|
|
|
|
|
+ runningLog.setLogTime(DateTimeConverter.localDateTime(event.getTimeStamp()));
|
|
|
|
|
+ runningLog.setLevel(event.getLevel().toString());
|
|
|
|
|
+ runningLog.setThreadName(event.getThreadName());
|
|
|
|
|
+ runningLog.setLoggerName(event.getLoggerName());
|
|
|
|
|
+ runningLog.setMessage(event.getMessage());
|
|
|
|
|
+ return runningLog;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|