aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>2016-08-12 10:32:57 +0200
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>2016-08-16 08:20:15 +0200
commit3736b3d7d2d70fd44deaf9f89dfe0c4c048b4bd6 (patch)
tree155686726f8d7a19b421d004041a8be26e562243
parent67a6256bb3d06161f19321d42365761c5269c109 (diff)
downloadsonarqube-3736b3d7d2d70fd44deaf9f89dfe0c4c048b4bd6.tar.gz
sonarqube-3736b3d7d2d70fd44deaf9f89dfe0c4c048b4bd6.zip
SONAR-7833 ce activity logs in sonar.log if level is DEBUG or TRACE
-rw-r--r--server/sonar-server/src/main/java/org/sonar/ce/log/CeActivityLogAcceptFilter.java7
-rw-r--r--server/sonar-server/src/main/java/org/sonar/ce/log/CeActivityLogConsoleFilter.java (renamed from server/sonar-server/src/main/java/org/sonar/ce/log/CeActivityLogDenyFilter.java)17
-rw-r--r--server/sonar-server/src/main/java/org/sonar/ce/log/CeLogging.java13
-rw-r--r--server/sonar-server/src/main/java/org/sonar/ce/log/CeProcessLogging.java2
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/ExecuteVisitorsStep.java2
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/computation/task/step/ComputationStepExecutor.java2
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/computation/taskprocessor/CeWorkerCallableImpl.java29
-rw-r--r--server/sonar-server/src/test/java/org/sonar/ce/log/CeActivityLogAcceptFilterTest.java33
-rw-r--r--server/sonar-server/src/test/java/org/sonar/ce/log/CeActivityLogConsoleFilterTest.java89
-rw-r--r--server/sonar-server/src/test/java/org/sonar/ce/log/CeActivityLogDenyFilterTest.java62
-rw-r--r--server/sonar-server/src/test/java/org/sonar/ce/log/CeLoggingTest.java90
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/computation/taskprocessor/CeWorkerCallableImplTest.java12
12 files changed, 226 insertions, 132 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/ce/log/CeActivityLogAcceptFilter.java b/server/sonar-server/src/main/java/org/sonar/ce/log/CeActivityLogAcceptFilter.java
index efd9091e231..ab58b01a7dd 100644
--- a/server/sonar-server/src/main/java/org/sonar/ce/log/CeActivityLogAcceptFilter.java
+++ b/server/sonar-server/src/main/java/org/sonar/ce/log/CeActivityLogAcceptFilter.java
@@ -21,7 +21,6 @@ package org.sonar.ce.log;
import ch.qos.logback.core.filter.Filter;
import ch.qos.logback.core.spi.FilterReply;
-import java.util.Objects;
import org.slf4j.MDC;
/**
@@ -31,7 +30,11 @@ public class CeActivityLogAcceptFilter<E> extends Filter<E> {
@Override
public FilterReply decide(E o) {
- return Objects.equals("true", MDC.get(CeLogging.MDC_CE_ACTIVITY_FLAG)) ? FilterReply.ACCEPT : FilterReply.DENY;
+ String ceActivityFlag = MDC.get(CeLogging.MDC_CE_ACTIVITY_FLAG);
+ if (ceActivityFlag != null) {
+ return FilterReply.ACCEPT;
+ }
+ return FilterReply.DENY;
}
}
diff --git a/server/sonar-server/src/main/java/org/sonar/ce/log/CeActivityLogDenyFilter.java b/server/sonar-server/src/main/java/org/sonar/ce/log/CeActivityLogConsoleFilter.java
index b1e361ab71d..584532766d7 100644
--- a/server/sonar-server/src/main/java/org/sonar/ce/log/CeActivityLogDenyFilter.java
+++ b/server/sonar-server/src/main/java/org/sonar/ce/log/CeActivityLogConsoleFilter.java
@@ -19,19 +19,26 @@
*/
package org.sonar.ce.log;
+import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.filter.Filter;
import ch.qos.logback.core.spi.FilterReply;
-import java.util.Objects;
import org.slf4j.MDC;
+import static ch.qos.logback.core.spi.FilterReply.ACCEPT;
+import static ch.qos.logback.core.spi.FilterReply.DENY;
+
/**
- * Ignores Compute Engine activity logs.
+ * Ignores Compute Engine activity logs in console unless level is DEBUG or lower.
*/
-public class CeActivityLogDenyFilter<E> extends Filter<E> {
+public class CeActivityLogConsoleFilter extends Filter<ILoggingEvent> {
@Override
- public FilterReply decide(E o) {
- return Objects.equals("true", MDC.get(CeLogging.MDC_CE_ACTIVITY_FLAG)) ? FilterReply.DENY : FilterReply.ACCEPT;
+ public FilterReply decide(ILoggingEvent o) {
+ String ceActivityFlag = MDC.get(CeLogging.MDC_CE_ACTIVITY_FLAG);
+ if (ceActivityFlag == null || "all".equals(ceActivityFlag)) {
+ return ACCEPT;
+ }
+ return DENY;
}
}
diff --git a/server/sonar-server/src/main/java/org/sonar/ce/log/CeLogging.java b/server/sonar-server/src/main/java/org/sonar/ce/log/CeLogging.java
index 240dbea8121..2f8e4424d6a 100644
--- a/server/sonar-server/src/main/java/org/sonar/ce/log/CeLogging.java
+++ b/server/sonar-server/src/main/java/org/sonar/ce/log/CeLogging.java
@@ -26,6 +26,7 @@ import ch.qos.logback.core.Appender;
import ch.qos.logback.core.FileAppender;
import java.util.function.Supplier;
import org.apache.log4j.MDC;
+import org.sonar.api.utils.log.Logger;
import org.sonar.ce.queue.CeTask;
import org.sonar.process.LogbackHelper;
import org.sonar.process.Props;
@@ -54,19 +55,23 @@ public class CeLogging {
// TODO clear task UUID from MDF (cf. SONAR-7960)
}
- public static void logCeActivity(Runnable logProducer) {
- MDC.put(MDC_CE_ACTIVITY_FLAG, "true");
+ public static void logCeActivity(Logger logger, Runnable logProducer) {
+ MDC.put(MDC_CE_ACTIVITY_FLAG, computeCeActivityFlag(logger));
logProducer.run();
MDC.remove(MDC_CE_ACTIVITY_FLAG);
}
- public static <T> T logCeActivity(Supplier<T> logProducer) {
- MDC.put(MDC_CE_ACTIVITY_FLAG, "true");
+ public static <T> T logCeActivity(Logger logger, Supplier<T> logProducer) {
+ MDC.put(MDC_CE_ACTIVITY_FLAG, computeCeActivityFlag(logger));
T res = logProducer.get();
MDC.remove(MDC_CE_ACTIVITY_FLAG);
return res;
}
+ private static String computeCeActivityFlag(Logger logger) {
+ return logger.isDebugEnabled() || logger.isTraceEnabled() ? "all" : "ce_only";
+ }
+
public static Appender<ILoggingEvent> createCeActivityAppenderConfiguration(LogbackHelper helper, LoggerContext ctx, Props processProps) {
LogbackHelper.RollingPolicy rollingPolicy = helper.createRollingPolicy(ctx, processProps, CE_ACTIVITY_FILE_NAME_PREFIX);
FileAppender<ILoggingEvent> fileAppender = rollingPolicy.createAppender(CE_ACTIVITY_APPENDER_NAME);
diff --git a/server/sonar-server/src/main/java/org/sonar/ce/log/CeProcessLogging.java b/server/sonar-server/src/main/java/org/sonar/ce/log/CeProcessLogging.java
index 301e2400b13..0f7fe744dde 100644
--- a/server/sonar-server/src/main/java/org/sonar/ce/log/CeProcessLogging.java
+++ b/server/sonar-server/src/main/java/org/sonar/ce/log/CeProcessLogging.java
@@ -41,7 +41,7 @@ public class CeProcessLogging extends ServerProcessLogging {
@Override
protected void configureAppenders(String logFormat, LoggerContext ctx, LogbackHelper helper, Props props) {
- ConsoleAppender<ILoggingEvent> consoleAppender = helper.newConsoleAppender(ctx, "CONSOLE", logFormat, new CeActivityLogDenyFilter());
+ ConsoleAppender<ILoggingEvent> consoleAppender = helper.newConsoleAppender(ctx, "CONSOLE", logFormat, new CeActivityLogConsoleFilter());
ctx.getLogger(ROOT_LOGGER_NAME).addAppender(consoleAppender);
ctx.getLogger(ROOT_LOGGER_NAME).addAppender(CeLogging.createCeActivityAppenderConfiguration(helper, ctx, props));
}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/ExecuteVisitorsStep.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/ExecuteVisitorsStep.java
index 4da14fba3f7..d761a418013 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/ExecuteVisitorsStep.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/ExecuteVisitorsStep.java
@@ -51,7 +51,7 @@ public class ExecuteVisitorsStep implements ComputationStep {
public void execute() {
VisitorsCrawler visitorsCrawler = new VisitorsCrawler(visitors);
visitorsCrawler.visit(treeRootHolder.getRoot());
- logCeActivity(() -> logVisitorExecutionDurations(visitors, visitorsCrawler));
+ logCeActivity(LOGGER, () -> logVisitorExecutionDurations(visitors, visitorsCrawler));
}
private static void logVisitorExecutionDurations(List<ComponentVisitor> visitors, VisitorsCrawler visitorsCrawler) {
diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/step/ComputationStepExecutor.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/step/ComputationStepExecutor.java
index b41c1c5e0f1..c066be83496 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/step/ComputationStepExecutor.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/step/ComputationStepExecutor.java
@@ -64,7 +64,7 @@ public final class ComputationStepExecutor {
for (ComputationStep step : steps.instances()) {
stepProfiler.start();
step.execute();
- logCeActivity(() -> stepProfiler.stopInfo(step.getDescription()));
+ logCeActivity(LOGGER, () -> stepProfiler.stopInfo(step.getDescription()));
}
}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/taskprocessor/CeWorkerCallableImpl.java b/server/sonar-server/src/main/java/org/sonar/server/computation/taskprocessor/CeWorkerCallableImpl.java
index 3f890a1d893..436128423ce 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/computation/taskprocessor/CeWorkerCallableImpl.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/computation/taskprocessor/CeWorkerCallableImpl.java
@@ -68,8 +68,6 @@ public class CeWorkerCallableImpl implements CeWorkerCallable {
}
private void executeTask(CeTask task) {
- // logging twice: once in sonar.log and once in CE appender
- Profiler regularProfiler = startProfiler(task);
ceLogging.initForTask(task);
Profiler ceProfiler = startActivityProfiler(task);
@@ -89,44 +87,23 @@ public class CeWorkerCallableImpl implements CeWorkerCallable {
LOG.error(format("Failed to execute task %s", task.getUuid()), e);
} finally {
queue.remove(task, status, process);
- // logging twice: once in sonar.log and once in CE appender
stopActivityProfiler(ceProfiler, task, status);
ceLogging.clearForTask();
- stopProfiler(regularProfiler, task, status);
}
}
- private static Profiler startProfiler(CeTask task) {
- Profiler profiler = Profiler.create(LOG);
- addContext(profiler, task);
- return profiler.startDebug("Execute task");
- }
-
private static Profiler startActivityProfiler(CeTask task) {
Profiler profiler = Profiler.create(LOG);
addContext(profiler, task);
- return logCeActivity(() -> profiler.startInfo("Execute task"));
- }
-
- private static void stopProfiler(Profiler profiler, CeTask task, CeActivityDto.Status status) {
- if (!profiler.isDebugEnabled()) {
- return;
- }
-
- addContext(profiler, task);
- if (status == CeActivityDto.Status.FAILED) {
- profiler.stopError("Executed task");
- } else {
- profiler.stopDebug("Executed task");
- }
+ return logCeActivity(LOG, () -> profiler.startInfo("Execute task"));
}
private static void stopActivityProfiler(Profiler profiler, CeTask task, CeActivityDto.Status status) {
addContext(profiler, task);
if (status == CeActivityDto.Status.FAILED) {
- logCeActivity(() -> profiler.stopError("Executed task"));
+ logCeActivity(LOG, () -> profiler.stopError("Executed task"));
} else {
- logCeActivity(() -> profiler.stopInfo("Executed task"));
+ logCeActivity(LOG, () -> profiler.stopInfo("Executed task"));
}
}
diff --git a/server/sonar-server/src/test/java/org/sonar/ce/log/CeActivityLogAcceptFilterTest.java b/server/sonar-server/src/test/java/org/sonar/ce/log/CeActivityLogAcceptFilterTest.java
index 3dcb4b4abb9..1da7b374037 100644
--- a/server/sonar-server/src/test/java/org/sonar/ce/log/CeActivityLogAcceptFilterTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/ce/log/CeActivityLogAcceptFilterTest.java
@@ -19,11 +19,12 @@
*/
package org.sonar.ce.log;
-import ch.qos.logback.core.spi.FilterReply;
import org.apache.log4j.MDC;
import org.junit.After;
import org.junit.Test;
+import static ch.qos.logback.core.spi.FilterReply.ACCEPT;
+import static ch.qos.logback.core.spi.FilterReply.DENY;
import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.ce.log.CeLogging.MDC_CE_ACTIVITY_FLAG;
@@ -39,25 +40,37 @@ public class CeActivityLogAcceptFilterTest {
@Test
public void rejects_logs_when_property_is_not_set_in_MDC() {
- assertThat(underTest.decide(UNUSED)).isEqualTo(FilterReply.DENY);
+ assertThat(underTest.decide(UNUSED)).isEqualTo(DENY);
}
@Test
- public void rejects_logs_when_property_is_not_true_in_MDC() {
+ public void accepts_logs_when_property_is_neither_ce_only_nor_all_in_MDC() {
MDC.put(MDC_CE_ACTIVITY_FLAG, "bla");
- assertThat(underTest.decide(UNUSED)).isEqualTo(FilterReply.DENY);
+ assertThat(underTest.decide(UNUSED)).isEqualTo(ACCEPT);
}
@Test
- public void rejects_logs_when_property_is_not_true_in_lowercase_in_MDC() {
- MDC.put(MDC_CE_ACTIVITY_FLAG, "TrUE");
- assertThat(underTest.decide(UNUSED)).isEqualTo(FilterReply.DENY);
+ public void accepts_logs_when_property_is_ce_only_not_in_lowercase_in_MDC() {
+ MDC.put(MDC_CE_ACTIVITY_FLAG, "Ce_ONly");
+ assertThat(underTest.decide(UNUSED)).isEqualTo(ACCEPT);
}
@Test
- public void accepts_logs_when_property_is_true_in_lowercase_in_MDC() {
- MDC.put(MDC_CE_ACTIVITY_FLAG, "true");
- assertThat(underTest.decide(UNUSED)).isEqualTo(FilterReply.ACCEPT);
+ public void accepts_logs_when_property_is_ce_only_in_lowercase_in_MDC() {
+ MDC.put(MDC_CE_ACTIVITY_FLAG, "ce_only");
+ assertThat(underTest.decide(UNUSED)).isEqualTo(ACCEPT);
+ }
+
+ @Test
+ public void accepts_logs_when_property_is_all_in_lowercase_in_MDC() {
+ MDC.put(MDC_CE_ACTIVITY_FLAG, "all");
+ assertThat(underTest.decide(UNUSED)).isEqualTo(ACCEPT);
+ }
+
+ @Test
+ public void accepts_logs_when_property_is_all_not_in_lowercase_in_MDC() {
+ MDC.put(MDC_CE_ACTIVITY_FLAG, "aLl");
+ assertThat(underTest.decide(UNUSED)).isEqualTo(ACCEPT);
}
}
diff --git a/server/sonar-server/src/test/java/org/sonar/ce/log/CeActivityLogConsoleFilterTest.java b/server/sonar-server/src/test/java/org/sonar/ce/log/CeActivityLogConsoleFilterTest.java
new file mode 100644
index 00000000000..7a0e6b38a7f
--- /dev/null
+++ b/server/sonar-server/src/test/java/org/sonar/ce/log/CeActivityLogConsoleFilterTest.java
@@ -0,0 +1,89 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact 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.ce.log;
+
+import ch.qos.logback.classic.spi.ILoggingEvent;
+import org.apache.log4j.MDC;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+import static ch.qos.logback.classic.Level.DEBUG;
+import static ch.qos.logback.classic.Level.INFO;
+import static ch.qos.logback.core.spi.FilterReply.ACCEPT;
+import static ch.qos.logback.core.spi.FilterReply.DENY;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.when;
+import static org.sonar.ce.log.CeLogging.MDC_CE_ACTIVITY_FLAG;
+
+public class CeActivityLogConsoleFilterTest {
+ private ILoggingEvent loggingEvent = Mockito.mock(ILoggingEvent.class);
+
+ private CeActivityLogConsoleFilter underTest = new CeActivityLogConsoleFilter();
+
+ @Before
+ public void setUp() throws Exception {
+ when(loggingEvent.getLevel()).thenReturn(INFO);
+ }
+
+ @After
+ public void tearDown() {
+ MDC.clear();
+ }
+
+ @Test
+ public void accepts_logs_when_property_is_not_set_in_MDC() {
+ assertThat(underTest.decide(loggingEvent)).isEqualTo(ACCEPT);
+ }
+
+ @Test
+ public void rejects_logs_when_property_is_set_to_anything_but_all_in_lowercase_in_MDC() {
+ MDC.put(MDC_CE_ACTIVITY_FLAG, "bla");
+ assertThat(underTest.decide(loggingEvent)).isEqualTo(DENY);
+ }
+
+ @Test
+ public void rejects_logs_when_property_is_set_to_all_not_in_lowercase_in_MDC() {
+ MDC.put(MDC_CE_ACTIVITY_FLAG, "AlL");
+ assertThat(underTest.decide(loggingEvent)).isEqualTo(DENY);
+ }
+
+ @Test
+ public void accepts_logs_when_property_is_all_in_MDC() {
+ MDC.put(MDC_CE_ACTIVITY_FLAG, "all");
+ assertThat(underTest.decide(loggingEvent)).isEqualTo(ACCEPT);
+ }
+
+ @Test
+ public void rejects_logs_when_property_is_ce_only_in_MDC() {
+ MDC.put(MDC_CE_ACTIVITY_FLAG, "ce_only");
+ assertThat(underTest.decide(loggingEvent)).isEqualTo(DENY);
+ }
+
+ @Test
+ public void accepts_logs_when_property_is_all_in_lowercase_in_MDC() {
+ when(loggingEvent.getLevel()).thenReturn(DEBUG);
+
+ MDC.put(MDC_CE_ACTIVITY_FLAG, "all");
+ assertThat(underTest.decide(loggingEvent)).isEqualTo(ACCEPT);
+ }
+
+}
diff --git a/server/sonar-server/src/test/java/org/sonar/ce/log/CeActivityLogDenyFilterTest.java b/server/sonar-server/src/test/java/org/sonar/ce/log/CeActivityLogDenyFilterTest.java
deleted file mode 100644
index f6ad1ad5e8c..00000000000
--- a/server/sonar-server/src/test/java/org/sonar/ce/log/CeActivityLogDenyFilterTest.java
+++ /dev/null
@@ -1,62 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact 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.ce.log;
-
-import ch.qos.logback.core.spi.FilterReply;
-import org.apache.log4j.MDC;
-import org.junit.After;
-import org.junit.Test;
-
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.sonar.ce.log.CeLogging.MDC_CE_ACTIVITY_FLAG;
-
-public class CeActivityLogDenyFilterTest {
- private static final Object UNUSED = "";
-
- private CeActivityLogDenyFilter underTest = new CeActivityLogDenyFilter();
-
- @After
- public void tearDown() {
- MDC.clear();
- }
-
- @Test
- public void accepts_logs_when_property_is_not_set_in_MDC() {
- assertThat(underTest.decide(UNUSED)).isEqualTo(FilterReply.ACCEPT);
- }
-
- @Test
- public void acceots_logs_when_property_is_not_true_in_MDC() {
- MDC.put(MDC_CE_ACTIVITY_FLAG, "bla");
- assertThat(underTest.decide(UNUSED)).isEqualTo(FilterReply.ACCEPT);
- }
-
- @Test
- public void accepts_logs_when_property_is_not_true_in_lowercase_in_MDC() {
- MDC.put(MDC_CE_ACTIVITY_FLAG, "TrUE");
- assertThat(underTest.decide(UNUSED)).isEqualTo(FilterReply.ACCEPT);
- }
-
- @Test
- public void rejects_logs_when_property_is_true_in_lowercase_in_MDC() {
- MDC.put(MDC_CE_ACTIVITY_FLAG, "true");
- assertThat(underTest.decide(UNUSED)).isEqualTo(FilterReply.DENY);
- }
-}
diff --git a/server/sonar-server/src/test/java/org/sonar/ce/log/CeLoggingTest.java b/server/sonar-server/src/test/java/org/sonar/ce/log/CeLoggingTest.java
index 74f54cbd08a..216397f2168 100644
--- a/server/sonar-server/src/test/java/org/sonar/ce/log/CeLoggingTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/ce/log/CeLoggingTest.java
@@ -19,6 +19,7 @@
*/
package org.sonar.ce.log;
+import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
import ch.qos.logback.classic.spi.ILoggingEvent;
@@ -36,12 +37,20 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
+import org.mockito.Mockito;
import org.slf4j.MDC;
+import org.sonar.api.utils.log.Logger;
import org.sonar.process.LogbackHelper;
import org.sonar.process.ProcessProperties;
import org.sonar.process.Props;
+import static ch.qos.logback.classic.Level.DEBUG;
+import static ch.qos.logback.classic.Level.ERROR;
+import static ch.qos.logback.classic.Level.INFO;
+import static ch.qos.logback.classic.Level.TRACE;
+import static ch.qos.logback.classic.Level.WARN;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.when;
import static org.sonar.ce.log.CeLogging.MDC_CE_ACTIVITY_FLAG;
public class CeLoggingTest {
@@ -101,24 +110,83 @@ public class CeLoggingTest {
}
@Test
- public void logCeActivity_of_Runnable_set_flag_in_MDC_calls_Runnable_and_remove_flag() {
+ public void logCeActivity_of_Runnable_set_flag_in_MDC_to_ce_only_when_log_level_is_ERROR_and_calls_Runnable_and_remove_flag() {
+ callLogCeActivityOfRunnableAndVerify("ce_only", ERROR);
+ }
+
+ @Test
+ public void logCeActivity_of_Runnable_set_flag_in_MDC_to_ce_only_when_log_level_is_WARN_and_calls_Runnable_and_remove_flag() {
+ callLogCeActivityOfRunnableAndVerify("ce_only", WARN);
+ }
+
+ @Test
+ public void logCeActivity_of_Runnable_set_flag_in_MDC_to_ce_only_when_log_level_is_INFO_and_calls_Runnable_and_remove_flag() {
+ callLogCeActivityOfRunnableAndVerify("ce_only", INFO);
+ }
+
+ @Test
+ public void logCeActivity_of_Runnable_set_flag_in_MDC_to_all_when_log_level_is_DEBUG_and_calls_Runnable_and_remove_flag() {
+ callLogCeActivityOfRunnableAndVerify("all", DEBUG);
+ }
+
+ @Test
+ public void logCeActivity_of_Runnable_set_flag_in_MDC_to_all_when_log_level_is_TRACE_and_calls_Runnable_and_remove_flag() {
+ callLogCeActivityOfRunnableAndVerify("all", TRACE);
+ }
+
+ private void callLogCeActivityOfRunnableAndVerify(String expectedMdcValue, Level logLevel) {
AtomicBoolean called = new AtomicBoolean(false);
- CeLogging.logCeActivity(() -> {
- assertThat(MDC.get(MDC_CE_ACTIVITY_FLAG)).isEqualTo("true");
- called.compareAndSet(false, true);
- });
+ CeLogging.logCeActivity(
+ createLogger(logLevel),
+ () -> {
+ assertThat(MDC.get(MDC_CE_ACTIVITY_FLAG)).isEqualTo(expectedMdcValue);
+ called.compareAndSet(false, true);
+ });
assertThat(MDC.get(MDC_CE_ACTIVITY_FLAG)).isNull();
assertThat(called.get()).isTrue();
}
+ private static Logger createLogger(Level info) {
+ Logger logger = Mockito.mock(Logger.class);
+ when(logger.isDebugEnabled()).thenReturn(DEBUG.isGreaterOrEqual(info));
+ when(logger.isTraceEnabled()).thenReturn(TRACE.isGreaterOrEqual(info));
+ return logger;
+ }
+
+ @Test
+ public void logCeActivity_of_Supplier_set_flag_in_MDC_to_ce_only_when_log_level_is_ERROR_and_calls_Supplier_and_remove_flag() {
+ callLogCeActivityOfSupplierAndVerify(ERROR, "ce_only");
+ }
+
@Test
- public void logCeActivity_of_Runnable_set_flag_in_MDC_calls_Supplier_and_remove_flag() {
+ public void logCeActivity_of_Supplier_set_flag_in_MDC_to_ce_only_when_log_level_is_WARN_and_calls_Supplier_and_remove_flag() {
+ callLogCeActivityOfSupplierAndVerify(WARN, "ce_only");
+ }
+
+ @Test
+ public void logCeActivity_of_Supplier_set_flag_in_MDC_to_ce_only_when_log_level_is_INFO_and_calls_Supplier_and_remove_flag() {
+ callLogCeActivityOfSupplierAndVerify(INFO, "ce_only");
+ }
+
+ @Test
+ public void logCeActivity_of_Supplier_set_flag_in_MDC_to_all_when_log_level_is_DEBUG_and_calls_Supplier_and_remove_flag() {
+ callLogCeActivityOfSupplierAndVerify(DEBUG, "all");
+ }
+
+ @Test
+ public void logCeActivity_of_Supplier_set_flag_in_MDC_to_all_when_log_level_is_TRACE_and_calls_Supplier_and_remove_flag() {
+ callLogCeActivityOfSupplierAndVerify(TRACE, "all");
+ }
+
+ private void callLogCeActivityOfSupplierAndVerify(Level logLevel, String expectedFlag) {
AtomicBoolean called = new AtomicBoolean(false);
- CeLogging.logCeActivity(() -> {
- assertThat(MDC.get(MDC_CE_ACTIVITY_FLAG)).isEqualTo("true");
- called.compareAndSet(false, true);
- return 1;
- });
+ CeLogging.logCeActivity(
+ createLogger(logLevel),
+ () -> {
+ assertThat(MDC.get(MDC_CE_ACTIVITY_FLAG)).isEqualTo(expectedFlag);
+ called.compareAndSet(false, true);
+ return 1;
+ });
assertThat(MDC.get(MDC_CE_ACTIVITY_FLAG)).isNull();
assertThat(called.get()).isTrue();
}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/taskprocessor/CeWorkerCallableImplTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/taskprocessor/CeWorkerCallableImplTest.java
index 27c7c388794..1460f3f6e39 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/computation/taskprocessor/CeWorkerCallableImplTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/computation/taskprocessor/CeWorkerCallableImplTest.java
@@ -186,10 +186,7 @@ public class CeWorkerCallableImplTest {
assertThat(logs.get(0)).contains(" | submitter=FooBar");
assertThat(logs.get(1)).contains(" | submitter=FooBar | time=");
assertThat(logTester.logs(LoggerLevel.ERROR)).isEmpty();
- logs = logTester.logs(LoggerLevel.DEBUG);
- assertThat(logs).hasSize(2);
- assertThat(logs.get(0)).contains(" | submitter=FooBar");
- assertThat(logs.get(1)).contains(" | submitter=FooBar | time=");
+ assertThat(logTester.logs(LoggerLevel.DEBUG)).isEmpty();
}
@Test
@@ -207,13 +204,10 @@ public class CeWorkerCallableImplTest {
assertThat(logs).hasSize(1);
assertThat(logs.iterator().next()).contains(" | submitter=FooBar");
logs = logTester.logs(LoggerLevel.ERROR);
- assertThat(logs).hasSize(3);
+ assertThat(logs).hasSize(2);
assertThat(logs.get(0)).isEqualTo("Failed to execute task " + ceTask.getUuid());
assertThat(logs.get(1)).contains(" | submitter=FooBar | time=");
- assertThat(logs.get(2)).contains(" | submitter=FooBar | time=");
- logs = logTester.logs(LoggerLevel.DEBUG);
- assertThat(logs).hasSize(1);
- assertThat(logs.get(0)).contains(" | submitter=FooBar");
+ assertThat(logTester.logs(LoggerLevel.DEBUG)).isEmpty();
}
private static CeTask createCeTask(@Nullable String submitterLogin) {