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

18
pom.xml
View file

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

View file

@ -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<String, Integer> 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());
}
}
}

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>