Skip to content

Commit

Permalink
[cli] Don't log to STDOUT if debug flags are set (#474)
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
devplayer0 authored and jimschubert committed Aug 20, 2018
1 parent 62dfb74 commit bd7c9e3
Showing 1 changed file with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -32,6 +34,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

/**
* User: lanwen Date: 24.03.15 Time: 20:22
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit bd7c9e3

Please sign in to comment.