GUACAMOLE-2181: Pre-initialize logging such that format is always consistent.

This commit is contained in:
Michael Jumper committed 2025-12-12 10:42:29 -08:00
1 parent 4098e76801
commit 42f450315e
1 file changed
+26
@@ -19,11 +19,17 @@
package org.apache.guacamole;
import ch.qos.logback.core.joran.spi.JoranException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Set;
import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import org.apache.guacamole.log.GuacamoleLogbackServiceProvider;
import org.apache.guacamole.log.LogLevel;
import org.apache.guacamole.log.ReconfigurableLoggerFactory;
import org.slf4j.ILoggerFactory;
import org.slf4j.LoggerFactory;
import org.slf4j.helpers.Reporter;
@@ -38,6 +44,12 @@ import org.slf4j.helpers.Reporter;
*/
public class GuacamoleServletContainerInitializer implements ServletContainerInitializer {
/**
* The log level that should be used when we have not yet loaded any
* configuration dictating otherwise.
*/
private static final LogLevel EARLY_LOG_LEVEL = LogLevel.INFO;
@Override
public void onStartup(Set<Class<?>> c, ServletContext ctx) throws ServletException {
@@ -59,6 +71,20 @@ public class GuacamoleServletContainerInitializer implements ServletContainerIni
}
// Attempt early initialization of Logback (before we begin parsing
// Guacamole's configuration), such that even startup messages follow
// a consistent format
try (InputStream logbackConfiguration = EARLY_LOG_LEVEL.getLogbackConfiguration()) {
ILoggerFactory loggerFactory = LoggerFactory.getILoggerFactory();
if (loggerFactory instanceof ReconfigurableLoggerFactory)
((ReconfigurableLoggerFactory) loggerFactory).reconfigure(logbackConfiguration);
}
catch (JoranException | IOException e) {
System.err.println("Early initialization of Logback failed - "
+ "logging format may not match until the web application "
+ "finishes starting up: " + e.getMessage());
}
}
}