Added logging

This commit is contained in:
genuineparts 2024-11-12 15:06:41 +01:00
parent 892f5fe56b
commit c34ecfebac
3 changed files with 72 additions and 11 deletions

16
pom.xml
View file

@ -6,6 +6,10 @@
<version>0.1</version> <version>0.1</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>ÖBB delay check and output for Info Orbs</name> <name>ÖBB delay check and output for Info Orbs</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.json</groupId> <groupId>org.json</groupId>
@ -17,7 +21,16 @@
<artifactId>config</artifactId> <artifactId>config</artifactId>
<version>1.4.3</version> <version>1.4.3</version>
</dependency> </dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.16</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.5.12</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
<finalName>OebbApi</finalName> <finalName>OebbApi</finalName>
@ -73,6 +86,7 @@
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
<configuration> <configuration>
<source>21</source> <source>21</source>
<target>21</target> <target>21</target>

View file

@ -1,5 +1,7 @@
package be.jaud; package be.jaud;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler; import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer; import com.sun.net.httpserver.HttpServer;
@ -16,31 +18,44 @@ import java.util.HashMap;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
public class Main { public class Main {
private static final String version = "0.2";
private static final HashMap<String, Integer> data = new HashMap<>(); private static final HashMap<String, Integer> data = new HashMap<>();
public static Settings conf; public static Settings conf;
static OebbCheck check = new OebbCheck(); static OebbCheck check = new OebbCheck();
static long time; static long time;
static SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); static SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
static ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor(); 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(); conf = Settings.getInstance();
LOG.info("OebbInfo Version " + version + " starting.");
try {
refresh(); refresh();
HttpServer server = HttpServer.create(new InetSocketAddress(conf.getInt("port")), 0); } catch (IOException e) {
server.createContext("/oebbapi", new OebbHandler()); LOG.error("Error at refresh: {}", e.toString());
server.setExecutor(null); // creates a default executor }
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 = () -> { Runnable task = () -> {
try { try {
refresh(); refresh();
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); LOG.error("Error at Task: {}", e.toString());
} }
}; };
service.scheduleAtFixedRate(task, 60, conf.getInt("refreshTime"), TimeUnit.SECONDS); service.scheduleAtFixedRate(task, 60, conf.getInt("refreshTime"), TimeUnit.SECONDS);
if (server != null) {
server.createContext("/oebbapi", new OebbHandler());
server.setExecutor(null); // creates a default executor
server.start(); server.start();
} }
}
private static void refresh() throws IOException { private static void refresh() throws IOException {
data.put(conf.getString("monitors.monitor1.DepartureTime"),check.getStatus(conf.getString("monitors.monitor1.Station"),conf.getString("monitors.monitor1.DepartureTime"), conf.getString("monitors.monitor1.FinalStop"),conf.getString("monitors.monitor1.TrainType"),String.valueOf(conf.getInt("monitors.monitor1.maxjourneys")))); data.put(conf.getString("monitors.monitor1.DepartureTime"),check.getStatus(conf.getString("monitors.monitor1.Station"),conf.getString("monitors.monitor1.DepartureTime"), conf.getString("monitors.monitor1.FinalStop"),conf.getString("monitors.monitor1.TrainType"),String.valueOf(conf.getInt("monitors.monitor1.maxjourneys"))));
@ -61,7 +76,7 @@ public class Main {
os.write(response.getBytes()); os.write(response.getBytes());
os.close(); os.close();
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); LOG.error("Error at HttpRequest: {}", e.toString());
} }
} }
} }

View file

@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder
by default -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%ex%n
</pattern>
</encoder>
</appender>
<appender name="FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg %ex %n</pattern>
</encoder>
<file>logs/logfile.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>logs/logfile-%i.log
</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>10</maxIndex>
</rollingPolicy>
<triggeringPolicy
class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>1024KB</MaxFileSize>
</triggeringPolicy>
</appender>
<root level="info">
<appender-ref ref="FILE"/>
<appender-ref ref="STDOUT"/>
</root>
</configuration>