diff options
author | antoine.vinot <antoine.vinot@sonarsource.com> | 2023-12-12 09:07:34 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2023-12-19 20:02:55 +0000 |
commit | 6a2f77f0e34e09ddd7202e92e685948229570f3f (patch) | |
tree | 676d6a86b5a26dfc936359ee6f6c53b75425a691 /server/sonar-server-common | |
parent | 7fe24812e4dccac7aebff9b2c5109727a76af56c (diff) | |
download | sonarqube-6a2f77f0e34e09ddd7202e92e685948229570f3f.tar.gz sonarqube-6a2f77f0e34e09ddd7202e92e685948229570f3f.zip |
SONAR-21227 Configure a new Logger file for deprecated API usages
Diffstat (limited to 'server/sonar-server-common')
2 files changed, 71 insertions, 8 deletions
diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/log/ServerProcessLogging.java b/server/sonar-server-common/src/main/java/org/sonar/server/log/ServerProcessLogging.java index c3d6cd21be7..092881f49b0 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/log/ServerProcessLogging.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/log/ServerProcessLogging.java @@ -59,7 +59,7 @@ public abstract class ServerProcessLogging { private final ProcessId processId; private final String threadIdFieldPattern; - private final LogbackHelper helper = new LogbackHelper(); + protected final LogbackHelper helper = new LogbackHelper(); private final LogLevelConfig logLevelConfig; protected ServerProcessLogging(ProcessId processId, String threadIdFieldPattern) { @@ -122,7 +122,7 @@ public abstract class ServerProcessLogging { configureRootLogger(props); helper.apply(logLevelConfig, props); configureDirectToConsoleLoggers(props, ctx, STARTUP_LOGGER_NAME); - extendConfigure(); + extendConfigure(props); helper.enableJulChangePropagation(ctx); @@ -135,21 +135,25 @@ public abstract class ServerProcessLogging { protected abstract void extendLogLevelConfiguration(LogLevelConfig.Builder logLevelConfigBuilder); - protected abstract void extendConfigure(); + protected abstract void extendConfigure(Props props); private void configureRootLogger(Props props) { - RootLoggerConfig config = newRootLoggerConfigBuilder() + RootLoggerConfig config = buildRootLoggerConfig(props); + Encoder<ILoggingEvent> encoder = helper.createEncoder(props, config, helper.getRootContext()); + helper.configureGlobalFileLog(props, config, encoder); + helper.configureForSubprocessGobbler(props, encoder); + } + + protected RootLoggerConfig buildRootLoggerConfig(Props props) { + return newRootLoggerConfigBuilder() .setProcessId(processId) .setNodeNameField(getNodeNameWhenCluster(props)) .setThreadIdFieldPattern(threadIdFieldPattern) .build(); - Encoder<ILoggingEvent> encoder = helper.createEncoder(props, config, helper.getRootContext()); - helper.configureGlobalFileLog(props, config, encoder); - helper.configureForSubprocessGobbler(props, encoder); } @CheckForNull - private static String getNodeNameWhenCluster(Props props) { + protected static String getNodeNameWhenCluster(Props props) { boolean clusterEnabled = props.valueAsBoolean(CLUSTER_ENABLED.getKey(), Boolean.parseBoolean(CLUSTER_ENABLED.getDefaultValue())); return clusterEnabled ? props.value(CLUSTER_NODE_NAME.getKey(), CLUSTER_NODE_NAME.getDefaultValue()) : null; diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/log/ServerProcessLoggingTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/log/ServerProcessLoggingTest.java new file mode 100644 index 00000000000..a61333a30ef --- /dev/null +++ b/server/sonar-server-common/src/test/java/org/sonar/server/log/ServerProcessLoggingTest.java @@ -0,0 +1,59 @@ +/* + * SonarQube + * Copyright (C) 2009-2023 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.server.log; + +import org.junit.Test; +import org.mockito.Mockito; +import org.sonar.process.ProcessId; +import org.sonar.process.Props; +import org.sonar.process.logging.LogLevelConfig; +import org.sonar.process.logging.RootLoggerConfig; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.sonar.process.ProcessId.WEB_SERVER; +import static org.sonar.process.logging.RootLoggerConfig.newRootLoggerConfigBuilder; + +public class ServerProcessLoggingTest { + + @Test + public void buildRootLoggerConfig_shouldBuildConfig() { + ServerProcessLogging serverProcessLogging = getServerProcessLoggingFakeImpl(WEB_SERVER, "threadIdFieldPattern"); + Props props = Mockito.mock(Props.class); + RootLoggerConfig expected = newRootLoggerConfigBuilder() + .setProcessId(WEB_SERVER) + .setNodeNameField(null) + .setThreadIdFieldPattern("threadIdFieldPattern") + .build(); + + RootLoggerConfig result = serverProcessLogging.buildRootLoggerConfig(props); + + assertThat(result).usingRecursiveComparison().isEqualTo(expected); + } + + private ServerProcessLogging getServerProcessLoggingFakeImpl(ProcessId processId, String threadIdFieldPattern) { + return new ServerProcessLogging(processId, threadIdFieldPattern) { + @Override + protected void extendLogLevelConfiguration(LogLevelConfig.Builder logLevelConfigBuilder) {} + + @Override + protected void extendConfigure(Props props) {} + }; + } +} |