[logback-dev] [JIRA] (LOGBACK-1290) Logback don't have advice

QOS.CH (JIRA) noreply-jira at qos.ch
Sat Mar 25 08:10:00 CET 2017


xiaohu-zhang created LOGBACK-1290:
-------------------------------------

             Summary: Logback don't have advice 
                 Key: LOGBACK-1290
                 URL: https://jira.qos.ch/browse/LOGBACK-1290
             Project: logback
          Issue Type: Improvement
          Components: logback-classic
    Affects Versions: 1.3.0
         Environment: operating system:windows 

softwar platform:eclipse
            Reporter: xiaohu-zhang
            Assignee: Logback dev list
            Priority: Minor
         Attachments: AdviceModel.java, ILogbackAdvice.java, Logger.java

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).

 



--
This message was sent by Atlassian JIRA
(v7.3.1#73012)


More information about the logback-dev mailing list