aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java/testutils/LogTester.java
diff options
context:
space:
mode:
authorJulien HENRY <julien.henry@sonarsource.com>2024-05-03 11:41:04 +0200
committerJulien HENRY <julien.henry@sonarsource.com>2024-05-06 14:21:26 +0200
commit90a5df59eddccf630deec66f8d487dd1d69114a9 (patch)
treefc5498ee6a19c70ab27077e73b146342a634d065 /src/test/java/testutils/LogTester.java
parent9688ddf5c73288621cb431052196d0e7cae6080c (diff)
downloadsonar-scanner-cli-90a5df59eddccf630deec66f8d487dd1d69114a9.tar.gz
sonar-scanner-cli-90a5df59eddccf630deec66f8d487dd1d69114a9.zip
SCANCLI-146 Log using slf4j + logback
Diffstat (limited to 'src/test/java/testutils/LogTester.java')
-rw-r--r--src/test/java/testutils/LogTester.java91
1 files changed, 91 insertions, 0 deletions
diff --git a/src/test/java/testutils/LogTester.java b/src/test/java/testutils/LogTester.java
new file mode 100644
index 0000000..1708479
--- /dev/null
+++ b/src/test/java/testutils/LogTester.java
@@ -0,0 +1,91 @@
+/*
+ * SonarScanner CLI
+ * Copyright (C) 2011-2024 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 testutils;
+
+import ch.qos.logback.classic.spi.ILoggingEvent;
+import ch.qos.logback.classic.spi.LoggingEvent;
+import java.util.List;
+import org.junit.jupiter.api.extension.AfterEachCallback;
+import org.junit.jupiter.api.extension.BeforeEachCallback;
+import org.junit.jupiter.api.extension.ExtensionContext;
+import org.slf4j.LoggerFactory;
+import org.slf4j.event.Level;
+
+public class LogTester implements BeforeEachCallback, AfterEachCallback {
+
+ private final ConcurrentListAppender<ILoggingEvent> listAppender = new ConcurrentListAppender<>();
+
+ public LogTester() {
+ setLevel(Level.INFO);
+ }
+
+ /**
+ * Change log level.
+ * By default, INFO logs are enabled when LogTester is started.
+ */
+ public LogTester setLevel(Level level) {
+ getRootLogger().setLevel(ch.qos.logback.classic.Level.fromLocationAwareLoggerInteger(level.toInt()));
+ return this;
+ }
+
+ private static ch.qos.logback.classic.Logger getRootLogger() {
+ return (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
+ }
+
+ /**
+ * Logs in chronological order (item at index 0 is the oldest one)
+ */
+ public List<String> logs() {
+ return listAppender.list.stream().map(e -> (LoggingEvent) e)
+ .map(LoggingEvent::getFormattedMessage)
+ .toList();
+ }
+
+ /**
+ * Logs in chronological order (item at index 0 is the oldest one) for
+ * a given level
+ */
+ public List<String> logs(Level level) {
+ return listAppender.list.stream().map(e -> (LoggingEvent) e)
+ .filter(e -> e.getLevel().equals(ch.qos.logback.classic.Level.fromLocationAwareLoggerInteger(level.toInt())))
+ .map(LoggingEvent::getFormattedMessage)
+ .toList();
+ }
+
+ public LogTester clear() {
+ listAppender.list.clear();
+ return this;
+ }
+
+ @Override
+ public void beforeEach(ExtensionContext context) throws Exception {
+ getRootLogger().addAppender(listAppender);
+ listAppender.start();
+ }
+
+ @Override
+ public void afterEach(ExtensionContext context) throws Exception {
+ listAppender.stop();
+ listAppender.list.clear();
+ getRootLogger().detachAppender(listAppender);
+ // Reset the level for following-up test suites
+ setLevel(Level.INFO);
+ }
+}