本文整理了Java中org.apache.logging.log4j.core.Logger.addAppender()
方法的一些代码示例,展示了Logger.addAppender()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Logger.addAppender()
方法的具体详情如下:
包路径:org.apache.logging.log4j.core.Logger
类名称:Logger
方法名:addAppender
[英]This method is not exposed through the public API and is used primarily for unit testing.
[中]此方法不通过公共API公开,主要用于单元测试。
代码示例来源:origin: alibaba/nacos
protected void activateAppender(org.apache.logging.log4j.core.Logger logger) {
removeAllAppenders(this.logger);
Map<String, Appender> appenders = null;
if ((appenders = logger.getAppenders()) != null) {
for (Appender appender : appenders.values()) {
this.logger.addAppender(appender);
}
}
}
代码示例来源:origin: org.apache.logging.log4j/log4j-core
protected void initRootLogger(final Appender appender) {
root.addAppender(appender);
root.setAdditive(false);
root.setLevel(Level.DEBUG);
}
代码示例来源:origin: alibaba/nacos
@Override
public void activateConsoleAppender(String target, String encoding) {
org.apache.logging.log4j.core.Layout layout = org.apache.logging.log4j.core.layout.PatternLayout.newBuilder().
withConfiguration(configuration)
.withPattern(LoggerHelper.getPattern())
.withCharset(Charset.forName(encoding))
.build();
org.apache.logging.log4j.core.appender.ConsoleAppender appender = ConsoleAppender.createAppender(layout, null,
ConsoleAppender.Target.valueOf(target.toUpperCase().replace(".", "_")), "LoggerApiConsoleAppender", false,
false, true);
appender.start();
removeAllAppenders(logger);
logger.addAppender(appender);
setProductName(productName);
}
代码示例来源:origin: alibaba/nacos
@Override
public void activateAsync(List<Object[]> args) {
Map<String, Appender> appenders = logger.getAppenders();
if (appenders == null) {
throw new IllegalStateException("Activate async appender failed, no appender exist.");
}
AppenderRef[] refs = new AppenderRef[appenders.size()];
int i = 0;
for (Appender appender : appenders.values()) {
configuration.addAppender(appender);
refs[i++] = AppenderRef.createAppenderRef(appender.getName(), null, null);
}
AsyncAppender.Builder builder = AsyncAppender.newBuilder()
.setName(productName + "." + logger.getName() + ".AsyncAppender")
.setConfiguration(configuration)
.setAppenderRefs(refs)
.setBlockingQueueFactory(ArrayBlockingQueueFactory.<LogEvent>createFactory());
invokeMethod(builder, args);
AsyncAppender asyncAppender = builder.build();
asyncAppender.start();
removeAllAppenders(logger);
logger.addAppender(asyncAppender);
setProductName(productName);
}
代码示例来源:origin: apache/incubator-druid
@BeforeClass
public static void setUpStatic()
{
appender = OutputStreamAppender
.newBuilder()
.setName("test stream")
.setTarget(baos)
.setLayout(JsonLayout.createLayout(false, true, false, true, true, StandardCharsets.UTF_8))
.build();
final Logger logger = (Logger)
LogManager.getLogger(LoggingRequestLogger.class);
appender.start();
logger.addAppender(appender);
}
代码示例来源:origin: alibaba/nacos
@Override
public void activateAppenderWithSizeRolling(String productName, String file, String encoding, String size,
int maxBackupIndex) {
org.apache.logging.log4j.core.appender.RollingFileAppender appender = RollingFileAppender.newBuilder()
.withName(productName + "." + file.replace(File.separatorChar, '.') + ".Appender")
.withFileName(LoggerHelper.getLogFileP(productName, file))
.withAppend(true)
.withBufferedIo(true)
.setConfiguration(configuration)
.withFilePattern(LoggerHelper.getLogFile(productName, file) + ".%i")
.withLayout(buildLayout(encoding))
.withCreateOnDemand(false)
.withPolicy(SizeBasedTriggeringPolicy.createPolicy(size))
.withStrategy(DefaultRolloverStrategy
.createStrategy(String.valueOf(maxBackupIndex), "1", "max", null, null, false, configuration))
.build();
appender.start();
removeAllAppenders(logger);
logger.addAppender(appender);
setProductName(productName);
}
代码示例来源:origin: org.apache.logging.log4j/log4j-core
root.addAppender(appender);
root.setLevel(Level.DEBUG);
代码示例来源:origin: alibaba/nacos
@Override
public void activateAppenderWithTimeAndSizeRolling(String productName, String file, String encoding, String size,
String datePattern, int maxBackupIndex) {
org.apache.logging.log4j.core.appender.RollingFileAppender appender = RollingFileAppender.newBuilder()
.withName(productName + "." + file.replace(File.separatorChar, '.') + ".Appender")
.withFileName(LoggerHelper.getLogFileP(productName, file))
.withAppend(true)
.withBufferedIo(true)
.setConfiguration(configuration)
.withFilePattern(LoggerHelper.getLogFile(productName, file) + ".%d{" + datePattern + "}.%i")
.withLayout(buildLayout(encoding))
.withCreateOnDemand(false)
.withPolicy(CompositeTriggeringPolicy.createPolicy(TimeBasedTriggeringPolicy.createPolicy("1", "true"),
SizeBasedTriggeringPolicy.createPolicy(size)))
.withStrategy(DefaultRolloverStrategy
.createStrategy(String.valueOf(maxBackupIndex), "1", "max", null, null, false, configuration))
.build();
appender.start();
removeAllAppenders(logger);
logger.addAppender(appender);
setProductName(productName);
}
代码示例来源:origin: org.apache.logging.log4j/log4j-core
@Test
public void testSubstituteStructuredData() {
final String mdcId = "RequestContext";
final String expectedToContain = String.format("ATM %s MSG-ID - Message", PROCESSID);
for (final Appender appender : root.getAppenders().values()) {
root.removeAppender(appender);
}
final AbstractStringLayout layout = Rfc5424Layout.createLayout(Facility.LOCAL0, "Event", 3692, false, mdcId,
null, null, true, null, "ATM", "MSG-ID", "key1, key2, locale", null, null, null, false, null, null);
final ListAppender appender = new ListAppender("List", null, layout, true, false);
appender.start();
root.addAppender(appender);
root.setLevel(Level.DEBUG);
root.info("Message");
try {
final List<String> list = appender.getMessages();
assertTrue("Not enough list entries", list.size() > 0);
final String message = list.get(0);
Assert.assertTrue("Not the expected message received", message.contains(expectedToContain));
appender.clear();
} finally {
root.removeAppender(appender);
appender.stop();
}
}
代码示例来源:origin: org.apache.logging.log4j/log4j-core
@Test
public void testParameterizedMessage() {
for (final Appender appender : root.getAppenders().values()) {
root.removeAppender(appender);
}
// set up appender
final AbstractStringLayout layout = Rfc5424Layout.createLayout(Facility.LOCAL0, "Event", 3692, true, "RequestContext",
null, null, true, null, "ATM", null, "key1, key2, locale", null, null, null, true, null, null);
final ListAppender appender = new ListAppender("List", null, layout, true, false);
appender.start();
// set appender on root and set level to debug
root.addAppender(appender);
root.setLevel(Level.DEBUG);
root.info("Hello {}", "World");
try {
final List<String> list = appender.getMessages();
assertTrue("Not enough list entries", list.size() > 0);
final String message = list.get(0);
assertTrue("Incorrect message. Expected - Hello World, Actual - " + message, message.contains("Hello World"));
} finally {
root.removeAppender(appender);
appender.stop();
}
}
}
代码示例来源:origin: org.apache.logging.log4j/log4j-core
/**
* Test case for MDC exception conversion pattern.
*/
@Test
public void testException() throws Exception {
for (final Appender appender : root.getAppenders().values()) {
root.removeAppender(appender);
}
// set up layout/appender
final AbstractStringLayout layout = Rfc5424Layout.createLayout(Facility.LOCAL0, "Event", 3692, true, "RequestContext",
null, null, true, null, "ATM", null, "key1, key2, locale", null, "loginId", "%xEx", true, null, null);
final ListAppender appender = new ListAppender("List", null, layout, true, false);
appender.start();
// set appender on root and set level to debug
root.addAppender(appender);
root.setLevel(Level.DEBUG);
ThreadContext.put("loginId", "JohnDoe");
// output starting message
root.debug("starting mdc pattern test", new IllegalArgumentException("Test"));
try {
final List<String> list = appender.getMessages();
assertTrue("Not enough list entries", list.size() > 1);
final String string = list.get(1);
assertTrue("No Exception in " + string, string.contains("IllegalArgumentException"));
appender.clear();
} finally {
root.removeAppender(appender);
appender.stop();
}
}
代码示例来源:origin: alibaba/nacos
@Override
public void activateAppender(String productName, String file, String encoding) {
org.apache.logging.log4j.core.appender.RollingFileAppender appender = RollingFileAppender.newBuilder()
.withName(productName + "." + file.replace(File.separatorChar, '.') + ".Appender")
.withFileName(LoggerHelper.getLogFileP(productName, file))
.withAppend(true)
.withBufferedIo(true)
.setConfiguration(configuration)
.withFilePattern(LoggerHelper.getLogFile(productName, file) + ".%d{yyyy-MM-dd}")
.withLayout(buildLayout(encoding))
.withCreateOnDemand(false)
.withPolicy(TimeBasedTriggeringPolicy.createPolicy("1", "true"))
.withStrategy(DefaultRolloverStrategy.createStrategy(null, null, "nomax", null, null, false, configuration))
.build();
appender.start();
removeAllAppenders(logger);
logger.addAppender(appender);
setProductName(productName);
}
代码示例来源:origin: alibaba/nacos
@Override
public void activateAppenderWithTimeAndSizeRolling(String productName, String file, String encoding, String size,
String datePattern) {
org.apache.logging.log4j.core.appender.RollingFileAppender appender = RollingFileAppender.newBuilder()
.withName(productName + "." + file.replace(File.separatorChar, '.') + ".Appender")
.withFileName(LoggerHelper.getLogFileP(productName, file))
.withAppend(true)
.withBufferedIo(true)
.setConfiguration(configuration)
.withFilePattern(LoggerHelper.getLogFile(productName, file) + ".%d{" + datePattern + "}")
.withLayout(buildLayout(encoding))
.withCreateOnDemand(false)
.withPolicy(CompositeTriggeringPolicy.createPolicy(TimeBasedTriggeringPolicy.createPolicy("1", "true"),
SizeBasedTriggeringPolicy.createPolicy(size)))
.withStrategy(DefaultRolloverStrategy.createStrategy(null, null, "nomax", null, null, false, configuration))
.build();
appender.start();
removeAllAppenders(logger);
logger.addAppender(appender);
setProductName(productName);
}
代码示例来源:origin: org.apache.logging.log4j/log4j-core
this.rootLogger.addAppender(appender);
this.rootLogger.setLevel(Level.DEBUG);
this.checkContains("loggerFqcn: \"" + AbstractLogger.class.getName() + "\"", list);
for (final Appender app : appenders.values()) {
this.rootLogger.addAppender(app);
代码示例来源:origin: org.apache.logging.log4j/log4j-core
appender.start();
root.addAppender(appender);
root.setLevel(Level.DEBUG);
代码示例来源:origin: org.apache.logging.log4j/log4j-core
this.rootLogger.addAppender(appender);
this.rootLogger.setLevel(Level.DEBUG);
this.checkContains("message: \"starting mdc pattern test\"", list);
for (final Appender app : appenders.values()) {
this.rootLogger.addAppender(app);
代码示例来源:origin: org.apache.logging.log4j/log4j-core
root.addAppender(appender);
root.setLevel(Level.DEBUG);
代码示例来源:origin: org.apache.logging.log4j/log4j-core
@Test
public void testUdpAppender() throws Exception {
try {
udpServer.latch.await();
} catch (final InterruptedException ex) {
ex.printStackTrace();
}
// @formatter:off
final SocketAppender appender = SocketAppender.newBuilder()
.withProtocol(Protocol.UDP)
.withPort(tcpServer.getLocalPort())
.withReconnectDelayMillis(-1)
.withName("test")
.withImmediateFail(false)
.withLayout(JsonLayout.newBuilder().setProperties(true).build())
.build();
// @formatter:on
appender.start();
// set appender on root and set level to debug
logger.addAppender(appender);
logger.setAdditive(false);
logger.setLevel(Level.DEBUG);
logger.debug("This is a udp message");
final LogEvent event = udpServer.getQueue().poll(3, TimeUnit.SECONDS);
assertNotNull("No event retrieved", event);
assertTrue("Incorrect event", event.getMessage().getFormattedMessage().equals("This is a udp message"));
assertTrue("Message not delivered via UDP", udpServer.getCount() > 0);
}
代码示例来源:origin: org.apache.logging.log4j/log4j-core
@Test
public void testTcpAppenderDeadlock() throws Exception {
// @formatter:off
final SocketAppender appender = SocketAppender.newBuilder()
.withHost("localhost")
.withPort(DYN_PORT)
.withReconnectDelayMillis(100)
.withName("test")
.withImmediateFail(false)
.withLayout(JsonLayout.newBuilder().setProperties(true).build())
.build();
// @formatter:on
appender.start();
// set appender on root and set level to debug
logger.addAppender(appender);
logger.setAdditive(false);
logger.setLevel(Level.DEBUG);
final TcpSocketTestServer tcpSocketServer = new TcpSocketTestServer(DYN_PORT);
try {
tcpSocketServer.start();
logger.debug("This message is written because a deadlock never.");
final LogEvent event = tcpSocketServer.getQueue().poll(3, TimeUnit.SECONDS);
assertNotNull("No event retrieved", event);
} finally {
tcpSocketServer.shutdown();
}
}
代码示例来源:origin: org.apache.logging.log4j/log4j-core
@Test
public void testTcpAppenderNoWait() throws Exception {
// @formatter:off
final SocketAppender appender = SocketAppender.newBuilder()
.withHost("localhost")
.withPort(ERROR_PORT)
.withReconnectDelayMillis(100)
.withName("test")
.withImmediateFail(false)
.withIgnoreExceptions(false)
.withLayout(JsonLayout.newBuilder().setProperties(true).build())
.build();
// @formatter:on
appender.start();
// set appender on root and set level to debug
logger.addAppender(appender);
logger.setAdditive(false);
logger.setLevel(Level.DEBUG);
try {
logger.debug("This message is written because a deadlock never.");
fail("No Exception was thrown");
} catch (final Exception ex) {
// TODO: move exception to @Test(expect = Exception.class)
// Failure is expected.
// ex.printStackTrace();
}
}
内容来源于网络,如有侵权,请联系作者删除!