From c34ecfebac9d90353b58f9de71a142febcc935f5 Mon Sep 17 00:00:00 2001 From: genuineparts Date: Tue, 12 Nov 2024 15:06:41 +0100 Subject: [PATCH] Added logging --- pom.xml | 18 ++++++++++++++++-- src/main/java/be/jaud/Main.java | 33 ++++++++++++++++++++++++--------- src/main/resources/logback.xml | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 11 deletions(-) create mode 100644 src/main/resources/logback.xml diff --git a/pom.xml b/pom.xml index 6cf0936..af3bd65 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,11 @@ 0.1 jar ÖBB delay check and output for Info Orbs - + + UTF-8 + UTF-8 + + org.json json @@ -17,7 +21,16 @@ config 1.4.3 - + + org.slf4j + slf4j-api + 2.0.16 + + + ch.qos.logback + logback-classic + 1.5.12 + OebbApi @@ -73,6 +86,7 @@ org.apache.maven.plugins maven-compiler-plugin + 3.13.0 21 21 diff --git a/src/main/java/be/jaud/Main.java b/src/main/java/be/jaud/Main.java index 3fb79cc..d236f71 100644 --- a/src/main/java/be/jaud/Main.java +++ b/src/main/java/be/jaud/Main.java @@ -1,5 +1,7 @@ package be.jaud; +import org.slf4j.LoggerFactory; +import org.slf4j.Logger; import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpHandler; import com.sun.net.httpserver.HttpServer; @@ -16,30 +18,43 @@ import java.util.HashMap; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; - public class Main { + private static final String version = "0.2"; private static final HashMap data = new HashMap<>(); public static Settings conf; static OebbCheck check = new OebbCheck(); static long time; static SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); static ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor(); + public static final Logger LOG = LoggerFactory.getLogger(Main.class); - public static void main(String[] args) throws IOException { + public static void main(String[] args) { conf = Settings.getInstance(); - refresh(); - HttpServer server = HttpServer.create(new InetSocketAddress(conf.getInt("port")), 0); - server.createContext("/oebbapi", new OebbHandler()); - server.setExecutor(null); // creates a default executor + LOG.info("OebbInfo Version " + version + " starting."); + try { + refresh(); + } catch (IOException e) { + LOG.error("Error at refresh: {}", e.toString()); + } + HttpServer server = null; + try { + server = HttpServer.create(new InetSocketAddress(conf.getInt("port")), 0); + } catch (IOException e) { + LOG.error("Error at HttpServer: {}", e.toString()); + } Runnable task = () -> { try { refresh(); } catch (IOException e) { - throw new RuntimeException(e); + LOG.error("Error at Task: {}", e.toString()); } }; service.scheduleAtFixedRate(task, 60, conf.getInt("refreshTime"), TimeUnit.SECONDS); - server.start(); + if (server != null) { + server.createContext("/oebbapi", new OebbHandler()); + server.setExecutor(null); // creates a default executor + server.start(); + } } private static void refresh() throws IOException { @@ -61,7 +76,7 @@ public class Main { os.write(response.getBytes()); os.close(); } catch (IOException e) { - throw new RuntimeException(e); + LOG.error("Error at HttpRequest: {}", e.toString()); } } } diff --git a/src/main/resources/logback.xml b/src/main/resources/logback.xml new file mode 100644 index 0000000..71d8c79 --- /dev/null +++ b/src/main/resources/logback.xml @@ -0,0 +1,32 @@ + + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%ex%n + + + + + + %-4relative [%thread] %-5level %logger{35} - %msg %ex %n + + logs/logfile.log + + logs/logfile-%i.log + + 1 + 10 + + + 1024KB + + + + + + + \ No newline at end of file