Skip to content

Commit

Permalink
Avoid biz logger context being attached early,support for logback (#816)
Browse files Browse the repository at this point in the history
* Avoid biz logger context being attached early,support for logback

* format

* fix ut

---------

Co-authored-by: yuanyuan <fengjun.zfj@antgroup.com>
  • Loading branch information
yuanyuancin and yuanyuan2021 committed Dec 27, 2023
1 parent bb17be2 commit 3fca4a6
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@
*/
package com.alipay.sofa.ark.common.adapter;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.selector.ContextSelector;
import ch.qos.logback.classic.spi.LoggerContextListener;
import com.alipay.sofa.ark.common.util.StringUtils;

import java.util.HashMap;
Expand Down Expand Up @@ -86,7 +83,6 @@ private LoggerContext getContext(ClassLoader cls) {
if (null == loggerContext) {
loggerContext = new LoggerContext();
loggerContext.setName(Integer.toHexString(System.identityHashCode(cls)));
loggerContext.addListener(new ArkLoggerContextListener(this));
CLASS_LOADER_LOGGER_CONTEXT.put(cls, loggerContext);
}
}
Expand Down Expand Up @@ -138,38 +134,4 @@ public LoggerContext removeContext(ClassLoader cls) {
public List<String> getContextNames() {
return CLASS_LOADER_LOGGER_CONTEXT.values().stream().map(LoggerContext::getName).collect(Collectors.toList());
}

public static class ArkLoggerContextListener implements LoggerContextListener {

private ArkLogbackContextSelector contextSelector;

public ArkLoggerContextListener(ArkLogbackContextSelector contextSelector) {
this.contextSelector = contextSelector;
}

@Override
public boolean isResetResistant() {
return true;
}

@Override
public void onStart(LoggerContext context) {

}

@Override
public void onReset(LoggerContext context) {

}

@Override
public void onStop(LoggerContext context) {
contextSelector.detachLoggerContext(context.getName());
}

@Override
public void onLevelChange(Logger logger, Level level) {

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ public void testContextSelector() throws NoSuchMethodException, InvocationTarget
Assert.assertNotNull(invoke);
Assert.assertEquals(invoke, contextSelector.getLoggerContext(contextName));
Assert.assertTrue(contextSelector.getContextNames().contains(contextName));
contextSelector.getLoggerContext(contextName).stop();
Assert.assertTrue(contextSelector.getContextNames().isEmpty());

}
}

0 comments on commit 3fca4a6

Please sign in to comment.