From adbdacf51ffd17c64bed90d2de963c192f1a4921 Mon Sep 17 00:00:00 2001 From: Sophio Japharidze Date: Fri, 3 Jan 2025 12:00:14 +0100 Subject: [PATCH] SLLS-262 include thread and logger name in the logs --- .../ls/clientapi/SonarLintVSCodeClient.java | 5 +- .../AbstractLanguageServerMediumTests.java | 4 +- .../ls/mediumtests/JavaMediumTests.java | 15 ++--- .../LanguageServerMediumTests.java | 62 ++++++++----------- .../LanguageServerWithFoldersMediumTests.java | 24 ++----- 5 files changed, 42 insertions(+), 68 deletions(-) diff --git a/src/main/java/org/sonarsource/sonarlint/ls/clientapi/SonarLintVSCodeClient.java b/src/main/java/org/sonarsource/sonarlint/ls/clientapi/SonarLintVSCodeClient.java index 1d15fff0..21d0f0d2 100644 --- a/src/main/java/org/sonarsource/sonarlint/ls/clientapi/SonarLintVSCodeClient.java +++ b/src/main/java/org/sonarsource/sonarlint/ls/clientapi/SonarLintVSCodeClient.java @@ -195,13 +195,14 @@ public void showMessage(org.sonarsource.sonarlint.core.rpc.protocol.client.messa @Override public void log(LogParams params) { + var prefix = String.format("[%s : %s] ", params.getLoggerName(), params.getThreadName()); var rawMessage = params.getMessage(); - var sanitizedMessage = rawMessage != null ? rawMessage : "null"; + var sanitizedMessage = rawMessage != null ? prefix.concat(rawMessage) : "null"; var level = params.getLevel(); logOutput.log(sanitizedMessage, level); var stackTrace = params.getStackTrace(); if (stackTrace != null) { - logOutput.log(stackTrace, level); + logOutput.log(prefix.concat(stackTrace), level); } } diff --git a/src/test/java/org/sonarsource/sonarlint/ls/mediumtests/AbstractLanguageServerMediumTests.java b/src/test/java/org/sonarsource/sonarlint/ls/mediumtests/AbstractLanguageServerMediumTests.java index f2ab7311..25cae301 100644 --- a/src/test/java/org/sonarsource/sonarlint/ls/mediumtests/AbstractLanguageServerMediumTests.java +++ b/src/test/java/org/sonarsource/sonarlint/ls/mediumtests/AbstractLanguageServerMediumTests.java @@ -771,7 +771,7 @@ protected ThrowingExtractor wit return p -> p.getMessage().replaceAll("\\[(\\w*)\\s+-\\s[\\d:.]*\\]", "[$1]"); } - protected ThrowingExtractor withoutTimestampAndMillis() { + protected static ThrowingExtractor withoutTimestampAndMillis() { return p -> p.getMessage().replaceAll("\\[(\\w*)\\s+-\\s[\\d:.]*\\]", "[$1]").replaceAll("\\d{2,4}ms", "XXXms"); } @@ -795,7 +795,7 @@ protected ThrowingExtractor wit return d -> d.getRange().getStart().getLine(); } - protected void awaitUntilAsserted(ThrowingRunnable assertion) { + protected static void awaitUntilAsserted(ThrowingRunnable assertion) { await().atMost(2, MINUTES).untilAsserted(assertion); } diff --git a/src/test/java/org/sonarsource/sonarlint/ls/mediumtests/JavaMediumTests.java b/src/test/java/org/sonarsource/sonarlint/ls/mediumtests/JavaMediumTests.java index 9feb5e7b..9c535429 100644 --- a/src/test/java/org/sonarsource/sonarlint/ls/mediumtests/JavaMediumTests.java +++ b/src/test/java/org/sonarsource/sonarlint/ls/mediumtests/JavaMediumTests.java @@ -47,6 +47,7 @@ import static org.assertj.core.groups.Tuple.tuple; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; +import static org.sonarsource.sonarlint.ls.mediumtests.LanguageServerMediumTests.assertAnalysisLogsContains; class JavaMediumTests extends AbstractLanguageServerMediumTests { @@ -231,9 +232,9 @@ void analyzeSimpleJavaFilePassVmClasspath() throws Exception { assertThat(client.logs) .extracting(withoutTimestamp()) .containsSubsequence( - "[Debug] Property 'sonar.java.jdkHome' set with: " + currentJdkHome, - "[Debug] Property 'sonar.java.jdkHome' resolved with:" + System.lineSeparator() + "[" + jrtFsJarPath + "]", - "[Debug] Property 'sonar.java.libraries' resolved with:" + System.lineSeparator() + "[" + jrtFsJarPath + "]"); + "[Debug] [sonarlint : sonarlint-analysis-engine] Property 'sonar.java.jdkHome' set with: " + currentJdkHome, + "[Debug] [sonarlint : sonarlint-analysis-engine] Property 'sonar.java.jdkHome' resolved with:" + System.lineSeparator() + "[" + jrtFsJarPath + "]", + "[Debug] [sonarlint : sonarlint-analysis-engine] Property 'sonar.java.libraries' resolved with:" + System.lineSeparator() + "[" + jrtFsJarPath + "]"); } @Test @@ -278,18 +279,14 @@ void testClassPathUpdateEvictCacheAndTriggersNewAnalysis(@TempDir Path projectRo didOpen(uri, "java", "import org.junit.Test;\npublic class FooTest {\n @Test\n public void test() {\n String s = \"foo\";\n}\n}"); - awaitUntilAsserted(() -> assertThat(client.logs) - .extracting(withoutTimestampAndMillis()) - .contains("[Info] Analysis detected 2 issues and 0 Security Hotspots in XXXms")); + assertAnalysisLogsContains(2, 0); client.logs.clear(); // Update classpath javaConfigResponse.setClasspath(new String[]{Paths.get(this.getClass().getResource("/junit-4.12.jar").toURI()).toAbsolutePath().toString()}); lsProxy.didClasspathUpdate(new DidClasspathUpdateParams(projectRootUri2)); - awaitUntilAsserted(() -> assertThat(client.logs) - .extracting(withoutTimestampAndMillis()) - .contains("[Info] Analysis detected 3 issues and 0 Security Hotspots in XXXms")); + assertAnalysisLogsContains(3, 0); awaitUntilAsserted(() -> assertThat(client.getDiagnostics(uri)) .extracting(startLine(), startCharacter(), endLine(), endCharacter(), code(), Diagnostic::getSource, Diagnostic::getMessage, Diagnostic::getSeverity) diff --git a/src/test/java/org/sonarsource/sonarlint/ls/mediumtests/LanguageServerMediumTests.java b/src/test/java/org/sonarsource/sonarlint/ls/mediumtests/LanguageServerMediumTests.java index d7fcfc35..533a789d 100644 --- a/src/test/java/org/sonarsource/sonarlint/ls/mediumtests/LanguageServerMediumTests.java +++ b/src/test/java/org/sonarsource/sonarlint/ls/mediumtests/LanguageServerMediumTests.java @@ -428,9 +428,7 @@ void noIssueOnTestJSFiles() throws Exception { var fooTestUri = getUri("fooTest.js", analysisDir); didOpen(fooTestUri, "javascript", jsContent); - awaitUntilAsserted(() -> assertThat(client.logs) - .extracting(withoutTimestampAndMillis()) - .contains("[Info] Analysis detected 0 issues and 0 Security Hotspots in XXXms")); + assertAnalysisLogsContains(0, 0); assertThat(client.getDiagnostics(fooTestUri)).isEmpty(); client.clear(); @@ -438,9 +436,7 @@ void noIssueOnTestJSFiles() throws Exception { notifyConfigurationChangeOnClient(); didChange(fooTestUri, jsContent); - awaitUntilAsserted(() -> assertThat(client.logs) - .extracting(withoutTimestampAndMillis()) - .contains("[Info] Analysis detected 1 issue and 0 Security Hotspots in XXXms")); + assertAnalysisLogsContains(1, 0); awaitUntilAsserted(() -> assertThat(client.getDiagnostics(fooTestUri)).hasSize(1)); client.logs.clear(); @@ -448,9 +444,7 @@ void noIssueOnTestJSFiles() throws Exception { var fooMyTestUri = getUri("fooMyTest.js", analysisDir); didOpen(fooMyTestUri, "javascript", jsContent); - awaitUntilAsserted(() -> assertThat(client.logs) - .extracting(withoutTimestampAndMillis()) - .contains("[Info] Analysis detected 0 issues and 0 Security Hotspots in XXXms")); + assertAnalysisLogsContains(0, 0); assertThat(client.getDiagnostics(fooMyTestUri)).isEmpty(); } @@ -526,9 +520,7 @@ void delayAnalysisOnChange() throws Exception { didOpen(uri, "python", "def foo():\n print('Error code %d' % '42')\n"); - awaitUntilAsserted(() -> assertThat(client.logs) - .extracting(withoutTimestampAndMillis()) - .contains("[Info] Analysis detected 1 issue and 0 Security Hotspots in XXXms")); + assertAnalysisLogsContains(1, 0); client.logs.clear(); @@ -563,10 +555,7 @@ void noAnalysisOnNullContent() throws Exception { lsProxy.getTextDocumentService() .didClose(new DidCloseTextDocumentParams(new TextDocumentIdentifier(uri))); - awaitUntilAsserted(() -> assertThat(client.logs) - .extracting(withoutTimestampAndMillis()) - .contains("[Info] Analysis detected 0 issues and 0 Security Hotspots in XXXms", - "[Info] Analysis detected 0 issues and 0 Security Hotspots in XXXms")); + assertAnalysisLogsContains(0, 0); assertThat(client.getDiagnostics(uri)).isEmpty(); } @@ -590,7 +579,7 @@ void vcsIgnoredShouldNotAnalyzed() throws Exception { didOpen(uri, "python", "# Nothing to see here\n"); awaitUntilAsserted(() -> assertThat(client.logs).extracting(withoutTimestamp()) - .contains("[Error] No file to analyze")); + .contains("[Error] [sonarlint : SonarLint Analysis Executor] No file to analyze")); assertThat(client.getDiagnostics(uri)).isEmpty(); } @@ -798,11 +787,8 @@ void test_analysis_logs_disabled() throws Exception { var uri = getUri("testAnalysisLogsDisabled.py", analysisDir); didOpen(uri, "python", "def foo():\n toto = 0\n"); - awaitUntilAsserted(() -> assertThat(client.logs) - .filteredOn(notFromContextualTSserver()) - .extracting(withoutTimestampAndMillis()) - .contains( - "[Info] Analysis detected 1 issue and 0 Security Hotspots in XXXms")); + + assertAnalysisLogsContains(1, 0); } @Test @@ -815,11 +801,7 @@ void test_debug_logs_enabled() throws Exception { var uri = getUri("testAnalysisLogsDebugEnabled.py", analysisDir); didOpen(uri, "python", "def foo():\n toto = 0\n"); - awaitUntilAsserted(() -> assertThat(client.logs) - .filteredOn(notFromContextualTSserver()) - .extracting(withoutTimestampAndMillis()) - .containsSubsequence( - "[Info] Analysis detected 1 issue and 0 Security Hotspots in XXXms")); + assertAnalysisLogsContains(1, 0); } @Test @@ -836,10 +818,10 @@ void test_analysis_logs_enabled() throws Exception { .filteredOn(notFromContextualTSserver()) .extracting(withoutTimestampAndMillis()) .contains( - "[Info] Index files", - "[Info] 1 file indexed", - "[Info] 1 source file to be analyzed", - "[Info] Analysis detected 1 issue and 0 Security Hotspots in XXXms")); + "[Info] [sonarlint : sonarlint-analysis-engine] Index files", + "[Info] [sonarlint : Report about progress of file indexation] 1 file indexed", + "[Info] [sonarlint : rules execution progress] 1 source file to be analyzed", + "[Info] [sonarlint : sonarlint-analysis-engine] Analysis detected 1 issue and 0 Security Hotspots in XXXms")); } @Test @@ -857,11 +839,11 @@ void test_analysis_with_debug_logs_enabled() throws Exception { .filteredOn(notFromContextualTSserver()) .extracting(withoutTimestampAndMillis()) .contains( - "[Info] Index files", - "[Debug] Language of file \"" + uri + "\" is set to \"PYTHON\"", - "[Info] 1 file indexed", - "[Debug] Execute Sensor: Python Sensor", - "[Info] Analysis detected 1 issue and 0 Security Hotspots in XXXms")); + "[Info] [sonarlint : sonarlint-analysis-engine] Index files", + "[Debug] [sonarlint : sonarlint-analysis-engine] Language of file \"" + uri + "\" is set to \"PYTHON\"", + "[Info] [sonarlint : Report about progress of file indexation] 1 file indexed", + "[Debug] [sonarlint : sonarlint-analysis-engine] Execute Sensor: Python Sensor", + "[Info] [sonarlint : sonarlint-analysis-engine] Analysis detected 1 issue and 0 Security Hotspots in XXXms")); } @Test @@ -1134,4 +1116,12 @@ private Predicate notFromContextualTSserver() { return m -> !m.getMessage().contains("SonarTS") && !m.getMessage().contains("Using typescript at"); } + public static void assertAnalysisLogsContains(int issues, int hotspots) { + var issuesString = issues == 1 ? "issue" : "issues"; + var hotspotsString = hotspots == 1 ? "Security Hotspot" : "Security Hotspots"; + awaitUntilAsserted(() -> assertThat(client.logs) + .extracting(withoutTimestampAndMillis()) + .contains(String.format("[Info] [sonarlint : sonarlint-analysis-engine] Analysis detected %d %s and %d %s in XXXms", issues, issuesString, hotspots, hotspotsString))); + } + } diff --git a/src/test/java/org/sonarsource/sonarlint/ls/mediumtests/LanguageServerWithFoldersMediumTests.java b/src/test/java/org/sonarsource/sonarlint/ls/mediumtests/LanguageServerWithFoldersMediumTests.java index 2382612a..4475dfe8 100644 --- a/src/test/java/org/sonarsource/sonarlint/ls/mediumtests/LanguageServerWithFoldersMediumTests.java +++ b/src/test/java/org/sonarsource/sonarlint/ls/mediumtests/LanguageServerWithFoldersMediumTests.java @@ -48,6 +48,7 @@ import static org.assertj.core.groups.Tuple.tuple; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; +import static org.sonarsource.sonarlint.ls.mediumtests.LanguageServerMediumTests.assertAnalysisLogsContains; class LanguageServerWithFoldersMediumTests extends AbstractLanguageServerMediumTests { @@ -164,10 +165,7 @@ void shouldBatchAnalysisFromTheSameFolder() { didOpen(file1InFolder, "python", "def foo():\n return\n"); didOpen(file2InFolder, "python", "def foo():\n return\n"); - awaitUntilAsserted(() -> assertThat(client.logs) - .extracting(withoutTimestampAndMillis()) - .contains("[Info] Analysis detected 0 issues and 0 Security Hotspots in XXXms", - "[Info] Analysis detected 0 issues and 0 Security Hotspots in XXXms")); + assertAnalysisLogsContains(0, 0); client.logs.clear(); @@ -179,11 +177,7 @@ void shouldBatchAnalysisFromTheSameFolder() { .didChange(new DidChangeTextDocumentParams(new VersionedTextDocumentIdentifier(file2InFolder, 2), List.of(new TextDocumentContentChangeEvent("def foo():\n toto2 = 0\n plouf2 = 0\n")))); - awaitUntilAsserted(() -> assertThat(client.logs) - .extracting(withoutTimestampAndMillis()) - .containsSubsequence( - "[Info] Analysis detected 2 issues and 0 Security Hotspots in XXXms", - "[Info] Analysis detected 2 issues and 0 Security Hotspots in XXXms")); + assertAnalysisLogsContains(2, 0); } @Test @@ -198,11 +192,7 @@ void shouldNotBatchAnalysisFromDifferentFolders() { didOpen(file1InFolder1, "python", "def foo():\n toto = 0\n"); didOpen(file2InFolder2, "python", "def foo():\n toto2 = 0\n"); - awaitUntilAsserted(() -> assertThat(client.logs) - .extracting(withoutTimestampAndMillis()) - .containsSubsequence( - "[Info] Analysis detected 1 issue and 0 Security Hotspots in XXXms", - "[Info] Analysis detected 1 issue and 0 Security Hotspots in XXXms")); + assertAnalysisLogsContains(1, 0); client.logs.clear(); @@ -214,11 +204,7 @@ void shouldNotBatchAnalysisFromDifferentFolders() { .didChange(new DidChangeTextDocumentParams(new VersionedTextDocumentIdentifier(file2InFolder2, 2), List.of(new TextDocumentContentChangeEvent("def foo():\n toto2 = 0\n plouf2 = 0\n")))); - awaitUntilAsserted(() -> assertThat(client.logs) - .extracting(withoutTimestampAndMillis()) - .containsSubsequence( - "[Info] Analysis detected 2 issues and 0 Security Hotspots in XXXms", - "[Info] Analysis detected 2 issues and 0 Security Hotspots in XXXms")); + assertAnalysisLogsContains(2, 0); } @Test