This commit is contained in:
Olivier Duval 2021-02-11 19:58:34 +01:00
parent 378f785535
commit 9a909c4e69
5 changed files with 124 additions and 29 deletions

2
llog.properties Normal file
View File

@ -0,0 +1,2 @@
=INFO, WARN
fr.doap=DEBUG

View File

@ -1,5 +1,8 @@
package fr.doap.llog;
import java.text.DateFormat;
import java.util.Date;
public interface ILogger {
public void logHead(LogMsg aLogMsg, StackTraceElement aSTE);
public void logCauseHead(LogMsg aLogMsg, Throwable aThrowable);
@ -10,9 +13,10 @@ public interface ILogger {
static public class Console implements ILogger {
@Override
public void logHead(LogMsg aLogMsg, StackTraceElement aSTE) {
String lC = aSTE.getClassName()+"."+aSTE.getMethodName();
if (aLogMsg.mSeverity >= L.ERROR) System.err.println(aLogMsg.mTag+"/"+L.getSeverity(aLogMsg.mSeverity)+ " - " + lC + ": " + aLogMsg.mMessage + " (" + aSTE.getFileName() + ":" + aSTE.getLineNumber() + ")");
else System.out.println(aLogMsg.mTag+"/"+L.getSeverity(aLogMsg.mSeverity)+ " - " + lC + ": " + aLogMsg.mMessage + " (" + aSTE.getFileName() + ":" + aSTE.getLineNumber() + ")");
String lC = aSTE.getClassName()+" @"+aSTE.getMethodName();
String lP = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM).format(new Date(aLogMsg.mTime))+" "+aLogMsg.mCallerThread.getName()+" "+L.getSeverity(aLogMsg.mSeverity).substring(0,1)+"/"+aLogMsg.mTag;
if (aLogMsg.mSeverity >= L.ERROR) System.err.println(lP+ " - " + lC + ": " + aLogMsg.mMessage + " (" + aSTE.getFileName() + ":" + aSTE.getLineNumber() + ")");
else System.out.println(lP+ " - " + lC + ": " + aLogMsg.mMessage + " (" + aSTE.getFileName() + ":" + aSTE.getLineNumber() + ")");
}
@Override

View File

@ -1,5 +1,8 @@
package fr.doap.llog;
import java.io.*;
import java.util.Properties;
public class L {
static final public int ALL = 0;
static final public int TRACE = 100;
@ -16,13 +19,17 @@ public class L {
mLooper.put(aKey,aFilter);
}
static public void setLogger(ILogger aLogger) {
mLooper.setLogger(aLogger);
}
static public String getSeverity(int aSeverity) {
switch(aSeverity) {
case TRACE: return "TRACE";
case VERBOSE: return "VERBOSE";
case DEBUG: return "DEBUG";
case CONFIG: return "CONFIG";
case INFO: return "INFO";
case CONFIG: return "CONFIG";
case WARN: return "WARN";
case ERROR: return "ERROR";
case FATAL: return "FATAL";
@ -30,10 +37,64 @@ public class L {
return "?";
}
static public LogMsg l(int aSeverity, Throwable aThrowable, String aTag, String aMsg, Object ... aArgs) {
static public int getSeverity(String aSeverity) {
switch(aSeverity) {
case "ALL": return ALL;
case "TRACE": return TRACE;
case "VERBOSE": return VERBOSE;
case "DEBUG": return DEBUG;
case "INFO": return INFO;
case "CONFIG": return CONFIG;
case "WARN": return WARN;
case "ERROR": return ERROR;
case "FATAL": return FATAL;
case "OFF": return OFF;
}
return -1;
}
static public void t(Throwable aThrowable, String aMsg, Object ... aArgs) { l(TRACE, aThrowable, aMsg, aArgs); }
static public void t(Throwable aThrowable) { l(TRACE, aThrowable, null); }
static public void t(String aMsg, Object ... aArgs) { l(TRACE, null, aMsg, aArgs); }
static public void v(Throwable aThrowable, String aMsg, Object ... aArgs) { l(VERBOSE, aThrowable, aMsg, aArgs); }
static public void v(Throwable aThrowable) { l(VERBOSE, aThrowable, null); }
static public void v(String aMsg, Object ... aArgs) { l(VERBOSE, null, aMsg, aArgs); }
static public void d(Throwable aThrowable, String aMsg, Object ... aArgs) { l(DEBUG, aThrowable, aMsg, aArgs); }
static public void d(Throwable aThrowable) { l(DEBUG, aThrowable, null); }
static public void d(String aMsg, Object ... aArgs) { l(DEBUG, null, aMsg, aArgs); }
static public void i(Throwable aThrowable, String aMsg, Object ... aArgs) { l(INFO, aThrowable, aMsg, aArgs); }
static public void i(Throwable aThrowable) { l(INFO, aThrowable, null); }
static public void i(String aMsg, Object ... aArgs) { l(INFO, null, aMsg, aArgs); }
static public void c(Throwable aThrowable, String aMsg, Object ... aArgs) { l(CONFIG, aThrowable, aMsg, aArgs); }
static public void c(Throwable aThrowable) { l(CONFIG, aThrowable, null); }
static public void c(String aMsg, Object ... aArgs) { l(CONFIG, null, aMsg, aArgs); }
static public void w(Throwable aThrowable, String aMsg, Object ... aArgs) { l(WARN, aThrowable, aMsg, aArgs); }
static public void w(Throwable aThrowable) { l(WARN, aThrowable, null); }
static public void w(String aMsg, Object ... aArgs) { l(WARN, null, aMsg, aArgs); }
static public void e(Throwable aThrowable, String aMsg, Object ... aArgs) { l(ERROR, aThrowable, aMsg, aArgs); }
static public void e(Throwable aThrowable) { l(ERROR, aThrowable, null); }
static public void e(String aMsg, Object ... aArgs) { l(ERROR, null, aMsg, aArgs); }
static public void f(Throwable aThrowable, String aMsg, Object ... aArgs) { l(FATAL, aThrowable, aMsg, aArgs); }
static public void f(Throwable aThrowable) { l(FATAL, aThrowable, null); }
static public void f(String aMsg, Object ... aArgs) { l(FATAL, null, aMsg, aArgs); }
static public LogMsg l(int aSeverity, Throwable aThrowable, String aMsg, Object ... aArgs) {
String lMsg = aMsg;
if (aArgs != null && aArgs.length > 0) lMsg = String.format(aMsg, aArgs);
LogMsg lLogMsg = new LogMsg(aSeverity, aThrowable, aTag, lMsg);
String lTag = null;
if (lMsg.startsWith("#")) {
String lP[] = lMsg.split("[ :,]+", 2);
lTag = lP[0].substring(1);
lMsg = lP[1];
}
LogMsg lLogMsg = new LogMsg(aSeverity, aThrowable, lTag, lMsg);
mLooper.post(lLogMsg);
return lLogMsg;
}

View File

@ -1,9 +1,14 @@
package fr.doap.llog;
import java.util.Set;
import java.io.*;
import java.util.*;
public class LogLooper extends ThreadLooper {
public LogLooper() {
reloadConfig();
}
public void post(LogMsg aLogMsg) {
add(new LLMsg(aLogMsg));
}
@ -12,6 +17,10 @@ public class LogLooper extends ThreadLooper {
mFilters.put(aKey, aF);
}
public void setLogger(ILogger aLogger) {
mLogger = aLogger;
}
public class LLMsg implements WorkItem {
public LLMsg(LogMsg aLogMsg) { mLogMsg = aLogMsg; }
@ -20,7 +29,7 @@ public class LogLooper extends ThreadLooper {
int lI = mLogMsg.getCallerIndex();
StackTraceElement lS = mLogMsg.mSTE[lI];
String lClass = lS.getClassName();
lClass = lClass.substring(lClass.lastIndexOf('.'));
lClass = lClass.substring(lClass.lastIndexOf('.')+1);
if (mLogMsg.mTag == null || mLogMsg.mTag.isEmpty()) mLogMsg.mTag = lClass;
String lC = lS.getClassName() + "." + lS.getMethodName();
@ -56,6 +65,27 @@ public class LogLooper extends ThreadLooper {
private LogMsg mLogMsg;
}
public void reloadConfig() {
Properties lProp = new Properties();
File lF = new File("llog.properties");
if (lF.exists()) {
try {
InputStream is = new FileInputStream(lF);
lProp.load(is);
for (String k : lProp.stringPropertyNames()) {
String v = lProp.getProperty(k);
String lv[] = v.split(",");
int lLevel = L.getSeverity(lv[0].trim().toUpperCase());
int lST = L.ERROR;
if (lv.length > 1) lST = L.getSeverity(lv[1].trim().toUpperCase());
put(k.trim(), new Filter(lLevel, lST));
}
} catch (Exception e) {
System.out.println("Problem in file " + lF.getAbsolutePath()+": "+e.getMessage());
}
} else System.out.println("Can't load file " + lF.getAbsolutePath());
}
private Dict<Filter> mFilters = new Dict<>();
private ILogger mLogger = ILogger.CONSOLE;
}

View File

@ -17,35 +17,33 @@ public class Test {
protected static void testDict() {
Dict<String> lD = new Dict<>();
L.l(1, null, "Dict", "Empty: %s", lD.toString());
L.i("Empty: %s", lD.toString());
lD.put("fr.doap", "fr_doap");
L.l(1, null, "Dict", "1: %s", lD.toString());
L.i("1: %s", lD.toString());
lD.put("com.doap.fr", "com_doap_fr");
L.l(1, null, "Dict", "2: %s", lD.toString());
L.i("2: %s", lD.toString());
lD.put("com", "com");
L.l(1, null, "Dict", "3: %s", lD.toString());
L.i("3: %s", lD.toString());
lD.put("fr.doap.llog", "fr_doap_llog");
L.l(1, null, "Dict", "4: %s", lD.toString());
L.i("4: %s", lD.toString());
lD.put("", "ROOT");
L.l(1, null, "Dict", "5: %s", lD.toString());
L.i("5: %s", lD.toString());
lD.put("com.doap.fr", "com_doap_fr+++");
L.l(1, null, "Dict", "6: %s", lD.toString());
L.l(1, null, "Dict", "fr.doap.llog: %s", lD.get("fr.doap.llog"));
L.l(1, null, "Dict", "fr: %s", lD.get("fr"));
L.l(1, null, "Dict", "com.doap.fr.toto: %s", lD.get("com.doap.fr.toto"));
L.l(1, null, "Dict", "com.bibi: %s", lD.get("com.bibi"));
L.l(1, null, "Dict", "org.tata: %s", lD.get("org.tata"));
L.l(1, null, "Dict", "com.doap: %s", lD.get("com.doap"));
L.l(1, null, "Dict", "com.doap.bibi: %s", lD.get("com.doap.bibi"));
L.i("6: %s", lD.toString());
L.i("fr.doap.llog: %s", lD.get("fr.doap.llog"));
L.i("fr: %s", lD.get("fr"));
L.i("com.doap.fr.toto: %s", lD.get("com.doap.fr.toto"));
L.i("com.bibi: %s", lD.get("com.bibi"));
L.i("org.tata: %s", lD.get("org.tata"));
L.i("com.doap: %s", lD.get("com.doap"));
L.i("com.doap.bibi: %s", lD.get("com.doap.bibi"));
}
protected static void testFilter() {
Filter lF = new Filter(L.DEBUG, L.WARN);
L.filter("", lF);
L.l(L.ERROR, null, "Dict", "Always show with ST");
L.l(L.INFO, null, "Dict", "Always show");
L.l(L.DEBUG, null, "Dict", "Always");
L.l(L.TRACE, null, "Dict", "Not showing");
L.l(L.CONFIG, null, "Dict", "Finished");
L.e("Always show with ST");
L.i("#tag Always show");
L.d("Always %s", "toto");
L.t("Not showing");
L.c(new Throwable("Thow here"), "#config Finished");
}
}