Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Silencing log4j StatusLogger during initialization #1322

Merged
merged 4 commits into from Aug 4, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -26,7 +26,9 @@

import co.elastic.apm.agent.configuration.converter.ByteValue;
import co.elastic.apm.agent.configuration.converter.ByteValueConverter;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.config.Configurator;
import org.apache.logging.log4j.status.StatusLogger;
import org.stagemonitor.configuration.ConfigurationOption;
import org.stagemonitor.configuration.ConfigurationOptionProvider;
import org.stagemonitor.configuration.source.ConfigurationSource;
Expand Down Expand Up @@ -195,7 +197,23 @@ public void assertValid(Boolean value) {
.buildWithDefault(LogFormat.PLAIN_TEXT);

public static void init(List<ConfigurationSource> sources, String ephemeralId) {
Configurator.initialize(new Log4j2ConfigurationFactory(sources, ephemeralId).getConfiguration());
// The initialization of log4j may produce errors if the traced application uses log4j settings (for
// example - through file in the classpath or System properties) that configures specific properties for
// loading classes by name. Since we shade our usage of log4j, such non-shaded classes may not (and should not)
// be found on the classpath.
// All handled Exceptions should not prevent us from using log4j further, as the system falls back to a default
// which we expect anyway. We take a calculated risk of ignoring such errors only through initialization time,
// assuming that errors that will make the logging system non-usable won't be handled.
System.setProperty("log4j2.StatusLogger.level", "OFF");
System.setProperty("org.apache.logging.log4j.simplelog.StatusLogger.level", "OFF");
System.setProperty("Log4jDefaultStatusLevel", "OFF");
try {
Configurator.initialize(new Log4j2ConfigurationFactory(sources, ephemeralId).getConfiguration());
} catch (Throwable throwable) {
System.err.println("Failure during initialization of agent's log4j system: " + throwable.getMessage());
} finally {
StatusLogger.getLogger().setLevel(Level.ERROR);
eyalkoren marked this conversation as resolved.
Show resolved Hide resolved
}
}

public String getLogFile() {
Expand Down