Skip to content

Commit

Permalink
[GR-11702] Logger should log to STDERR, not to STDOUT.
Browse files Browse the repository at this point in the history
(cherry picked from commit 7991539)
  • Loading branch information
tzezula authored and ansalond committed Sep 28, 2018
1 parent 2dfbdb7 commit 94446ca
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -973,8 +973,7 @@ public Builder fileSystem(final FileSystem fileSystem) {
* {@code JavaScriptLanguage} class.<br>
* <p>
* If the {@code logHandler} is not set on {@link Engine} nor on {@link Context} the log
* messages are printed to {@link #out(java.io.OutputStream) Context's standard output
* stream}.
* messages are printed to {@link #err(java.io.OutputStream) Context's error output stream}.
*
* @param logHandler the {@link Handler} to use for logging in built {@link Context}.
* @return the {@link Builder}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -875,18 +875,18 @@ public void testTemporaryEngine() throws Exception {
public void testLogging() throws Exception {
setPatchable(FIRST);
// In context pre-initialization there is no sdk Context to set log handler,
// logging is done to System.out
final PrintStream origOut = System.out;
final ByteArrayOutputStream preInitOut = new ByteArrayOutputStream();
try (PrintStream printStream = new PrintStream(preInitOut)) {
System.setOut(printStream);
// logging is done to System.err
final PrintStream origErr = System.err;
final ByteArrayOutputStream preInitErr = new ByteArrayOutputStream();
try (PrintStream printStream = new PrintStream(preInitErr)) {
System.setErr(printStream);
System.setProperty("polyglot.log.engine.level", "FINE");
doContextPreinitialize(FIRST);
} finally {
System.setOut(origOut);
System.setErr(origErr);
System.getProperties().remove("polyglot.log.engine.level");
}
final String preInitLog = preInitOut.toString("UTF-8");
final String preInitLog = preInitErr.toString("UTF-8");
assertTrue(preInitLog.contains("Pre-initialized context for language: ContextPreInitializationFirst"));
List<CountingContext> contexts = new ArrayList<>(emittedContexts);
assertEquals(1, contexts.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -441,12 +441,12 @@ public boolean test(final LoggingContext context, final Collection<TruffleLogger
return false;
}
};
final ByteArrayOutputStream out = new ByteArrayOutputStream();
try (Context ctx = Context.newBuilder().out(out).build()) {
final ByteArrayOutputStream err = new ByteArrayOutputStream();
try (Context ctx = Context.newBuilder().err(err).build()) {
ctx.eval(LoggingLanguageFirst.ID, "");
}
out.close();
final String output = new String(out.toByteArray());
err.close();
final String output = new String(err.toByteArray());
final Pattern p = Pattern.compile("\\[(.*)\\]\\sWARNING:\\s(.*)");
for (String line : output.split("\n")) {
final Matcher m = p.matcher(line);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1015,7 +1015,7 @@ public synchronized Context createContext(OutputStream configOut, OutputStream c
}

Handler useHandler = logHandler != null ? logHandler : this.logHandler;
useHandler = useHandler != null ? useHandler : PolyglotLogHandler.createStreamHandler(useOut, false, true);
useHandler = useHandler != null ? useHandler : PolyglotLogHandler.createStreamHandler(useErr, false, true);

final InputStream useIn = configIn == null ? this.in : configIn;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public Engine buildEngine(OutputStream out, OutputStream err, InputStream in, Ma
*/
@Override
public void preInitializeEngine() {
final Handler logHandler = PolyglotLogHandler.createStreamHandler(System.out, false, true);
final Handler logHandler = PolyglotLogHandler.createStreamHandler(System.err, false, true);
try {
final PolyglotEngineImpl preInitializedEngine = PolyglotEngineImpl.preInitialize(
this,
Expand Down

0 comments on commit 94446ca

Please sign in to comment.