diff --git a/pom.xml b/pom.xml index 8eb8276..e658dfa 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ de.exodus-hn excalibur-tour - 0.1.0 + 0.1.1 @@ -29,6 +29,12 @@ 26 + + + jitpack.io + https://jitpack.io + + org.springframework.boot @@ -102,6 +108,11 @@ org.springframework.boot spring-boot-starter-websocket + + com.github.serilogj + serilogj + 0.6.1 + diff --git a/src/main/java/de/exodus_hn/excalibur_tour/configurations/MqttConfig.java b/src/main/java/de/exodus_hn/excalibur_tour/configurations/MqttConfig.java index 5c4ea04..6094269 100644 --- a/src/main/java/de/exodus_hn/excalibur_tour/configurations/MqttConfig.java +++ b/src/main/java/de/exodus_hn/excalibur_tour/configurations/MqttConfig.java @@ -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"); } } diff --git a/src/main/java/de/exodus_hn/excalibur_tour/configurations/SerilogjConfig.java b/src/main/java/de/exodus_hn/excalibur_tour/configurations/SerilogjConfig.java new file mode 100644 index 0000000..63d40d9 --- /dev/null +++ b/src/main/java/de/exodus_hn/excalibur_tour/configurations/SerilogjConfig.java @@ -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(); + } + +}