Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cherrypick/update logging #60

Merged
merged 2 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.ibm</groupId>
<artifactId>sonar-cryptography</artifactId>
<version>1.1.4-SNAPSHOT</version>
<version>2.0.0-SNAPSHOT</version>
</parent>

<artifactId>common</artifactId>
Expand Down
4 changes: 2 additions & 2 deletions engine/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.ibm</groupId>
<artifactId>sonar-cryptography</artifactId>
<version>1.1.4-SNAPSHOT</version>
<version>2.0.0-SNAPSHOT</version>
</parent>

<artifactId>engine</artifactId>
Expand All @@ -20,7 +20,7 @@
<dependency>
<groupId>com.ibm</groupId>
<artifactId>common</artifactId>
<version>1.1.4-SNAPSHOT</version>
<version>2.0.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.apache.commons.lang3.tuple.Pair;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.java.model.ExpressionUtils;
import org.sonar.plugins.java.api.JavaCheck;
import org.sonar.plugins.java.api.JavaFileScannerContext;
import org.sonar.plugins.java.api.semantic.Symbol;
import org.sonar.plugins.java.api.tree.*;

public final class JavaDetectionEngine implements IDetectionEngine<Tree, Symbol> {
private static final Logger LOGGER = Loggers.get(JavaDetectionEngine.class);
private static final Logger LOGGER = LoggerFactory.getLogger(JavaDetectionEngine.class);

@Nonnull
private final DetectionStore<JavaCheck, Tree, Symbol, JavaFileScannerContext> detectionStore;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.jetbrains.annotations.NotNull;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.java.model.ExpressionUtils;
import org.sonar.plugins.java.api.JavaCheck;
import org.sonar.plugins.java.api.JavaFileScannerContext;
Expand All @@ -41,7 +41,7 @@

public final class JavaLanguageSupport
implements ILanguageSupport<JavaCheck, Tree, Symbol, JavaFileScannerContext> {
private static final Logger LOGGER = Loggers.get(JavaLanguageSupport.class);
private static final Logger LOGGER = LoggerFactory.getLogger(JavaLanguageSupport.class);
@Nonnull private final Handler<JavaCheck, Tree, Symbol, JavaFileScannerContext> handler;

public JavaLanguageSupport() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@
import java.util.*;
import javax.annotation.Nonnull;
import org.jetbrains.annotations.NotNull;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.java.model.ExpressionUtils;
import org.sonar.plugins.java.api.tree.*;

public class JavaLanguageTranslation implements ILanguageTranslation<Tree> {
@Nonnull private static final Logger LOGGER = Loggers.get(JavaLanguageTranslation.class);
@Nonnull
private static final Logger LOGGER = LoggerFactory.getLogger(JavaLanguageTranslation.class);

@Nonnull
@Override
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,11 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class ExportRules {

private static final Logger LOGGER = Loggers.get(ExportRules.class);
private static final Logger LOGGER = LoggerFactory.getLogger(ExportRules.class);

protected <T> void exportToJSON(
List<IDetectionRule<T>> listOfEntryPoints, FileWriter fileWriter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,18 @@
import java.util.List;
import java.util.Set;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.apache.commons.lang3.StringUtils;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DetectionStoreLogger<R, T, S, P> {
private static final Logger LOGGER = Loggers.get(DetectionStoreLogger.class);
private static final Logger LOGGER = LoggerFactory.getLogger(DetectionStoreLogger.class);

public void print(@Nonnull DetectionStore<R, T, S, P> rootDetectionStore) {
printDstoreValues(0, List.of(rootDetectionStore));
}

private int truncateSizeForLongNumbers = 6;
private static final int TRUNCATE_SIZE_FOR_LONG_NUMBERS = 6;

private void printDstoreValues(
int tabs, @Nonnull List<DetectionStore<R, T, S, P>> detectionStores) {
Expand All @@ -59,16 +58,12 @@ private void printDstoreValues(
store.getDetectionRule()
.bundle()
.getIdentifier()
.hashCode(),
true,
truncateSizeForLongNumbers)
.hashCode())
+ ", level: "
+ store.getLevel()
+ ", hash: "
+ getFormattedNumericString(
store.hashCode(),
true,
truncateSizeForLongNumbers)
store.hashCode())
+ "] "
+ " ".repeat(Math.max(0, tabs))
+ (tabs > 0 ? "└─ " : "")
Expand Down Expand Up @@ -97,16 +92,12 @@ private void printDstoreValues(
store.getDetectionRule()
.bundle()
.getIdentifier()
.hashCode(),
true,
truncateSizeForLongNumbers)
.hashCode())
+ ", level: "
+ store.getLevel()
+ ", hash: "
+ getFormattedNumericString(
store.hashCode(),
true,
truncateSizeForLongNumbers)
store.hashCode())
+ "] "
+ " ".repeat(Math.max(0, tabs))
+ (tabs > 0 ? "└─ " : "")
Expand Down Expand Up @@ -161,26 +152,25 @@ private String getDetectionValueContextMessage(
}

@Nonnull
String getFormattedNumericString(
int hashInt, boolean canBeNegative, @Nullable Integer maxCharacters) {
String getFormattedNumericString(int hashInt) {
String res = "";
if (canBeNegative && hashInt >= 0) {
if (hashInt >= 0) {
res += "";
}
res += Integer.toString(hashInt);

if (maxCharacters != null) {
if (maxCharacters < 3) {
throw new IllegalArgumentException(
"Max characters must be greater than or equal to 3");
} else if (res.length() > maxCharacters) {
res = res.substring(0, maxCharacters - 1) + "…";
} else if (res.length() < maxCharacters) {
res += StringUtils.repeat(" ", maxCharacters - res.length());
}
if (DetectionStoreLogger.TRUNCATE_SIZE_FOR_LONG_NUMBERS < 3) {
throw new IllegalArgumentException("Max characters must be greater than or equal to 3");
} else if (res.length() > DetectionStoreLogger.TRUNCATE_SIZE_FOR_LONG_NUMBERS) {
res = res.substring(0, DetectionStoreLogger.TRUNCATE_SIZE_FOR_LONG_NUMBERS - 1) + "…";
} else if (res.length() < DetectionStoreLogger.TRUNCATE_SIZE_FOR_LONG_NUMBERS) {
res +=
StringUtils.repeat(
" ",
DetectionStoreLogger.TRUNCATE_SIZE_FOR_LONG_NUMBERS - res.length());
}
if (maxCharacters != null && res.length() > maxCharacters) {
res = res.substring(0, maxCharacters - 1) + "…";
if (res.length() > DetectionStoreLogger.TRUNCATE_SIZE_FOR_LONG_NUMBERS) {
res = res.substring(0, DetectionStoreLogger.TRUNCATE_SIZE_FOR_LONG_NUMBERS - 1) + "…";
}
return res;
}
Expand Down
4 changes: 2 additions & 2 deletions enricher/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.ibm</groupId>
<artifactId>sonar-cryptography</artifactId>
<version>1.1.4-SNAPSHOT</version>
<version>2.0.0-SNAPSHOT</version>
</parent>

<artifactId>enricher</artifactId>
Expand All @@ -21,7 +21,7 @@
<dependency>
<groupId>com.ibm</groupId>
<artifactId>mapper</artifactId>
<version>1.1.4-SNAPSHOT</version>
<version>2.0.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
Expand Down
8 changes: 4 additions & 4 deletions java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.ibm</groupId>
<artifactId>sonar-cryptography</artifactId>
<version>1.1.4-SNAPSHOT</version>
<version>2.0.0-SNAPSHOT</version>
</parent>

<artifactId>java</artifactId>
Expand All @@ -21,12 +21,12 @@
<dependency>
<groupId>com.ibm</groupId>
<artifactId>engine</artifactId>
<version>1.1.4-SNAPSHOT</version>
<version>2.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.ibm</groupId>
<artifactId>output</artifactId>
<version>1.1.4-SNAPSHOT</version>
<version>2.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.sonarsource.java</groupId>
Expand All @@ -37,7 +37,7 @@
<dependency>
<groupId>com.ibm</groupId>
<artifactId>enricher</artifactId>
<version>1.1.4-SNAPSHOT</version>
<version>2.0.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,11 @@
import java.util.Map;
import javax.annotation.Nonnull;
import org.jetbrains.annotations.Unmodifiable;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class MacReorganizer {

private static final Logger LOGGER = Loggers.get(MacReorganizer.class);
private static final Logger LOGGER = LoggerFactory.getLogger(MacReorganizer.class);

private MacReorganizer() {
// private
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.jetbrains.annotations.Unmodifiable;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.plugins.java.api.JavaCheck;
import org.sonar.plugins.java.api.JavaFileScannerContext;
import org.sonar.plugins.java.api.location.Position;
Expand All @@ -48,7 +48,7 @@ public final class JavaTranslator

public static final String UNKNOWN = "unknown";

private static final Logger LOGGER = Loggers.get(JavaTranslator.class);
private static final Logger LOGGER = LoggerFactory.getLogger(JavaTranslator.class);
@Nonnull private final JavaMapperConfig javaMapperConfig = new JavaMapperConfig();

public JavaTranslator(@Nonnull JavaCheck rule) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,13 @@
import com.ibm.plugin.translation.translator.JavaTranslator;
import java.util.Optional;
import org.jetbrains.annotations.NotNull;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.plugins.java.api.tree.Tree;

public final class JavaMacContextTranslator extends AbstractContextTranslator
implements IContextTranslationWithKind<Tree, MacContext.Kind> {

private static final Logger LOGGER = Loggers.get(JavaMacContextTranslator.class);
private static final Logger LOGGER = LoggerFactory.getLogger(JavaMacContextTranslator.class);

public JavaMacContextTranslator(@NotNull Configuration configuration) {
super(configuration);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
import java.io.FileWriter;
import java.io.IOException;
import org.junit.jupiter.api.Test;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/* This class is not really a test, but is used to export all Java rules to a JSON file */
class ExportJavaRulesToJsonTest extends ExportRules {
private static final Logger LOGGER = Loggers.get(ExportJavaRulesToJsonTest.class);
private static final Logger LOGGER = LoggerFactory.getLogger(ExportJavaRulesToJsonTest.class);

@Test
void test() {
Expand Down
11 changes: 4 additions & 7 deletions java/src/test/java/com/ibm/plugin/TestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,14 @@
import javax.annotation.Nullable;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.BeforeEach;
import org.sonar.api.utils.log.LogTester;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.LoggerLevel;
import org.sonar.api.utils.log.Loggers;
import org.slf4j.event.Level;
import org.sonar.api.testfixtures.log.LogTesterJUnit5;
import org.sonar.plugins.java.api.JavaCheck;
import org.sonar.plugins.java.api.JavaFileScannerContext;
import org.sonar.plugins.java.api.semantic.Symbol;
import org.sonar.plugins.java.api.tree.Tree;

public abstract class TestBase extends JavaInventoryRule {
protected final LogTester logTester = new LogTester();
private static final Logger LOGGER = Loggers.get(TestBase.class);

@Nonnull
private final DetectionStoreLogger<JavaCheck, Tree, Symbol, JavaFileScannerContext>
Expand All @@ -69,7 +65,8 @@ public void resetState() {

@BeforeEach
public void debug() {
logTester.setLevel(LoggerLevel.DEBUG);
LogTesterJUnit5 logTesterJUnit5 = new LogTesterJUnit5();
logTesterJUnit5.setLevel(Level.DEBUG);
}

@Override
Expand Down
Loading
Loading