seq logging

This commit is contained in:
LionTion committed 2026-10-30 08:54:03 +01:00
1 parent 5bd9170800
commit fb37ba3b50
3 files changed
+56 -8

No files matched your search

@@ -3,13 +3,13 @@ package de.exodus_hn.excalibur_tour.configurations;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import java.io.IOException;
@@ -42,6 +42,13 @@ public class MqttConfig extends TextWebSocketHandler {
@Value("${testing.isMqttDev}")
public boolean isDev;
private final serilogj.core.Logger seqLogger;
@Autowired
public MqttConfig(serilogj.core.Logger seqLogger) {
this.seqLogger = seqLogger;
}
@PostConstruct
public void initMqtt() {
try {
@@ -78,10 +85,10 @@ public class MqttConfig extends TextWebSocketHandler {
if (topic.equals(eventTopic)) {
if ("online".equalsIgnoreCase(payload)) {
swordIsOnline.set(true);
logger.info("sword is online");
seqLogger.information("[{Service}]: Got Sword {State}!", (isDev ? "Staging-" : "") + "Backend", "Online");
} else if ("offline".equalsIgnoreCase(payload)) {
swordIsOnline.set(false);
logger.info("sword is offline");
seqLogger.information("[{Service}]: Got Sword {State}!", (isDev ? "Staging-" : "") + "Backend", "Offline");
}
}
@@ -136,16 +143,16 @@ public class MqttConfig extends TextWebSocketHandler {
if (clientMsg.payload.equals("takeLock")) {
logger.info(String.format("SID: %s LID: %s", session.getId(), swordLockId.get()));
if (swordLock.compareAndSet(false, true)) {
logger.info("LOCK SET");
swordLockId.set(session.getId());
broadcastToWebSockets("system/swordLock", "locked");
seqLogger.information("[{Service}]: SwordMutexLock {LockState}", (isDev ? "Staging-" : "") + "Backend", "Locked");
}
} else if (clientMsg.payload.equals("giveLock")) {
logger.info(String.format("SID: %s LID: %s", session.getId(), swordLockId.get()));
if (session.getId().equals(swordLockId.get())) {
logger.info("LOCK UNSET");
swordLock.set(false);
broadcastToWebSockets("system/swordLock", "unlocked");
seqLogger.information("[{Service}]: SwordMutexLock {LockState}", (isDev ? "Staging-" : "") + "Backend", "Unlocked");
}
}
}
@@ -169,8 +176,8 @@ public class MqttConfig extends TextWebSocketHandler {
if (swordlockid != null && swordlockid.equals(sessionId)) {
swordLock.set(false);
swordLockId.set(null);
logger.info("LOCK UNSET");
broadcastToWebSockets("system/swordLock", "unlocked");
seqLogger.information("[{Service}]: SwordMutexLock {LockState} due to client disconnect", (isDev ? "Staging-" : "") + "Backend", "Unlocked");
}
logger.fine("Client getrennt: " + session.getId());
@@ -185,8 +192,8 @@ public class MqttConfig extends TextWebSocketHandler {
if (swordlockid != null && swordlockid.equals(sessionId)) {
swordLock.set(false);
swordLockId.set(null);
logger.info("LOCK UNSET");
broadcastToWebSockets("system/swordLock", "unlocked");
seqLogger.information("[{Service}]: SwordMutexLock {LockState} due to client error", (isDev ? "Staging-" : "") + "Backend", "Unlocked");
}
}
@@ -0,0 +1,30 @@
package de.exodus_hn.excalibur_tour.configurations;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import serilogj.LoggerConfiguration;
import serilogj.events.LogEventLevel;
import static serilogj.sinks.coloredconsole.ColoredConsoleSinkConfigurator.coloredConsole;
import static serilogj.sinks.seq.SeqSinkConfigurator.seq;
@Configuration
public class SerilogjConfig {
@Value("${seq.server.url}")
private String seqUrl;
@Value("${seq.server.apiKey}")
private String seqApiKey;
@Bean
public serilogj.core.Logger seqLogger() {
return new LoggerConfiguration()
.writeTo(coloredConsole())
.writeTo(seq(seqUrl, seqApiKey))
.setMinimumLevel(LogEventLevel.Verbose)
.createLogger();
}
}