本文整理匯總了Java中org.apache.logging.log4j.core.appender.WriterAppender類的典型用法代碼示例。如果您正苦於以下問題:Java WriterAppender類的具體用法?Java WriterAppender怎麽用?Java WriterAppender使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
WriterAppender類屬於org.apache.logging.log4j.core.appender包,在下文中一共展示了WriterAppender類的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: addLoggingWriterAppender
import org.apache.logging.log4j.core.appender.WriterAppender; //導入依賴的package包/類
@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;
}
示例2: CLogOut
import org.apache.logging.log4j.core.appender.WriterAppender; //導入依賴的package包/類
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);
}