Skip to content

Commit

Permalink
Merge pull request #4630 from eclipse/jetty-9.4.x-4620-console-capture
Browse files Browse the repository at this point in the history
Issue #4620 - Better support for alt PrintStream in StdErrLog
  • Loading branch information
joakime authored Mar 3, 2020
2 parents 3a8b45d + f2a4c6b commit c08ca2a
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions jetty-util/src/main/java/org/eclipse/jetty/util/log/StdErrLog.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ public static void setTagPad(int pad)
private int _level;
// Level that this Logger was configured as (remembered in special case of .setDebugEnabled())
private int _configuredLevel;
private PrintStream _stderr = System.err;
// The alternate stream to print to (if set)
private PrintStream _altStream;
private boolean _source;
// Print the long form names, otherwise use abbreviated
private boolean _printLongNames = __long;
Expand Down Expand Up @@ -389,16 +390,14 @@ public void setLevel(int level)
this._level = level;
}

/**
* The alternate stream to use for STDERR.
*
* @param stream the stream of choice, or {@code null} to use {@link System#err}
*/
public void setStdErrStream(PrintStream stream)
{
if (stream == null)
{
this._stderr = System.err;
}
else
{
this._stderr = stream;
}
this._altStream = stream;
}

@Override
Expand Down Expand Up @@ -442,7 +441,14 @@ public void debug(String msg, Throwable thrown)

private void println(StringBuilder builder)
{
_stderr.println(builder);
if (_altStream != null)
_altStream.println(builder);
else
{
// We always use the PrintStream stored in System.err,
// just in case someone has replaced it with a call to System.setErr(PrintStream)
System.err.println(builder);
}
}

private void format(StringBuilder builder, String level, String msg, Object... inArgs)
Expand Down Expand Up @@ -648,7 +654,7 @@ protected Logger newLogger(String fullname)
StdErrLog logger = new StdErrLog(fullname);
// Preserve configuration for new loggers configuration
logger.setPrintLongNames(_printLongNames);
logger._stderr = this._stderr;
logger._altStream = this._altStream;

// Force the child to have any programmatic configuration
if (_level != _configuredLevel)
Expand Down

0 comments on commit c08ca2a

Please sign in to comment.