Details
-
Improvement
-
Resolution: Unresolved
-
Minor
-
None
-
1.3.0-alpha0
-
operating system:windows
softwar platform:eclipse
Description
the logback do not have advice to enhance its input.for example:I want to add some message
to all the logging message,their is no way to do so.
I think it can be improve like this:
1.add class called AdviceModel.java in package ch.qos.logback.classic
public class AdviceModel {
private String msg;
private String FQCN;
private Level level;
private Throwable throwable;
}
2.add the interface in package ch.qos.logback.classic
public interface ILogbackAdvice {
default AdviceModel before(String localFQCN,String msg,Level level){
return null;
}
default void after(String localFQCN,String msg,Level level){
}
default AdviceModel before(String localFQCN,String msg,Level level,Throwable t){
return null;
}
default void after(String localFQCN,String msg,Level level,Throwable t){
}
}
3.modify the Logger.java like this:
add field:
private Map<ClassLoader,ILogbackAdvice> adviceMap = new ConcurrentHashMap<>();
add method:
private ILogbackAdvice getAdvice(){
if(adviceMap != null){
try {
ILogbackAdvice adviceClassObject = adviceMap.get(Thread.currentThread().getContextClassLoader());
if(adviceClassObject == null){
Class<?> adviceClass = Thread.currentThread().getContextClassLoader().loadClass("LogbackAdvice");
adviceClassObject = (ILogbackAdvice)adviceClass.newInstance();
adviceMap.put(Thread.currentThread().getContextClassLoader(), adviceClassObject);
}
return adviceClassObject;
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
// TODO Auto-generated catch block
//there is no need for advicing do nothing
adviceMap = null;
}
}
return null;
}
then modify the method like error:
public void error(String msg) {
String fqcn = FQCN;
Level level = Level.ERROR;
String msgS = msg;
ILogbackAdvice adviceClassObject = getAdvice();
if(adviceClassObject != null){
AdviceModel model = adviceClassObject.before(FQCN,msg,Level.ERROR);
if(model != null){
String fqcns = model.getFQCN();
Level levels = model.getLevel();
String msgs = model.getMsg();
fqcn = fqcns != null ? fqcns:fqcn;
level = levels != null ? levels:level;
msgS = msgs != null ? msgs:msgS;
}
}
filterAndLog_0_Or3Plus(fqcn, null, level, msgS, null, null);
if(adviceClassObject != null){
adviceClassObject.after(FQCN,msg,Level.ERROR);
}
}
I want to improve the logback and commit the branch fixed by me,Please Let me know if you think this improvement is ok(of course I used java8 version features,if i commit the code,i will use java6 version features).