Skip to content

Commit

Permalink
Use caller classloader as default (rabbitmq#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
caojie09 authored Feb 18, 2020
1 parent f256831 commit 27e6d83
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 9 deletions.
2 changes: 1 addition & 1 deletion core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>sofa-common-tools-parent</artifactId>
<groupId>com.alipay.sofa.common</groupId>
<version>1.0.20</version>
<version>1.0.21-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.alipay.sofa.common.log;

import com.alipay.sofa.common.log.adapter.level.AdapterLevel;
import com.alipay.sofa.common.utils.ClassLoaderUtil;
import org.slf4j.ILoggerFactory;
import org.slf4j.Logger;

Expand All @@ -39,8 +40,8 @@ public class LoggerSpaceManager {
* @return org.slf4j.Logger;
*/
public static Logger getLoggerBySpace(String name, String spaceName) {
return getLoggerBySpace(name, new SpaceId(spaceName),
Collections.<String, String> emptyMap());
ClassLoader callerClassLoader = ClassLoaderUtil.getCallerClassLoader();
return getLoggerBySpace(name, spaceName, callerClassLoader);
}

/**
Expand All @@ -52,9 +53,8 @@ public static Logger getLoggerBySpace(String name, String spaceName) {
*/
public static Logger getLoggerBySpace(String name, SpaceId spaceId,
Map<String, String> properties) {
//init first
init(spaceId, properties);
return MultiAppLoggerSpaceManager.getLoggerBySpace(name, spaceId);
ClassLoader callerClassLoader = ClassLoaderUtil.getCallerClassLoader();
return getLoggerBySpace(name, spaceId, properties, callerClassLoader);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ public static Class loadClass(String className, Class referrer) throws ClassNotF
return loadClass(className, classLoader);
}

public static ClassLoader getCallerClassLoader() {
try {
String callerClassName = Thread.currentThread().getStackTrace()[3].getClassName();
return loadClass(callerClassName).getClassLoader();
} catch (Throwable t) {
throw new RuntimeException("Failed to get caller classloader ", t);
}
}

public static Class<?> loadClass(String className, ClassLoader classLoader)
throws ClassNotFoundException {
if (className == null) {
Expand Down
2 changes: 1 addition & 1 deletion log-sofa-boot-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>sofa-common-tools-parent</artifactId>
<groupId>com.alipay.sofa.common</groupId>
<version>1.0.20</version>
<version>1.0.21-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.alipay.sofa.common</groupId>
<artifactId>sofa-common-tools-parent</artifactId>
<version>1.0.20</version>
<version>1.0.21-SNAPSHOT</version>
<modules>
<module>core</module>
<module>log-sofa-boot-starter</module>
Expand All @@ -26,7 +26,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<logback.version>1.1.7</logback.version>
<slf4j.version>1.7.21</slf4j.version>
<common.tools.version>1.0.20</common.tools.version>
<common.tools.version>1.0.21-SNAPSHOT</common.tools.version>
</properties>

<dependencyManagement>
Expand Down

0 comments on commit 27e6d83

Please sign in to comment.