|
15 | 15 | */ |
16 | 16 | package io.qameta.allure; |
17 | 17 |
|
18 | | -import ch.qos.logback.classic.Level; |
19 | 18 | import com.beust.jcommander.JCommander; |
20 | 19 | import com.beust.jcommander.ParameterException; |
21 | 20 | import io.qameta.allure.command.GenerateCommand; |
|
26 | 25 | import org.slf4j.Logger; |
27 | 26 | import org.slf4j.LoggerFactory; |
28 | 27 |
|
| 28 | +import java.io.PrintWriter; |
| 29 | +import java.io.StringWriter; |
29 | 30 | import java.nio.file.Path; |
30 | 31 | import java.nio.file.Paths; |
31 | 32 | import java.util.List; |
32 | 33 | import java.util.Objects; |
33 | 34 | import java.util.Optional; |
34 | | - |
35 | | -import static org.slf4j.Logger.ROOT_LOGGER_NAME; |
| 35 | +import java.util.logging.Formatter; |
| 36 | +import java.util.logging.Handler; |
| 37 | +import java.util.logging.Level; |
| 38 | +import java.util.logging.LogManager; |
| 39 | +import java.util.logging.LogRecord; |
| 40 | +import java.util.logging.StreamHandler; |
36 | 41 |
|
37 | 42 | /** |
38 | 43 | * @author eroshenkoam Artem Eroshenko |
39 | 44 | */ |
40 | 45 | @SuppressWarnings({ |
41 | 46 | "DeclarationOrder", |
| 47 | + "ClassDataAbstractionCoupling" |
42 | 48 | }) |
43 | 49 | public class CommandLine { |
44 | 50 |
|
@@ -81,8 +87,8 @@ public static void main(final String[] args) throws InterruptedException { |
81 | 87 | final String allureHome = System.getenv("APP_HOME"); |
82 | 88 | final CommandLine commandLine; |
83 | 89 | if (Objects.isNull(allureHome)) { |
84 | | - LOGGER.info("APP_HOME is not set, using default configuration"); |
85 | 90 | commandLine = new CommandLine((Path) null); |
| 91 | + LOGGER.info("APP_HOME is not set, using default configuration"); |
86 | 92 | } else { |
87 | 93 | commandLine = new CommandLine(Paths.get(allureHome)); |
88 | 94 | } |
@@ -124,15 +130,13 @@ public Optional<ExitCode> parse(final String... args) { |
124 | 130 | "PMD.SystemPrintln", |
125 | 131 | }) |
126 | 132 | public ExitCode run() { |
127 | | - final ch.qos.logback.classic.Logger rootLogger = (ch.qos.logback.classic.Logger) |
128 | | - LoggerFactory.getLogger(ROOT_LOGGER_NAME); |
129 | | - |
| 133 | + final java.util.logging.Logger rootLogger = initRootLogger(); |
130 | 134 | if (mainCommand.getVerboseOptions().isQuiet()) { |
131 | 135 | rootLogger.setLevel(Level.OFF); |
132 | 136 | } |
133 | 137 |
|
134 | 138 | if (mainCommand.getVerboseOptions().isVerbose()) { |
135 | | - rootLogger.setLevel(Level.DEBUG); |
| 139 | + rootLogger.setLevel(Level.FINE); |
136 | 140 | } |
137 | 141 |
|
138 | 142 | if (mainCommand.isVersion()) { |
@@ -196,4 +200,63 @@ public MainCommand getMainCommand() { |
196 | 200 | private void printUsage(final JCommander commander) { |
197 | 201 | commander.usage(); |
198 | 202 | } |
| 203 | + |
| 204 | + private static java.util.logging.Logger initRootLogger() { |
| 205 | + final java.util.logging.Logger rootLogger = LogManager.getLogManager().getLogger(""); |
| 206 | + for (Handler handler : rootLogger.getHandlers()) { |
| 207 | + rootLogger.removeHandler(handler); |
| 208 | + } |
| 209 | + |
| 210 | + final Handler handler = new StdOutHandler(); |
| 211 | + handler.setLevel(Level.ALL); |
| 212 | + rootLogger.addHandler(handler); |
| 213 | + return rootLogger; |
| 214 | + } |
| 215 | + |
| 216 | + /** |
| 217 | + * Print only a message from LogRecord. |
| 218 | + */ |
| 219 | + private static final class MessageOnlyFormatter extends Formatter { |
| 220 | + |
| 221 | + @Override |
| 222 | + public String format(final LogRecord record) { |
| 223 | + final StringBuilder builder = new StringBuilder(); |
| 224 | + builder.append(formatMessage(record)) |
| 225 | + .append(System.lineSeparator()); |
| 226 | + |
| 227 | + final Throwable thrown = record.getThrown(); |
| 228 | + if (thrown != null) { |
| 229 | + final StringWriter stringWriter = new StringWriter(); |
| 230 | + final PrintWriter printWriter = new PrintWriter(stringWriter); |
| 231 | + thrown.printStackTrace(printWriter); |
| 232 | + printWriter.flush(); |
| 233 | + builder.append(stringWriter); |
| 234 | + } |
| 235 | + |
| 236 | + return builder.toString(); |
| 237 | + } |
| 238 | + } |
| 239 | + |
| 240 | + /** |
| 241 | + * This Handler publishes log records to System.out. |
| 242 | + * By default the MessageOnlyFormatter is used to only print log messages. |
| 243 | + */ |
| 244 | + @SuppressWarnings("PMD.AvoidSynchronizedAtMethodLevel") |
| 245 | + private static final class StdOutHandler extends StreamHandler { |
| 246 | + |
| 247 | + StdOutHandler() { |
| 248 | + super(System.out, new MessageOnlyFormatter()); |
| 249 | + } |
| 250 | + |
| 251 | + @Override |
| 252 | + public synchronized void publish(final LogRecord record) { |
| 253 | + super.publish(record); |
| 254 | + flush(); |
| 255 | + } |
| 256 | + |
| 257 | + @Override |
| 258 | + public synchronized void close() { |
| 259 | + flush(); |
| 260 | + } |
| 261 | + } |
199 | 262 | } |
0 commit comments