created log class

This commit is contained in:
max mustermann 2019-10-02 21:42:04 +02:00
parent 4d7792c469
commit 889cdef86b
4 changed files with 112 additions and 6 deletions

View File

@ -1,4 +1,5 @@
import db.jdcb; import db.jdcb;
import website.Log;
import java.io.IOException; import java.io.IOException;
import java.sql.ResultSet; import java.sql.ResultSet;
@ -15,7 +16,7 @@ public class main {
Runtime.getRuntime().addShutdownHook(new Thread(() -> { Runtime.getRuntime().addShutdownHook(new Thread(() -> {
try { try {
Thread.sleep(200); Thread.sleep(200);
System.out.println("Shutting down ..."); Log.warning("Shutting down ...");
//shutdown routine //shutdown routine
} catch (InterruptedException e) { } catch (InterruptedException e) {
@ -26,8 +27,7 @@ public class main {
Thread mythread = new Thread(() -> new website.Webserver().startserver()); Thread mythread = new Thread(() -> new website.Webserver().startserver());
mythread.start(); mythread.start();
Log.message("thread started");
System.out.println("thread started");

103
src/website/Log.java Normal file
View File

@ -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<String> colors= new ArrayList<String>(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);
}
}

View File

@ -22,7 +22,7 @@ public class MainPage implements HttpHandler {
} else { } else {
path = uri.getPath(); path = uri.getPath();
} }
System.out.println("looking for: " + root + path); Log.message("looking for: " + root + path);
File file = new File(root + path).getCanonicalFile(); File file = new File(root + path).getCanonicalFile();

View File

@ -5,10 +5,12 @@ import com.sun.net.httpserver.HttpServer;
import java.io.IOException; import java.io.IOException;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.util.logging.Level;
public class Webserver { public class Webserver {
public void startserver() { public void startserver() {
System.out.println("starting server"); Log.info("starting server");
HttpServer server = null; HttpServer server = null;
try { try {
server = HttpServer.create(new InetSocketAddress(8000), 0); server = HttpServer.create(new InetSocketAddress(8000), 0);
@ -23,7 +25,8 @@ public class Webserver {
server.setExecutor(null); // creates a default executor server.setExecutor(null); // creates a default executor
server.start(); 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");
} }