From a735485d86d364b2d1135f1c5c546a898f0fda3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szombati=20Oliv=C3=A9r?= Date: Sun, 19 May 2024 21:06:45 +0200 Subject: [PATCH] Fix: added basic console implementation --- .../src/temalab/logger/ConsoleLogger.java | 43 ++++++++++++++ test/core/src/temalab/logger/Log.java | 14 ++--- test/core/src/temalab/logger/Logger.java | 4 -- test/core/src/temalab/logger/LoggerBase.java | 58 ++++++++++--------- 4 files changed, 80 insertions(+), 39 deletions(-) create mode 100644 test/core/src/temalab/logger/ConsoleLogger.java diff --git a/test/core/src/temalab/logger/ConsoleLogger.java b/test/core/src/temalab/logger/ConsoleLogger.java new file mode 100644 index 0000000..14827cd --- /dev/null +++ b/test/core/src/temalab/logger/ConsoleLogger.java @@ -0,0 +1,43 @@ +package temalab.logger; +public class ConsoleLogger extends LoggerBase{ + static final String ANSI_RESET = "\u001B[0m"; + static final String ANSI_RED = "\u001B[31m"; + static final String ANSI_YELLOW = "\u001B[33m"; + static final String ANSI_BLUE = "\u001B[34m"; + + public ConsoleLogger(){ + setPrintStream(System.out); + } + + //TODO maybe can be placed elsewhere, e.g. Base or Logger interface and then it could be overriden + private void printLines(String label, String message){ + for (String line : message.split("\n")) { + getPrintStream().println(String.format("\t%s - %s", label, line)); + } + getPrintStream().print(ANSI_RESET); + } + + @Override + public void debug(String label, String message){ + getPrintStream().println(ANSI_RESET + getInfos("DEBUG")); + printLines(label, message); + } + + @Override + public void info(String label, String message){ + getPrintStream().print(ANSI_BLUE + getInfos("INFO")); + printLines(label, message); + } + + @Override + public void warning(String label, String message){ + getPrintStream().print(ANSI_YELLOW + getInfos("WARNING")); + printLines(label, message); + } + + @Override + public void error(String label, String message){ + getPrintStream().print(ANSI_RED + getInfos("ERROR")); + printLines(label, message); + } +} diff --git a/test/core/src/temalab/logger/Log.java b/test/core/src/temalab/logger/Log.java index 694394f..cec5643 100644 --- a/test/core/src/temalab/logger/Log.java +++ b/test/core/src/temalab/logger/Log.java @@ -1,12 +1,8 @@ package temalab.logger; - -import java.io.PrintStream; -import java.time.LocalDateTime; -import java.time.format.DateTimeFormatter; - public final class Log { private static Logger logger = new ConsoleLogger(); + /** * By default the Logger writes to the standard error stream. */ @@ -24,8 +20,8 @@ public void setLogger(Logger newLogger) { logger = newLogger; } - public void d(String label, String message){ logger.debug(label, message); } - public void i(String label, String message){ logger.info(label, message); } - public void w(String label, String message){ logger.warning(label, message); } - public void e(String label, String message){ logger.error(label, message); } + public static void d(String label, String message){ logger.debug(label, message); } + public static void i(String label, String message){ logger.info(label, message); } + public static void w(String label, String message){ logger.warning(label, message); } + public static void e(String label, String message){ logger.error(label, message); } } diff --git a/test/core/src/temalab/logger/Logger.java b/test/core/src/temalab/logger/Logger.java index 6d85552..e87cce8 100644 --- a/test/core/src/temalab/logger/Logger.java +++ b/test/core/src/temalab/logger/Logger.java @@ -1,8 +1,4 @@ package temalab.logger; - -import java.time.LocalDateTime; -import java.time.format.DateTimeFormatter; - public interface Logger { public void debug(String label, String message); public void info(String label, String message); diff --git a/test/core/src/temalab/logger/LoggerBase.java b/test/core/src/temalab/logger/LoggerBase.java index 7910373..ae8c1ac 100644 --- a/test/core/src/temalab/logger/LoggerBase.java +++ b/test/core/src/temalab/logger/LoggerBase.java @@ -1,50 +1,56 @@ package temalab.logger; - import java.io.PrintStream; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; -public class LoggerBase implements Logger { +public abstract class LoggerBase implements Logger { private PrintStream printStream = null; + protected PrintStream getPrintStream() {return printStream;} public void setPrintStream(PrintStream p){ - printStream.close(); //TODO + printStream.close(); //TODO Cleaner printStream = p; } - protected void getInfos(String level, String label, String message) { + public LoggerBase(){ + printStream = System.err; + } + + protected String getInfos(String level) { + String res = ""; + String timeOfLog = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); String header = String.format("[%s - %s]", timeOfLog, level); - String className = Thread.currentThread().getStackTrace()[1].getClass().getName(); //caller.getClass().getPackage().getName(); - String function = Thread.currentThread().getStackTrace()[1].getMethodName() + "()"; + String className = getCallerClass(); + String function = String.format("%s()", getCallerMethod()); - this.printStream.print(String.format("%-35s\t%-10s\t%-10s", header, className, function)); - this.printStream.println(String.format("%s - %s", label, message)); + res += String.format("%-35s\t%-10s\t%-10s", header, className, function); + return res; } - - @Override - public void debug(String label, String message) { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'debug'"); + //TODO this should be overriden in each implementation + public String getCallerClass(){ + //0 - getStackTrace() + //1 - getCallerClass() + //2 - getInfos() + //3 - the logger class + //4 - Log class + //5 - the actual caller + return Thread.currentThread().getStackTrace()[5].getClassName(); } - @Override - public void info(String label, String message) { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'info'"); + public String getCallerMethod(){ + return Thread.currentThread().getStackTrace()[5].getMethodName(); } + //TODO any better way to do it? @Override - public void warning(String label, String message) { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'warning'"); - } - + public void debug(String label, String message){} @Override - public void error(String label, String message) { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'error'"); - } + public void info(String label, String message){} + @Override + public void warning(String label, String message){} + @Override + public void error(String label, String message){} }