Server logging improved

This commit is contained in:
2021-03-22 04:20:10 +01:00
parent d0425f6402
commit 47732cf8f3
2 changed files with 18 additions and 8 deletions

View File

@@ -2,6 +2,7 @@ package logging;
public enum LogType { public enum LogType {
Log, Log,
Message, Input,
Output,
Error Error
} }

View File

@@ -1,6 +1,7 @@
package logging; package logging;
import java.sql.Timestamp; import java.text.SimpleDateFormat;
import java.util.Date;
public class ServerLogger { public class ServerLogger {
@@ -15,21 +16,29 @@ public class ServerLogger {
private static final String ANSI_WHITE = "\u001B[37m"; private static final String ANSI_WHITE = "\u001B[37m";
public void printLog(String message, String value, String name, LogType logType){ public void printLog(String message, String value, String name, LogType logType){
//generate timestamp with fixed length
String timestamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").format(new Date());
switch (logType){ switch (logType){
case Input:
System.out.printf(ANSI_WHITE + "(%s) [%s] [input ] %s: %s%n",timestamp, name, message, value);
break;
case Output:
System.out.printf(ANSI_CYAN + "(%s) [%s] [output] %s: %s%n",timestamp, name, message, value);
break;
case Log: case Log:
System.out.printf(ANSI_CYAN + "%s %s: %s%n"+ANSI_RESET, new Timestamp(System.currentTimeMillis()), message, value); System.out.printf(ANSI_PURPLE + "(%s) [status] %s: %s%n"+ANSI_RESET, timestamp, message, value);
break; break;
case Error: case Error:
System.out.printf(ANSI_RED + "%s %s: %s%n"+ ANSI_RESET, new Timestamp(System.currentTimeMillis()), message, value); System.out.printf(ANSI_RED + "(%s) [error] %s: %s%n"+ ANSI_RESET, timestamp, message, value);
break;
case Message:
System.out.printf(ANSI_WHITE + " %s %s %s%n" +ANSI_RESET, new Timestamp(System.currentTimeMillis()), name, message);
break; break;
default: default:
System.err.println("NO VALID LOGTYPE GIVEN");; System.err.println("NO VALID LOGTYPE GIVEN");;
break;
} }
} }