Skip to content

Commit bd7c9e3

Browse files
devplayer0jimschubert
authored andcommitted
[cli] Don't log to STDOUT if debug flags are set (#474)
It makes sense that error messages should be written to STDERR and all others should be written to STDOUT (as shown in #207). However, it would be convenient to parse the debugging output when the relevant flags are set. This change will disable logging to STDOUT and redirect all log messages to STDERR when any of the debug flags are set. (Resolves #473)
1 parent 62dfb74 commit bd7c9e3

File tree

1 file changed

+18
-0
lines changed
  • modules/openapi-generator-cli/src/main/java/org/openapitools/codegen/cmd

1 file changed

+18
-0
lines changed

modules/openapi-generator-cli/src/main/java/org/openapitools/codegen/cmd/Generate.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
package org.openapitools.codegen.cmd;
1919

20+
import ch.qos.logback.classic.LoggerContext;
21+
import ch.qos.logback.core.spi.FilterAttachable;
2022
import io.airlift.airline.Command;
2123
import io.airlift.airline.Option;
2224
import org.openapitools.codegen.ClientOptInput;
@@ -32,6 +34,7 @@
3234

3335
import java.util.ArrayList;
3436
import java.util.List;
37+
import java.util.stream.Stream;
3538

3639
/**
3740
* User: lanwen Date: 24.03.15 Time: 20:22
@@ -199,8 +202,23 @@ public class Generate implements Runnable {
199202
description = "Skips the default behavior of validating an input specification.")
200203
private Boolean skipValidateSpec;
201204

205+
@Option(name = {"--log-to-stderr"},
206+
title = "Log to STDERR",
207+
description = "write all log messages (not just errors) to STDOUT."
208+
+ " Useful for piping the JSON output of debug options (e.g. `-DdebugOperations`) to an external parser directly while testing a generator.")
209+
private Boolean logToStderr;
210+
202211
@Override
203212
public void run() {
213+
if (logToStderr != null) {
214+
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
215+
Stream.of(Logger.ROOT_LOGGER_NAME, "io.swagger", "org.openapitools")
216+
.map(lc::getLogger)
217+
.peek(logger -> logger.detachAppender("STDOUT"))
218+
.reduce((logger, next) -> logger.getName().equals(Logger.ROOT_LOGGER_NAME) ? logger : next)
219+
.map(root -> root.getAppender("STDERR"))
220+
.ifPresent(FilterAttachable::clearAllFilters);
221+
}
204222

205223
// attempt to read from config file
206224
CodegenConfigurator configurator = CodegenConfigurator.fromFile(configFile);

0 commit comments

Comments
 (0)