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

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>