Skip to content

Commit b2d55dc

Browse files
committed
feat: allow configuration of logging on cli
1 parent 0ed9150 commit b2d55dc

File tree

3 files changed

+94
-26
lines changed

3 files changed

+94
-26
lines changed

README.md

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -106,27 +106,30 @@ output =
106106
/_/
107107
108108
109-
Usage: spotless [-hV] [-e=<encoding>] [-l=<lineEnding>] [-m=<spotlessMode>]
110-
[-p=N] [-t=<targets>]... [FORMATTING_STEPS]
109+
Usage: spotless [-hV] [-e=<encoding>] [-l=<lineEnding>] [--log-file=<logFile>]
110+
[-m=<spotlessMode>] [-p=N] [-t=<targets>]... [-q | -v [-v]...]
111+
[FORMATTING_STEPS]
111112
Runs spotless
112-
-e, --encoding=<encoding>
113-
The encoding of the files to format.
114-
(default: UTF-8)
115-
-h, --help Show this help message and exit.
113+
-e, --encoding=<encoding> The encoding of the files to format.
114+
(default: UTF-8)
115+
-h, --help Show this help message and exit.
116116
-l, --line-ending=<lineEnding>
117-
The line ending of the files to format.
118-
One of: GIT_ATTRIBUTES,
119-
GIT_ATTRIBUTES_FAST_ALLSAME, PLATFORM_NATIVE,
120-
WINDOWS, UNIX, MAC_CLASSIC, PRESERVE
121-
(default: UNIX)
122-
-m, --mode=<spotlessMode>
123-
The mode to run spotless in.
124-
One of: CHECK, APPLY
125-
(default: APPLY)
126-
-p, --parallelity=N The number of parallel formatter threads to run.
127-
(default: #cores * 0.5)
128-
-t, --target=<targets> The target files to format.
129-
-V, --version Print version information and exit.
117+
The line ending of the files to format.
118+
One of: GIT_ATTRIBUTES,
119+
GIT_ATTRIBUTES_FAST_ALLSAME, PLATFORM_NATIVE,
120+
WINDOWS, UNIX, MAC_CLASSIC, PRESERVE
121+
(default: UNIX)
122+
--log-file=<logFile> The log file to write the output to.
123+
-m, --mode=<spotlessMode> The mode to run spotless in.
124+
One of: CHECK, APPLY
125+
(default: APPLY)
126+
-p, --parallelity=N The number of parallel formatter threads to run.
127+
(default: #cores * 0.5)
128+
-q, --quiet Disable as much output as possible.
129+
-t, --target=<targets> The target files to format.
130+
-v Enable verbose output. Multiple -v options
131+
increase the verbosity (max 5).
132+
-V, --version Print version information and exit.
130133
131134
Available formatting steps:
132135
license-header Runs license header

app/src/main/java/com/diffplug/spotless/cli/SpotlessCLI.java

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.diffplug.spotless.cli;
1717

18+
import java.io.File;
1819
import java.nio.charset.Charset;
1920
import java.nio.file.Path;
2021
import java.util.List;
@@ -119,10 +120,55 @@ public void setParallelity(int parallelity) {
119120
this.parallelity = parallelity;
120121
}
121122

123+
@CommandLine.ArgGroup(exclusive = true, multiplicity = "0..1")
124+
LoggingLevelOptions loggingLevelOptions;
125+
126+
class LoggingLevelOptions {
127+
128+
private boolean[] verbosity;
129+
130+
@CommandLine.Option(
131+
names = {"-v"},
132+
description = "Enable verbose output. Multiple -v options increase the verbosity (max 5).",
133+
arity = "0")
134+
public void setVerbose(boolean[] verbosity) {
135+
if (verbosity.length > 5) {
136+
throw new CommandLine.ParameterException(
137+
spec.commandLine(), "Error: --verbose can be used at most 5 times");
138+
}
139+
this.verbosity = verbosity;
140+
}
141+
142+
@CommandLine.Option(
143+
names = {"--quiet", "-q"},
144+
description = "Disable as much output as possible.",
145+
arity = "0")
146+
boolean quiet;
147+
148+
LoggingConfigurer.CLIOutputLevel toCliOutputLevel() {
149+
if (quiet) {
150+
return LoggingConfigurer.CLIOutputLevel.QUIET;
151+
}
152+
if (verbosity == null) {
153+
return LoggingConfigurer.CLIOutputLevel.DEFAULT;
154+
}
155+
int verbosityCount = this.verbosity.length;
156+
return LoggingConfigurer.CLIOutputLevel.verbosity(verbosityCount);
157+
}
158+
}
159+
160+
@CommandLine.Option(
161+
names = {"--log-file"},
162+
description = "The log file to write the output to.")
163+
File logFile;
164+
122165
@Override
123166
public void setupLogging() {
124167
LoggingConfigurer.configureLogging(
125-
LoggingConfigurer.CLIOutputLevel.VVVVV, null); // TODO add options to set that
168+
loggingLevelOptions != null
169+
? loggingLevelOptions.toCliOutputLevel()
170+
: LoggingConfigurer.CLIOutputLevel.DEFAULT,
171+
logFile);
126172
}
127173

128174
@Override

app/src/main/java/com/diffplug/spotless/cli/logging/output/LoggingConfigurer.java

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,31 @@ private static void configureJdkLogging(@NotNull CLIOutputLevel cliOutputLevel,
9494
}
9595

9696
public enum CLIOutputLevel {
97-
VVVVV, // everything, even debug levels
98-
VVVV, // spotless on debug, everything else non- debug levels
99-
VVV, // everything spotless, even debug levels
100-
VV, // everything spotless, except debug levels
101-
V, // only info and above
97+
VVVVV(5), // everything, even debug levels
98+
VVVV(4), // spotless on debug, everything else non- debug levels
99+
VVV(3), // everything spotless, even debug levels
100+
VV(2), // everything spotless, except debug levels
101+
V(1), // only info and above
102102
DEFAULT, // only warnings and above
103-
QUIET, // only errors and above
103+
QUIET; // only errors and above
104+
105+
private final int verbosity;
106+
107+
CLIOutputLevel(int verbosity) {
108+
this.verbosity = verbosity;
109+
}
110+
111+
CLIOutputLevel() {
112+
this(-1);
113+
}
114+
115+
public static CLIOutputLevel verbosity(int verbosity) {
116+
for (CLIOutputLevel level : CLIOutputLevel.values()) {
117+
if (level.verbosity == verbosity) {
118+
return level;
119+
}
120+
}
121+
throw new IllegalArgumentException("Unknown verbosity " + verbosity);
122+
}
104123
}
105124
}

0 commit comments

Comments
 (0)