diff --git a/src/main.java b/src/main.java index b78ecb5..0cc4b89 100644 --- a/src/main.java +++ b/src/main.java @@ -1,4 +1,5 @@ import db.jdcb; +import website.Log; import java.io.IOException; import java.sql.ResultSet; @@ -15,7 +16,7 @@ public class main { Runtime.getRuntime().addShutdownHook(new Thread(() -> { try { Thread.sleep(200); - System.out.println("Shutting down ..."); + Log.warning("Shutting down ..."); //shutdown routine } catch (InterruptedException e) { @@ -26,8 +27,7 @@ public class main { Thread mythread = new Thread(() -> new website.Webserver().startserver()); mythread.start(); - - System.out.println("thread started"); + Log.message("thread started"); diff --git a/src/website/Log.java b/src/website/Log.java new file mode 100644 index 0000000..797c95b --- /dev/null +++ b/src/website/Log.java @@ -0,0 +1,103 @@ +package website; + +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Date; +import java.util.logging.*; + +public class Log { + + private static final String ANSI_RESET = "\u001B[0m"; + private static final String ANSI_BLACK = "\u001B[30m"; + private static final String ANSI_RED = "\u001B[31m"; + private static final String ANSI_GREEN = "\u001B[32m"; + private static final String ANSI_YELLOW = "\u001B[33m"; + private static final String ANSI_BLUE = "\u001B[34m"; + private static final String ANSI_PURPLE = "\u001B[35m"; + private static final String ANSI_CYAN = "\u001B[36m"; + private static final String ANSI_WHITE = "\u001B[37m"; + + public static final int CRITICAL_ERROR=6; + public static final int ERROR=5; + public static final int WARNING=4; + public static final int INFO=3; + public static final int MESSAGE=2; + public static final int DEBUG=1; + + private static ArrayList colors= new ArrayList(Arrays.asList("","DEBUG","MESSAGE","INFO","WARNING","ERROR","CRITICAL_ERROR")); + + public static void criticalerror(String msg){ + log(msg,CRITICAL_ERROR); + } + + public static void error(String msg){ + log(msg,ERROR); + } + + public static void warning(String msg){ + log(msg,WARNING); + } + + public static void info(String msg){ + log(msg,INFO); + } + + public static void message(String msg){ + log(msg,MESSAGE); + } + + public static void debug(String msg){ + log(msg,DEBUG); + } + + + public static void log(String msg, int level){ + StringBuilder builder = new StringBuilder(); + switch (level){ + case INFO: + builder.append(ANSI_CYAN); + break; + case WARNING: + builder.append(ANSI_YELLOW); + break; + case ERROR: + builder.append(ANSI_RED); + break; + case CRITICAL_ERROR: + builder.append(ANSI_RED); + break; + case MESSAGE: + builder.append(ANSI_WHITE); + break; + case DEBUG: + builder.append(ANSI_BLUE); + break; + } + builder.append("["); + builder.append(calcDate(System.currentTimeMillis())); + builder.append("]"); + + builder.append(" ["); + builder.append(new Exception().getStackTrace()[1].getClassName()); + builder.append("]"); + + builder.append(" ["); + builder.append(colors.get(level)); + builder.append("]"); + + builder.append(ANSI_WHITE); + builder.append(" - "); + builder.append(msg); + + builder.append(ANSI_RESET); + System.out.println(builder.toString()); + } + + private static String calcDate(long millisecs) { + SimpleDateFormat date_format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + Date resultdate = new Date(millisecs); + return date_format.format(resultdate); + } + +} diff --git a/src/website/MainPage.java b/src/website/MainPage.java index 21f3bea..e879e65 100644 --- a/src/website/MainPage.java +++ b/src/website/MainPage.java @@ -22,7 +22,7 @@ public class MainPage implements HttpHandler { } else { path = uri.getPath(); } - System.out.println("looking for: " + root + path); + Log.message("looking for: " + root + path); File file = new File(root + path).getCanonicalFile(); diff --git a/src/website/Webserver.java b/src/website/Webserver.java index f2ef43f..9c8bca6 100644 --- a/src/website/Webserver.java +++ b/src/website/Webserver.java @@ -5,10 +5,12 @@ import com.sun.net.httpserver.HttpServer; import java.io.IOException; import java.net.InetSocketAddress; +import java.util.logging.Level; + public class Webserver { public void startserver() { - System.out.println("starting server"); + Log.info("starting server"); HttpServer server = null; try { server = HttpServer.create(new InetSocketAddress(8000), 0); @@ -23,7 +25,8 @@ public class Webserver { server.setExecutor(null); // creates a default executor server.start(); - System.out.println("Server available at http://127.0.0.1:8000 now"); + Log.info("Server available at http://127.0.0.1:8000 now"); + }