[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)
This commit is contained in:
Jack O'Sullivan 2018-08-20 02:33:49 +01:00 committed by Jim Schubert
parent 62dfb749f1
commit bd7c9e30d3

View File

@ -17,6 +17,8 @@
package org.openapitools.codegen.cmd;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.core.spi.FilterAttachable;
import io.airlift.airline.Command;
import io.airlift.airline.Option;
import org.openapitools.codegen.ClientOptInput;
@ -32,6 +34,7 @@ import static org.apache.commons.lang3.StringUtils.isNotEmpty;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
/**
* User: lanwen Date: 24.03.15 Time: 20:22
@ -199,8 +202,23 @@ public class Generate implements Runnable {
description = "Skips the default behavior of validating an input specification.")
private Boolean skipValidateSpec;
@Option(name = {"--log-to-stderr"},
title = "Log to STDERR",
description = "write all log messages (not just errors) to STDOUT."
+ " Useful for piping the JSON output of debug options (e.g. `-DdebugOperations`) to an external parser directly while testing a generator.")
private Boolean logToStderr;
@Override
public void run() {
if (logToStderr != null) {
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
Stream.of(Logger.ROOT_LOGGER_NAME, "io.swagger", "org.openapitools")
.map(lc::getLogger)
.peek(logger -> logger.detachAppender("STDOUT"))
.reduce((logger, next) -> logger.getName().equals(Logger.ROOT_LOGGER_NAME) ? logger : next)
.map(root -> root.getAppender("STDERR"))
.ifPresent(FilterAttachable::clearAllFilters);
}
// attempt to read from config file
CodegenConfigurator configurator = CodegenConfigurator.fromFile(configFile);