aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/sonarsource
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/main/java/org/sonarsource
parent9688ddf5c73288621cb431052196d0e7cae6080c (diff)
downloadsonar-scanner-cli-90a5df59eddccf630deec66f8d487dd1d69114a9.tar.gz
sonar-scanner-cli-90a5df59eddccf630deec66f8d487dd1d69114a9.zip
SCANCLI-146 Log using slf4j + logback
Diffstat (limited to 'src/main/java/org/sonarsource')
-rw-r--r--src/main/java/org/sonarsource/scanner/cli/Cli.java42
-rw-r--r--src/main/java/org/sonarsource/scanner/cli/Conf.java27
-rw-r--r--src/main/java/org/sonarsource/scanner/cli/Logs.java114
-rw-r--r--src/main/java/org/sonarsource/scanner/cli/Main.java53
-rw-r--r--src/main/java/org/sonarsource/scanner/cli/PropertyResolver.java4
-rw-r--r--src/main/java/org/sonarsource/scanner/cli/ScannerEngineBootstrapperFactory.java8
-rw-r--r--src/main/java/org/sonarsource/scanner/cli/ScannerVersion.java7
-rw-r--r--src/main/java/org/sonarsource/scanner/cli/Slf4jLogOutput.java50
-rw-r--r--src/main/java/org/sonarsource/scanner/cli/Stats.java13
-rw-r--r--src/main/java/org/sonarsource/scanner/cli/SystemInfo.java33
10 files changed, 145 insertions, 206 deletions
diff --git a/src/main/java/org/sonarsource/scanner/cli/Cli.java b/src/main/java/org/sonarsource/scanner/cli/Cli.java
index 4de2a36..a39b0af 100644
--- a/src/main/java/org/sonarsource/scanner/cli/Cli.java
+++ b/src/main/java/org/sonarsource/scanner/cli/Cli.java
@@ -19,23 +19,26 @@
*/
package org.sonarsource.scanner.cli;
+import ch.qos.logback.classic.Level;
import java.util.Properties;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import static java.util.Arrays.asList;
class Cli {
+ private static final Logger LOG = LoggerFactory.getLogger(Cli.class);
+
private boolean debugEnabled = false;
private boolean displayVersionOnly = false;
private boolean embedded = false;
private String invokedFrom = "";
private final Properties props = new Properties();
private final Exit exit;
- private final Logs logger;
- public Cli(Exit exit, Logs logger) {
+ public Cli(Exit exit) {
this.exit = exit;
- this.logger = logger;
}
boolean isDebugEnabled() {
@@ -80,19 +83,19 @@ class Cli {
displayVersionOnly = true;
} else if (asList("-e", "--errors").contains(arg)) {
- logger
+ LOG
.info("Option -e/--errors is no longer supported and will be ignored");
} else if (asList("-X", "--debug").contains(arg)) {
props.setProperty("sonar.verbose", "true");
debugEnabled = true;
- logger.setDebugEnabled(true);
-
+ var rootLogger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
+ rootLogger.setLevel(Level.DEBUG);
} else if (asList("-D", "--define").contains(arg)) {
return processProp(args, pos);
} else if ("--embedded".equals(arg)) {
- logger.info(
+ LOG.info(
"Option --embedded is deprecated and will be removed in a future release.");
embedded = true;
@@ -128,7 +131,7 @@ class Cli {
displayVersionOnly = false;
}
- private void appendPropertyTo(String arg, Properties props) {
+ private static void appendPropertyTo(String arg, Properties props) {
final String key;
final String value;
int j = arg.indexOf('=');
@@ -141,25 +144,24 @@ class Cli {
}
Object oldValue = props.setProperty(key, value);
if (oldValue != null) {
- logger.warn("Property '" + key + "' with value '" + oldValue + "' is "
- + "overridden with value '" + value + "'");
+ LOG.warn("Property '{}' with value '{}' is overridden with value '{}'", key, oldValue, value);
}
}
private void printErrorAndExit(String message) {
- logger.error(message);
+ LOG.error(message);
printUsage();
exit.exit(Exit.INTERNAL_ERROR);
}
- private void printUsage() {
- logger.info("");
- logger.info("usage: sonar-scanner [options]");
- logger.info("");
- logger.info("Options:");
- logger.info(" -D,--define <arg> Define property");
- logger.info(" -h,--help Display help information");
- logger.info(" -v,--version Display version information");
- logger.info(" -X,--debug Produce execution debug output");
+ private static void printUsage() {
+ System.out.println();
+ System.out.println("usage: sonar-scanner [options]");
+ System.out.println();
+ System.out.println("Options:");
+ System.out.println(" -D,--define <arg> Define property");
+ System.out.println(" -h,--help Display help information");
+ System.out.println(" -v,--version Display version information");
+ System.out.println(" -X,--debug Produce execution debug output");
}
}
diff --git a/src/main/java/org/sonarsource/scanner/cli/Conf.java b/src/main/java/org/sonarsource/scanner/cli/Conf.java
index 4e9a53a..b1cbe62 100644
--- a/src/main/java/org/sonarsource/scanner/cli/Conf.java
+++ b/src/main/java/org/sonarsource/scanner/cli/Conf.java
@@ -31,9 +31,13 @@ import java.util.List;
import java.util.Map;
import java.util.Properties;
import javax.annotation.Nullable;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import org.sonarsource.scanner.lib.EnvironmentConfig;
class Conf {
+ private static final Logger LOG = LoggerFactory.getLogger(Conf.class);
+
private static final String SCANNER_HOME = "scanner.home";
private static final String SCANNER_SETTINGS = "scanner.settings";
private static final String PROJECT_HOME = "project.home";
@@ -46,13 +50,11 @@ class Conf {
private static final String BOOTSTRAP_START_TIME = "sonar.scanner.bootstrapStartTime";
private final Cli cli;
- private final Logs logger;
private final Map<String, String> env;
private final long startTimeMs;
- Conf(Cli cli, Logs logger, Map<String, String> env) {
+ Conf(Cli cli, Map<String, String> env) {
this.cli = cli;
- this.logger = logger;
this.env = env;
this.startTimeMs = System.currentTimeMillis();
}
@@ -80,7 +82,7 @@ class Conf {
}
private Map<String, String> loadEnvironmentProperties() {
- return EnvironmentConfig.load(logger.getLogOutputAdapter());
+ return EnvironmentConfig.load(new Slf4jLogOutput());
}
private Properties loadGlobalProperties() {
@@ -93,10 +95,10 @@ class Conf {
Path settingsFile = locatePropertiesFile(knownPropsAtThatPoint, SCANNER_HOME, "conf/sonar-scanner.properties",
SCANNER_SETTINGS);
if (settingsFile != null && Files.isRegularFile(settingsFile)) {
- logger.info("Scanner configuration file: " + settingsFile);
+ LOG.info("Scanner configuration file: {}", settingsFile);
return toProperties(settingsFile);
}
- logger.info("Scanner configuration file: NONE");
+ LOG.info("Scanner configuration file: NONE");
return new Properties();
}
@@ -111,10 +113,10 @@ class Conf {
Path defaultRootSettingsFile = getRootProjectBaseDir(knownPropsAtThatPoint).resolve(SONAR_PROJECT_PROPERTIES_FILENAME);
Path rootSettingsFile = locatePropertiesFile(defaultRootSettingsFile, knownPropsAtThatPoint, PROJECT_SETTINGS);
if (rootSettingsFile != null && Files.isRegularFile(rootSettingsFile)) {
- logger.info("Project root configuration file: " + rootSettingsFile);
+ LOG.info("Project root configuration file: {}", rootSettingsFile);
rootProps.putAll(toProperties(rootSettingsFile));
} else {
- logger.info("Project root configuration file: NONE");
+ LOG.info("Project root configuration file: NONE");
}
Properties projectProps = new Properties();
@@ -302,10 +304,9 @@ class Conf {
/**
* Transforms a comma-separated list String property in to a array of
* trimmed strings.
- *
+ * <p>
* This works even if they are separated by whitespace characters (space
* char, EOL, ...)
- *
*/
static String[] getListFromProperty(Properties properties, String key) {
String value = properties.getProperty(key, "").trim();
@@ -314,12 +315,12 @@ class Conf {
}
String[] values = value.split(",");
List<String> trimmedValues = new ArrayList<>();
- for (int i = 0; i < values.length; i++) {
- String trimmedValue = values[i].trim();
+ for (String s : values) {
+ String trimmedValue = s.trim();
if (!trimmedValue.isEmpty()) {
trimmedValues.add(trimmedValue);
}
}
- return trimmedValues.toArray(new String[trimmedValues.size()]);
+ return trimmedValues.toArray(new String[0]);
}
}
diff --git a/src/main/java/org/sonarsource/scanner/cli/Logs.java b/src/main/java/org/sonarsource/scanner/cli/Logs.java
deleted file mode 100644
index f8b4ce3..0000000
--- a/src/main/java/org/sonarsource/scanner/cli/Logs.java
+++ /dev/null
@@ -1,114 +0,0 @@
-/*
- * 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 org.sonarsource.scanner.cli;
-
-import java.io.PrintStream;
-import java.time.LocalTime;
-import java.time.format.DateTimeFormatter;
-import org.sonarsource.scanner.lib.LogOutput;
-
-public class Logs {
- private DateTimeFormatter timeFormatter;
- private boolean debugEnabled = false;
- private PrintStream stdOut;
- private PrintStream stdErr;
-
- public Logs(PrintStream stdOut, PrintStream stdErr) {
- this.stdErr = stdErr;
- this.stdOut = stdOut;
- this.timeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss.SSS");
- }
-
- public void setDebugEnabled(boolean debugEnabled) {
- this.debugEnabled = debugEnabled;
- }
-
- public boolean isDebugEnabled() {
- return debugEnabled;
- }
-
- public void debug(String message) {
- if (isDebugEnabled()) {
- LocalTime currentTime = LocalTime.now();
- String timestamp = currentTime.format(timeFormatter);
- stdOut.println(timestamp + " DEBUG: " + message);
- }
- }
-
- public void info(String message) {
- print(stdOut, "INFO: " + message);
- }
-
- public void warn(String message) {
- print(stdOut, "WARN: " + message);
- }
-
- public void error(String message) {
- print(stdErr, "ERROR: " + message);
- }
-
- public void error(String message, Throwable t) {
- print(stdErr, "ERROR: " + message);
- t.printStackTrace(stdErr);
- }
-
- private void print(PrintStream stream, String msg) {
- if (debugEnabled) {
- LocalTime currentTime = LocalTime.now();
- String timestamp = currentTime.format(timeFormatter);
- stream.println(timestamp + " " + msg);
- } else {
- stream.println(msg);
- }
- }
-
- /**
- * Adapter for the scanner library.
- */
- public LogOutput getLogOutputAdapter() {
- return new LogOutputAdapter(this);
- }
-
- static class LogOutputAdapter implements LogOutput {
- private final Logs logs;
-
- public LogOutputAdapter(Logs logs) {
- this.logs = logs;
- }
-
- @Override
- public void log(String formattedMessage, Level level) {
- switch (level) {
- case TRACE, DEBUG:
- logs.debug(formattedMessage);
- break;
- case ERROR:
- logs.error(formattedMessage);
- break;
- case WARN:
- logs.warn(formattedMessage);
- break;
- case INFO:
- default:
- logs.info(formattedMessage);
- }
- }
- }
-}
diff --git a/src/main/java/org/sonarsource/scanner/cli/Main.java b/src/main/java/org/sonarsource/scanner/cli/Main.java
index 0da03aa..7ec139d 100644
--- a/src/main/java/org/sonarsource/scanner/cli/Main.java
+++ b/src/main/java/org/sonarsource/scanner/cli/Main.java
@@ -19,8 +19,11 @@
*/
package org.sonarsource.scanner.cli;
+import ch.qos.logback.classic.Level;
import java.util.Map;
import java.util.Properties;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import org.sonarsource.scanner.lib.ScannerEngineBootstrapper;
import org.sonarsource.scanner.lib.ScannerEngineFacade;
import org.sonarsource.scanner.lib.ScannerProperties;
@@ -38,32 +41,31 @@ import org.sonarsource.scanner.lib.ScannerProperties;
* @since 1.0
*/
public class Main {
+ private static final Logger LOG = LoggerFactory.getLogger(Main.class);
+
private static final String SEPARATOR = "------------------------------------------------------------------------";
private final Exit exit;
private final Cli cli;
private final Conf conf;
private ScannerEngineBootstrapper scannerEngineBootstrapper;
private final ScannerEngineBootstrapperFactory bootstrapperFactory;
- private final Logs logger;
- Main(Exit exit, Cli cli, Conf conf, ScannerEngineBootstrapperFactory bootstrapperFactory, Logs logger) {
+ Main(Exit exit, Cli cli, Conf conf, ScannerEngineBootstrapperFactory bootstrapperFactory) {
this.exit = exit;
this.cli = cli;
this.conf = conf;
this.bootstrapperFactory = bootstrapperFactory;
- this.logger = logger;
}
public static void main(String[] args) {
- Logs logs = new Logs(System.out, System.err);
Exit exit = new Exit();
- Cli cli = new Cli(exit, logs).parse(args);
- Main main = new Main(exit, cli, new Conf(cli, logs, System.getenv()), new ScannerEngineBootstrapperFactory(logs), logs);
+ Cli cli = new Cli(exit).parse(args);
+ Main main = new Main(exit, cli, new Conf(cli, System.getenv()), new ScannerEngineBootstrapperFactory());
main.analyze();
}
void analyze() {
- Stats stats = new Stats(logger).start();
+ Stats stats = new Stats().start();
int status = Exit.INTERNAL_ERROR;
try {
@@ -86,24 +88,24 @@ public class Main {
}
}
- private void logServerType(ScannerEngineFacade engine) {
+ private static void logServerType(ScannerEngineFacade engine) {
if (engine.isSonarCloud()) {
- logger.info("Communicating with SonarCloud");
+ LOG.info("Communicating with SonarCloud");
} else {
String serverVersion = engine.getServerVersion();
- logger.info(String.format("Communicating with SonarQube Server %s", serverVersion));
+ LOG.info("Communicating with SonarQube Server {}", serverVersion);
}
}
private void checkSkip(Properties properties) {
if ("true".equalsIgnoreCase(properties.getProperty(ScannerProperties.SKIP))) {
- logger.info("SonarScanner CLI analysis skipped");
+ LOG.info("SonarScanner CLI analysis skipped");
exit.exit(Exit.SUCCESS);
}
}
private void init(Properties p) {
- SystemInfo.print(logger);
+ SystemInfo.print();
if (cli.isDisplayVersionOnly()) {
exit.exit(Exit.SUCCESS);
}
@@ -111,39 +113,40 @@ public class Main {
scannerEngineBootstrapper = bootstrapperFactory.create(p, cli.getInvokedFrom());
}
- private void configureLogging(Properties props) {
+ private static void configureLogging(Properties props) {
if ("true".equals(props.getProperty("sonar.verbose"))
|| "DEBUG".equalsIgnoreCase(props.getProperty("sonar.log.level"))
|| "TRACE".equalsIgnoreCase(props.getProperty("sonar.log.level"))) {
- logger.setDebugEnabled(true);
+ var rootLogger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
+ rootLogger.setLevel(Level.DEBUG);
}
}
- private void displayExecutionResult(Stats stats, String resultMsg) {
- logger.info(SEPARATOR);
- logger.info("EXECUTION " + resultMsg);
- logger.info(SEPARATOR);
+ private static void displayExecutionResult(Stats stats, String resultMsg) {
+ LOG.info(SEPARATOR);
+ LOG.info("EXECUTION {}", resultMsg);
+ LOG.info(SEPARATOR);
stats.stop();
- logger.info(SEPARATOR);
+ LOG.info(SEPARATOR);
}
private void showError(String message, Throwable e, boolean debug) {
if (debug || !isUserError(e)) {
- logger.error(message, e);
+ LOG.error(message, e);
} else {
- logger.error(message);
- logger.error(e.getMessage());
+ LOG.error(message);
+ LOG.error(e.getMessage());
String previousMsg = "";
for (Throwable cause = e.getCause(); cause != null
&& cause.getMessage() != null
&& !cause.getMessage().equals(previousMsg); cause = cause.getCause()) {
- logger.error("Caused by: " + cause.getMessage());
+ LOG.error("Caused by: {}", cause.getMessage());
previousMsg = cause.getMessage();
}
}
if (!cli.isDebugEnabled()) {
- logger.error("");
+ LOG.error("");
suggestDebugMode();
}
}
@@ -155,7 +158,7 @@ public class Main {
private void suggestDebugMode() {
if (!cli.isEmbedded()) {
- logger.error("Re-run SonarScanner CLI using the -X switch to enable full debug logging.");
+ LOG.error("Re-run SonarScanner CLI using the -X switch to enable full debug logging.");
}
}
diff --git a/src/main/java/org/sonarsource/scanner/cli/PropertyResolver.java b/src/main/java/org/sonarsource/scanner/cli/PropertyResolver.java
index 7e88d19..0b33f8b 100644
--- a/src/main/java/org/sonarsource/scanner/cli/PropertyResolver.java
+++ b/src/main/java/org/sonarsource/scanner/cli/PropertyResolver.java
@@ -31,7 +31,7 @@ public class PropertyResolver {
private final Properties props;
private final Properties resolved;
private final List<String> queue;
- private Map<String, String> env;
+ private final Map<String, String> env;
public PropertyResolver(Properties props, Map<String, String> env) {
this.props = props;
@@ -72,7 +72,7 @@ public class PropertyResolver {
}
Matcher m = placeholderPattern.matcher(propValue);
- StringBuffer sb = new StringBuffer();
+ var sb = new StringBuilder();
while (m.find()) {
String varName = (null == m.group(1)) ? m.group(2) : m.group(1);
diff --git a/src/main/java/org/sonarsource/scanner/cli/ScannerEngineBootstrapperFactory.java b/src/main/java/org/sonarsource/scanner/cli/ScannerEngineBootstrapperFactory.java
index f9e7fe7..63ee9d0 100644
--- a/src/main/java/org/sonarsource/scanner/cli/ScannerEngineBootstrapperFactory.java
+++ b/src/main/java/org/sonarsource/scanner/cli/ScannerEngineBootstrapperFactory.java
@@ -25,12 +25,6 @@ import org.sonarsource.scanner.lib.ScannerEngineBootstrapper;
class ScannerEngineBootstrapperFactory {
- private final Logs logger;
-
- public ScannerEngineBootstrapperFactory(Logs logger) {
- this.logger = logger;
- }
-
ScannerEngineBootstrapper create(Properties props, String isInvokedFrom) {
String appName = "ScannerCLI";
String appVersion = ScannerVersion.version();
@@ -44,7 +38,7 @@ class ScannerEngineBootstrapperFactory {
}
ScannerEngineBootstrapper newScannerEngineBootstrapper(String appName, String appVersion) {
- return ScannerEngineBootstrapper.create(appName, appVersion, logger.getLogOutputAdapter());
+ return ScannerEngineBootstrapper.create(appName, appVersion, new Slf4jLogOutput());
}
diff --git a/src/main/java/org/sonarsource/scanner/cli/ScannerVersion.java b/src/main/java/org/sonarsource/scanner/cli/ScannerVersion.java
index a5f3ede..831841b 100644
--- a/src/main/java/org/sonarsource/scanner/cli/ScannerVersion.java
+++ b/src/main/java/org/sonarsource/scanner/cli/ScannerVersion.java
@@ -19,16 +19,19 @@
*/
package org.sonarsource.scanner.cli;
+import java.nio.charset.StandardCharsets;
import java.util.Scanner;
+import static java.util.Objects.requireNonNull;
+
public enum ScannerVersion {
INSTANCE;
- private String version;
+ private final String version;
ScannerVersion() {
- try (Scanner scanner = new Scanner(getClass().getResourceAsStream("/version.txt"), "UTF-8")) {
+ try (Scanner scanner = new Scanner(requireNonNull(getClass().getResourceAsStream("/version.txt")), StandardCharsets.UTF_8)) {
this.version = scanner.next();
}
}
diff --git a/src/main/java/org/sonarsource/scanner/cli/Slf4jLogOutput.java b/src/main/java/org/sonarsource/scanner/cli/Slf4jLogOutput.java
new file mode 100644
index 0000000..2ce5cba
--- /dev/null
+++ b/src/main/java/org/sonarsource/scanner/cli/Slf4jLogOutput.java
@@ -0,0 +1,50 @@
+/*
+ * 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 org.sonarsource.scanner.cli;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.sonarsource.scanner.lib.LogOutput;
+
+public class Slf4jLogOutput implements LogOutput {
+
+ private static final Logger LOG = LoggerFactory.getLogger(Slf4jLogOutput.class);
+
+ @Override
+ public void log(String s, Level level) {
+ switch (level) {
+ case TRACE:
+ LOG.trace(s);
+ break;
+ case DEBUG:
+ LOG.debug(s);
+ break;
+ case INFO:
+ LOG.info(s);
+ break;
+ case WARN:
+ LOG.warn(s);
+ break;
+ case ERROR:
+ LOG.error(s);
+ break;
+ }
+ }
+}
diff --git a/src/main/java/org/sonarsource/scanner/cli/Stats.java b/src/main/java/org/sonarsource/scanner/cli/Stats.java
index 128877b..0d9913a 100644
--- a/src/main/java/org/sonarsource/scanner/cli/Stats.java
+++ b/src/main/java/org/sonarsource/scanner/cli/Stats.java
@@ -19,14 +19,13 @@
*/
package org.sonarsource.scanner.cli;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
class Stats {
- private final Logs logger;
+ private static final Logger LOG = LoggerFactory.getLogger(Stats.class);
private long startTime;
- Stats(Logs logger) {
- this.logger = logger;
- }
-
Stats start() {
startTime = System.currentTimeMillis();
return this;
@@ -34,12 +33,12 @@ class Stats {
Stats stop() {
long stopTime = System.currentTimeMillis() - startTime;
- logger.info("Total time: " + formatTime(stopTime));
+ LOG.atInfo().addArgument(() -> formatTime(stopTime)).log("Total time: {}");
System.gc();
Runtime r = Runtime.getRuntime();
long mb = 1024L * 1024;
- logger.info("Final Memory: " + (r.totalMemory() - r.freeMemory()) / mb + "M/" + r.totalMemory() / mb + "M");
+ LOG.atInfo().addArgument((r.totalMemory() - r.freeMemory()) / mb + "M/" + r.totalMemory() / mb + "M").log("Final Memory: {}");
return this;
}
diff --git a/src/main/java/org/sonarsource/scanner/cli/SystemInfo.java b/src/main/java/org/sonarsource/scanner/cli/SystemInfo.java
index 30abee5..205a902 100644
--- a/src/main/java/org/sonarsource/scanner/cli/SystemInfo.java
+++ b/src/main/java/org/sonarsource/scanner/cli/SystemInfo.java
@@ -22,12 +22,16 @@ package org.sonarsource.scanner.cli;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
class SystemInfo {
+ private static final Logger LOG = LoggerFactory.getLogger(SystemInfo.class);
+
private static final Set<String> SENSITIVE_JVM_ARGUMENTS = Set.of(
- "-Dsonar.login",
- "-Dsonar.password",
- "-Dsonar.token");
+ "-Dsonar.login",
+ "-Dsonar.password",
+ "-Dsonar.token");
private static final Pattern PATTERN_ARGUMENT_SEPARATOR = Pattern.compile("\\s+");
private static System2 system = new System2();
@@ -38,13 +42,13 @@ class SystemInfo {
SystemInfo.system = system;
}
- static void print(Logs logger) {
- logger.info("SonarScanner CLI " + ScannerVersion.version());
- logger.info(java());
- logger.info(os());
+ static void print() {
+ LOG.info("SonarScanner CLI {}", ScannerVersion.version());
+ LOG.atInfo().log(SystemInfo::java);
+ LOG.atInfo().log(SystemInfo::os);
String scannerOpts = system.getenv("SONAR_SCANNER_OPTS");
if (scannerOpts != null) {
- logger.info("SONAR_SCANNER_OPTS=" + redactSensitiveArguments(scannerOpts));
+ LOG.atInfo().addArgument(() -> redactSensitiveArguments(scannerOpts)).log("SONAR_SCANNER_OPTS={}");
}
}
@@ -77,14 +81,11 @@ class SystemInfo {
}
static String os() {
- StringBuilder sb = new StringBuilder();
- sb
- .append(system.getProperty("os.name"))
- .append(" ")
- .append(system.getProperty("os.version"))
- .append(" ")
- .append(system.getProperty("os.arch"));
- return sb.toString();
+ return system.getProperty("os.name")
+ + " "
+ + system.getProperty("os.version")
+ + " "
+ + system.getProperty("os.arch");
}
static class System2 {