Skip to content

Commit

Permalink
SLLS-262 include thread and logger name in the logs
Browse files Browse the repository at this point in the history
  • Loading branch information
sophio-japharidze-sonarsource committed Jan 3, 2025
1 parent 70be16c commit f5ff536
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,7 @@ protected ThrowingExtractor<? super MessageParams, String, RuntimeException> wit
return p -> p.getMessage().replaceAll("\\[(\\w*)\\s+-\\s[\\d:.]*\\]", "[$1]");
}

protected ThrowingExtractor<? super MessageParams, String, RuntimeException> withoutTimestampAndMillis() {
protected static ThrowingExtractor<? super MessageParams, String, RuntimeException> withoutTimestampAndMillis() {
return p -> p.getMessage().replaceAll("\\[(\\w*)\\s+-\\s[\\d:.]*\\]", "[$1]").replaceAll("\\d{2,4}ms", "XXXms");
}

Expand All @@ -795,7 +795,7 @@ protected ThrowingExtractor<? super MessageParams, String, RuntimeException> wit
return d -> d.getRange().getStart().getLine();
}

protected void awaitUntilAsserted(ThrowingRunnable assertion) {
protected static void awaitUntilAsserted(ThrowingRunnable assertion) {
await().atMost(2, MINUTES).untilAsserted(assertion);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -428,29 +428,23 @@ 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();

setTestFilePattern(getFolderSettings(analysisDir.toUri().toString()), "{**/*MyTest*}");
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();

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();
}
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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();
}

Expand All @@ -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();
}

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -1134,4 +1116,12 @@ private Predicate<? super MessageParams> 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)));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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();

Expand All @@ -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
Expand All @@ -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();

Expand All @@ -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
Expand Down

0 comments on commit f5ff536

Please sign in to comment.