-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: added basic console implementation
- Loading branch information
1 parent
4ef5466
commit a735485
Showing
4 changed files
with
80 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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){} | ||
|
||
} |