Skip to content

Commit 080f237

Browse files
committedFeb 9, 2023
1162: use slf4j for logging
1 parent e0a290a commit 080f237

File tree

5 files changed

+52
-50
lines changed

5 files changed

+52
-50
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2023 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless;
17+
18+
import java.util.function.Supplier;
19+
20+
/**
21+
* This is a utility class to allow for lazy evaluation of arguments to be passed to a logger
22+
* and thus avoid unnecessary computation of the arguments if the log level is not enabled.
23+
*/
24+
public final class LazyArgLogger {
25+
26+
private final Supplier<Object> argSupplier;
27+
28+
private LazyArgLogger(Supplier<Object> argSupplier) {
29+
this.argSupplier = argSupplier;
30+
}
31+
32+
public static LazyArgLogger lazy(Supplier<Object> argSupplier) {
33+
return new LazyArgLogger(argSupplier);
34+
}
35+
36+
@Override
37+
public String toString() {
38+
return String.valueOf(argSupplier.get());
39+
}
40+
}

‎lib/src/main/java/com/diffplug/spotless/npm/EslintFormatterStep.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.diffplug.spotless.npm;
1717

18+
import static com.diffplug.spotless.LazyArgLogger.lazy;
1819
import static java.util.Objects.requireNonNull;
1920

2021
import java.io.File;
@@ -115,7 +116,7 @@ protected void prepareNodeServerLayout() throws IOException {
115116
// If any config files are provided, we need to make sure they are at the same location as the node modules
116117
// as eslint will try to resolve plugin/config names relatively to the config file location and some
117118
// eslint configs contain relative paths to additional config files (such as tsconfig.json e.g.)
118-
FormattedPrinter.SYSOUT.print("Copying config file <%s> to <%s> and using the copy", origEslintConfig.getEslintConfigPath(), nodeServerLayout.nodeModulesDir());
119+
logger.info("Copying config file <{}> to <{}> and using the copy", origEslintConfig.getEslintConfigPath(), nodeServerLayout.nodeModulesDir());
119120
File configFileCopy = NpmResourceHelper.copyFileToDir(origEslintConfig.getEslintConfigPath(), nodeServerLayout.nodeModulesDir());
120121
this.eslintConfigInUse = this.origEslintConfig.withEslintConfigPath(configFileCopy).verify();
121122
}
@@ -125,7 +126,7 @@ protected void prepareNodeServerLayout() throws IOException {
125126
@Nonnull
126127
public FormatterFunc createFormatterFunc() {
127128
try {
128-
FormattedPrinter.SYSOUT.print("creating formatter function (starting server)");
129+
logger.info("Creating formatter function (starting server)");
129130
ServerProcessInfo eslintRestServer = npmRunServer();
130131
EslintRestService restService = new EslintRestService(eslintRestServer.getBaseUrl());
131132
return Closeable.ofDangerous(() -> endServer(restService, eslintRestServer), new EslintFilePathPassingFormatterFunc(locations.projectDir(), nodeServerLayout.nodeModulesDir(), eslintConfigInUse, restService));
@@ -135,7 +136,7 @@ public FormatterFunc createFormatterFunc() {
135136
}
136137

137138
private void endServer(BaseNpmRestService restService, ServerProcessInfo restServer) throws Exception {
138-
FormattedPrinter.SYSOUT.print("Closing formatting function (ending server).");
139+
logger.info("Closing formatting function (ending server).");
139140
try {
140141
restService.shutdown();
141142
} catch (Throwable t) {
@@ -161,7 +162,7 @@ public EslintFilePathPassingFormatterFunc(File projectDir, File nodeModulesDir,
161162

162163
@Override
163164
public String applyWithFile(String unix, File file) throws Exception {
164-
FormattedPrinter.SYSOUT.print("formatting String '" + unix.substring(0, Math.min(50, unix.length())) + "[...]' in file '" + file + "'");
165+
logger.info("formatting String '{}[...]' in file '{}'", lazy(() -> unix.substring(0, Math.min(50, unix.length()))), file);
165166

166167
Map<FormatOption, Object> eslintCallOptions = new HashMap<>();
167168
setConfigToCallOptions(eslintCallOptions);

‎lib/src/main/java/com/diffplug/spotless/npm/FormattedPrinter.java

-42
This file was deleted.

‎lib/src/main/java/com/diffplug/spotless/npm/PrettierFormatterStep.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.diffplug.spotless.npm;
1717

18+
import static com.diffplug.spotless.LazyArgLogger.lazy;
1819
import static java.util.Objects.requireNonNull;
1920

2021
import java.io.File;
@@ -86,7 +87,7 @@ private static class State extends NpmFormatterStepStateBase implements Serializ
8687
@Nonnull
8788
public FormatterFunc createFormatterFunc() {
8889
try {
89-
FormattedPrinter.SYSOUT.print("creating formatter function (starting server)");
90+
logger.info("creating formatter function (starting server)");
9091
ServerProcessInfo prettierRestServer = npmRunServer();
9192
PrettierRestService restService = new PrettierRestService(prettierRestServer.getBaseUrl());
9293
String prettierConfigOptions = restService.resolveConfig(this.prettierConfig.getPrettierConfigPath(), this.prettierConfig.getOptions());
@@ -97,7 +98,7 @@ public FormatterFunc createFormatterFunc() {
9798
}
9899

99100
private void endServer(PrettierRestService restService, ServerProcessInfo restServer) throws Exception {
100-
FormattedPrinter.SYSOUT.print("Closing formatting function (ending server).");
101+
logger.info("Closing formatting function (ending server).");
101102
try {
102103
restService.shutdown();
103104
} catch (Throwable t) {
@@ -119,7 +120,7 @@ public PrettierFilePathPassingFormatterFunc(String prettierConfigOptions, Pretti
119120

120121
@Override
121122
public String applyWithFile(String unix, File file) throws Exception {
122-
FormattedPrinter.SYSOUT.print("formatting String '" + unix.substring(0, Math.min(50, unix.length())) + "[...]' in file '" + file + "'");
123+
logger.info("formatting String '{}[...]' in file '{}'", lazy(() -> unix.substring(0, Math.min(50, unix.length()))), file);
123124

124125
final String prettierConfigOptionsWithFilepath = assertFilepathInConfigOptions(file);
125126
try {

‎plugin-gradle/src/test/java/com/diffplug/gradle/spotless/PrettierIntegrationTest.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,10 @@ void usePhpAndJavaCommunityPlugin() throws IOException {
219219
"}");
220220
setFile("php-example.php").toResource("npm/prettier/plugins/php.dirty");
221221
setFile("JavaTest.java").toResource("npm/prettier/plugins/java-test.dirty");
222-
final BuildResult spotlessApply = gradleRunner().withArguments("--stacktrace", "spotlessApply").build();
222+
final BuildResult spotlessApply = gradleRunner().forwardOutput().withArguments("--stacktrace", "--info", "spotlessApply").build();
223223
Assertions.assertThat(spotlessApply.getOutput()).contains("BUILD SUCCESSFUL");
224+
final BuildResult spotlessApply2 = gradleRunner().forwardOutput().withArguments("--stacktrace", "--info", "spotlessApply").build();
225+
Assertions.assertThat(spotlessApply2.getOutput()).contains("BUILD SUCCESSFUL");
224226
assertFile("php-example.php").sameAsResource("npm/prettier/plugins/php.clean");
225227
assertFile("JavaTest.java").sameAsResource("npm/prettier/plugins/java-test.clean");
226228
}

0 commit comments

Comments
 (0)