I've tried to start two Spring applications in the same JVM which worked fine except that now every log line is written twice, so in case of log line that are written for both applications are written four times effectivly.
I've also tried to see what appenders are attached to the loggers an I got STDOUT
two times in it which would explain the behaviour.
It seems like starting spring twice ends up initializing logback twice without resetting the first initialization.
Is there some way to suppress this behaviour? I didn't even think that spring would manually initialize it.
The code I used to start the two applications (Main
and SecondaryMain
are in two indepenant packages, neither contained in the other):
public static void main(String[] args) throws Exception
{
run("App1", Main.class, args);
run("App2", SecondMain.class, args);
}
private static void run(String name, Class<?> clazz, String[] args)
{
SpringApplication run = new SpringApplication(clazz);
ThreadGroup threadGroup = new ThreadGroup(name);
Thread t = new Thread(threadGroup, () -> {run.run(args);}, name+".Main");
t.start();
}
My logback configuration (nothing special):
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{yyyy-MM-dd' 'HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="info">
<appender-ref ref="STDOUT" />
</root>
</configuration>
In the meanwhile I've found two ways to accomplish it
disabling logging management in spring
One can specify the logging system that spring should use but there's also the option none
so it won't set up any itself and that way also not twice
The con is that it won't integrate logging, in my case some components that used to log to logback suddenly didn't.
using a parent context
If both applications share a parent contract spring will only initialize logging once
This seems to be the cleaner solution and I haven't seen a con to it yet