These kind of issues are very often due to class-loading difference and in this case the difference is non trivial.
During this initialization phase, the LogManager
static initializer is called when the class is firstly loaded. If you look inside the static initializer you will see:
Enumeration<URL> enumResources = null;
try {
enumResources = cl.getResources(LOGGER_RESOURCE);
} catch (IOException e) {
logger.fatal("Unable to locate " + LOGGER_RESOURCE, e);
}
Later in the code, you will see a loop over the enum resources to create the logger context factory.
However, when you run the Scala class enumResources.hasMoreElements()
returns true, while when you run the java class it returns false (so no logger context and no loggers are added at all to the LogManager).
If you investigate further, you will see that the cl
variable is in fact a class loader, which in case of the Java class is an instance of sun.misc.Launcher$AppClassLoader
while for the Scala class is an instance sbt.classpath.ClasspathUtilities$$anon$1
If you look at the beginning of the static initializer, you will see that the following statement:
static {
// Shortcut binding to force a specific logging implementation.
PropsUtil managerProps = new PropsUtil("log4j2.LogManager.properties");
String factoryClass = managerProps.getStringProperty(FACTORY_PROPERTY_NAME);
ClassLoader cl = findClassLoader();
So you might want to have a look to the findClassLoader()
method:
private static ClassLoader findClassLoader() {
ClassLoader cl;
if (System.getSecurityManager() == null) {
cl = Thread.currentThread().getContextClassLoader();
} else {
cl = java.security.AccessController.doPrivileged(
new java.security.PrivilegedAction<ClassLoader>() {
public ClassLoader run() {
return Thread.currentThread().getContextClassLoader();
}
}
);
}
if (cl == null) {
cl = LogManager.class.getClassLoader();
}
return cl;
}
In both case, since the SecurityManager is not null, it returns the current Thread context class loader. Which is different for your Java class and your Scala class.