Fix: debug level and log on stdout are not the same

This commit is contained in:
Laurent Clouet
2024-05-05 10:55:46 +00:00
parent 5aaa88cf1a
commit 96cca24990
5 changed files with 39 additions and 10 deletions

View File

@@ -33,6 +33,10 @@ import java.util.Map;
import java.util.Optional;
public class Log {
private final String LEVEL_DEBUG = "debug";
private final String LEVEL_INFO = "info";
private final String LEVEL_ERROR = "error";
private static Log instance = null;
private Map<Integer, ArrayList<String>> checkpoints = new HashMap<Integer, ArrayList<String>>();
@@ -40,10 +44,12 @@ public class Log {
private DateFormat dateFormat;
private boolean printStdOut;
private boolean debugLevel;
private String logFile;
private Log(boolean print_, String logDirectory_) {
private Log(boolean debugLevel_, boolean print_, String logDirectory_) {
this.debugLevel = debugLevel_;
this.printStdOut = print_;
this.logFile = logDirectory_ + File.separator + "sheepit.log";
this.lastCheckPoint = 0;
@@ -56,7 +62,7 @@ public class Log {
}
public void debug(int point_, String msg_) {
this.append(point_, "debug", msg_);
this.append(point_, LEVEL_DEBUG, msg_);
}
public void info(String msg_) {
@@ -64,7 +70,7 @@ public class Log {
}
public void info(int point_, String msg_) {
this.append(point_, "info", msg_);
this.append(point_, LEVEL_INFO, msg_);
}
public void error(String msg_) {
@@ -72,10 +78,13 @@ public class Log {
}
public void error(int point_, String msg_) {
this.append(point_, "error", msg_);
this.append(point_, LEVEL_ERROR, msg_);
}
private synchronized void append(int point_, String level_, String msg_) {
if (LEVEL_DEBUG.equals(level_) && this.debugLevel == false) {
return;
}
String line = null;
try {
@@ -128,12 +137,14 @@ public class Log {
if (instance == null) {
String path = null;
boolean print = false;
boolean debug = false;
if (config != null) {
debug = config.isDebugLevel();
print = config.isPrintLog();
path = config.getLogDirectory() == null ? null : config.getLogDirectory();
}
instance = new Log(print, path);
instance = new Log(debug, print, path);
}
return instance;
}