Skip to content

Commit

Permalink
Merge pull request #38 from tomtom-international/feature/slf4j
Browse files Browse the repository at this point in the history
feat: in quiet mode dont log sl4j errors during james initialization
  • Loading branch information
piotrwielgolaski-tomtom authored Jan 18, 2021
2 parents 67ed88a + 73d55d8 commit 2fafe7f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,10 @@ private static void setupAgent(Instrumentation inst) {
Logger.setCurrentLogLevel(configuration.getLogLevel());
printBanner(configuration);

Stopwatch stopwatch = Stopwatch.createStarted();
if (configuration.isQuiet())
SystemErrCapture.register();

Stopwatch stopwatch = Stopwatch.createStarted();
PluginManager pluginManager = new PluginManager(configuration.getPluginIncludeDirectories(), configuration.getPluginIncludeFiles());
LOG.trace("pluginManager time=" + stopwatch.elapsed());

Expand Down Expand Up @@ -181,6 +183,8 @@ private static void setupAgent(Instrumentation inst) {

} catch (ConfigurationInitializationException e) {
e.printStackTrace();
} finally {
SystemErrCapture.unregisterAndReplay(line -> !line.startsWith("SLF4J:"));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.tomtom.james.newagent;

import com.google.common.io.CharStreams;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.io.StringReader;
import java.util.function.Predicate;

public class SystemErrCapture {

private static PrintStream oldErrStream;
private static ByteArrayOutputStream outputStream;
private static PrintStream newErrStream;

public static void register() {
SystemErrCapture.oldErrStream = System.err;
outputStream = new ByteArrayOutputStream();
newErrStream = new PrintStream(outputStream);
System.setErr(newErrStream);
}

public static void unregisterAndReplay(Predicate<String> predicate) {
if (newErrStream == null) {
return;
}
newErrStream.close();
System.setErr(oldErrStream);

replayErrorsMatchingPredicate(outputStream, predicate);
newErrStream = null;
outputStream = null;
}


private static void replayErrorsMatchingPredicate(final ByteArrayOutputStream outputStream,
final Predicate<String> predicate) {
try {
CharStreams.readLines(new StringReader(outputStream.toString()))
.stream().filter(predicate::test)
.forEach(System.err::println);
} catch (IOException e) {
// ignore me
}
}
}

0 comments on commit 2fafe7f

Please sign in to comment.