Java 类org.apache.logging.log4j.core.appender.WriterAppender 实例源码

项目:herd    文件:Log4j2LoggingHelper.java   
@Override
public StringWriter addLoggingWriterAppender(String appenderName)
{
    // Get the configuration
    final LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false);
    final Configuration configuration = loggerContext.getConfiguration();

    // Create a string writer as part of the appender so logging will be written to it.
    StringWriter stringWriter = new StringWriter();

    // Create and start the appender with the string writer.
    Appender appender = WriterAppender.createAppender(null, null, stringWriter, appenderName, false, true);
    appender.start();

    // Add the appender to the root logger.
    configuration.getRootLogger().addAppender(appender, null, null);

    // Return the string writer.
    return stringWriter;
}
项目:TweetwallFX    文件:CLogOut.java   
private CLogOut() {
    final ByteArrayOutputStream stdStream = new ByteArrayOutputStream() {
        @Override
        public synchronized void flush() throws IOException {
            String theString = toString("UTF-8");

            /* OK:
             Establishing connection.
             Twitter Stream consumer-1[Establishing connection]
             Connection established.
             Receiving status stream.
             Twitter Stream consumer-1[Receiving stream]
             *Received:{...}
             Twitter Stream consumer-1[Disposing thread]

             */

            /* WRONG:
             Establishing connection.
             Twitter Stream consumer-1[Establishing connection]
             Exceeded connection limit for user
             420
             Waiting for 10000 milliseconds
             Twitter Stream consumer-1[Disposing thread]

             */
            Platform.runLater(() -> {
                if (theString.startsWith("Establishing connection")) {
                    message.set("Establishing connection...\n Please, wait a few seconds");
                } else if (theString.startsWith("Receiving status stream")) {
                    message.set("Receiving tweets!! \n Press stop button to stop the search");
                } else if (theString.startsWith("Exceeded connection limit")) {
                    message.set("Exceeded connection limit...");
                } else if (theString.startsWith("Waiting for ")) {
                    message.set(theString + " or press stop button to stop the search");
                } else if (theString.contains("Disposing thread")) {
                    message.set("The search has finished");
                }
            });
            System.out.print("***** " + theString);
            reset();
        }
    };


    LoggerContext context = LoggerContext.getContext(false);
    Configuration config = context.getConfiguration();
    PatternLayout layout = PatternLayout.newBuilder().withConfiguration(config).withPattern("%m%n").build();
    Appender appender = WriterAppender.newBuilder().setLayout(layout).setTarget(new OutputStreamWriter(stdStream, StandardCharsets.UTF_8)).build();
    appender.start();
    config.addAppender(appender);        

    updateLoggers(appender, config);        
}