本文整理了Java中org.apache.logging.log4j.message.Message.getFormattedMessage()
方法的一些代码示例,展示了Message.getFormattedMessage()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Message.getFormattedMessage()
方法的具体详情如下:
包路径:org.apache.logging.log4j.message.Message
类名称:Message
方法名:getFormattedMessage
[英]Gets the Message formatted as a String. Each Message implementation determines the appropriate way to format the data encapsulated in the Message. Messages that provide more than one way of formatting the Message will implement MultiformatMessage.
When configured to log asynchronously, this method is called before the Message is queued, unless this message implements ReusableMessage or is annotated with AsynchronouslyFormattable. This gives the Message implementation class a chance to create a formatted message String with the current value of any mutable objects. The intention is that the Message implementation caches this formatted message and returns it on subsequent calls. (See LOG4J2-763.)
When logging synchronously, this method will not be called for Messages that implement the StringBuilderFormattable interface: instead, the StringBuilderFormattable#formatTo(StringBuilder) method will be called so the Message can format its contents without creating intermediate String objects.
[中]获取格式化为字符串的消息。每个消息实现都决定了格式化消息中封装的数据的适当方式。提供多种格式化方式的消息将实现多格式消息。
配置为异步日志时,在消息排队之前调用此方法,除非此消息实现ReusableMessage或使用AsynchronouslyFormattable注释。这使消息实现类有机会使用任何可变对象的当前值创建格式化的消息字符串。其目的是消息实现缓存这个格式化的消息,并在后续调用中返回它。(见LOG4J2-763。)
同步记录时,对于实现StringBuilderPerformAttable接口的消息,将不会调用此方法:相反,将调用StringBuilderPerformAttable#formatTo(StringBuilder)方法,以便消息可以格式化其内容,而无需创建中间字符串对象。
代码示例来源:origin: org.apache.logging.log4j/log4j-api
@Override
public String getFormattedMessage() {
if (message != null) {
return text + " " + message.getFormattedMessage();
}
return text;
}
代码示例来源:origin: org.apache.logging.log4j/log4j-api
private Message makeImmutable(final Message message) {
if (!(message instanceof ReusableMessage)) {
return message;
}
return new SimpleMessage(message.getFormattedMessage());
}
代码示例来源:origin: org.apache.logging.log4j/log4j-api
protected EntryMessage entryMsg(final String format, final Supplier<?>... paramSuppliers) {
final int count = paramSuppliers == null ? 0 : paramSuppliers.length;
final Object[] params = new Object[count];
for (int i = 0; i < count; i++) {
params[i] = paramSuppliers[i].get();
if (params[i] instanceof Message) {
params[i] = ((Message) params[i]).getFormattedMessage();
}
}
return entryMsg(format, params);
}
代码示例来源:origin: org.apache.logging.log4j/log4j-core
static Stack transform(final List<LogEvent> logEvents) {
final List<String> filtered = new ArrayList<>(logEvents.size());
for (LogEvent event : logEvents) {
filtered.add(event.getMessage().getFormattedMessage());
}
Collections.reverse(filtered);
Stack<String> result = new Stack<>();
result.addAll(filtered);
return result;
}
代码示例来源:origin: org.apache.logging.log4j/log4j-core
@Test
public void testEmptyAttribute() throws Exception {
final Logger logger = LogManager.getLogger();
logger.info("Test");
final StatusData data = StatusLogger.getLogger().getStatusData().get(0);
//System.out.println(data.getFormattedStatus());
assertEquals(Level.ERROR, data.getLevel());
assertTrue(data.getMessage().getFormattedMessage().contains("multiple root loggers"));
}
}
代码示例来源:origin: org.apache.logging.log4j/log4j-api
@Test
public void testNewMessage() {
final LocalizedMessageFactory localizedMessageFactory = new LocalizedMessageFactory(
ResourceBundle.getBundle("MF", Locale.US));
final Message message = localizedMessageFactory.newMessage("hello_world");
Assert.assertEquals("Hello world.", message.getFormattedMessage());
}
}
代码示例来源:origin: org.apache.logging.log4j/log4j-core
@Test
public void getLogger_String_MessageFactoryMismatch() {
final Logger testLogger = testMessageFactoryMismatch(testName.getMethodName(),
StringFormatterMessageFactory.INSTANCE, ParameterizedMessageFactory.INSTANCE);
testLogger.debug("%,d", Integer.MAX_VALUE);
final List<LogEvent> events = app.getEvents();
assertEventCount(events, 1);
assertEquals(String.format("%,d", Integer.MAX_VALUE), events.get(0).getMessage().getFormattedMessage());
}
代码示例来源:origin: org.apache.logging.log4j/log4j-core
@Test
public void getLogger_String_MessageFactoryMismatchNull() {
final Logger testLogger = testMessageFactoryMismatch(testName.getMethodName(), StringFormatterMessageFactory.INSTANCE, null);
testLogger.debug("%,d", Integer.MAX_VALUE);
final List<LogEvent> events = app.getEvents();
assertEventCount(events, 1);
assertEquals(String.format("%,d", Integer.MAX_VALUE), events.get(0).getMessage().getFormattedMessage());
}
代码示例来源:origin: org.apache.logging.log4j/log4j-api
@Test
public void testErrorStringParamSupplier() {
logger2.disable().error("abc {}", supplierArray1);
assertTrue(logger2.list.isEmpty());
assertFalse(supplier.invoked);
logger2.enable().error("abc {}", supplierArray1);
assertEquals(1, logger2.list.size());
assertTrue(supplier.invoked);
final LogEvent event = logger2.list.get(0);
assertEquals(Level.ERROR, event.level);
assertEquals("abc Hi", event.message.getFormattedMessage());
}
代码示例来源:origin: org.apache.logging.log4j/log4j-api
@Test
public void testTraceSupplier() {
logger2.disable().trace(supplier);
assertTrue(logger2.list.isEmpty());
assertFalse(supplier.invoked);
logger2.enable().trace(supplier);
assertEquals(1, logger2.list.size());
assertTrue(supplier.invoked);
final LogEvent event = logger2.list.get(0);
assertEquals(Level.TRACE, event.level);
assertSame(stringMessage, event.message.getFormattedMessage());
}
代码示例来源:origin: org.apache.logging.log4j/log4j-api
@Test
public void testWarnSupplier() {
logger2.disable().warn(supplier);
assertTrue(logger2.list.isEmpty());
assertFalse(supplier.invoked);
logger2.enable().warn(supplier);
assertEquals(1, logger2.list.size());
assertTrue(supplier.invoked);
final LogEvent event = logger2.list.get(0);
assertEquals(Level.WARN, event.level);
assertSame(stringMessage, event.message.getFormattedMessage());
}
代码示例来源:origin: org.apache.logging.log4j/log4j-api
@Test
public void testLogSupplier() {
logger2.disable().log(Level.WARN, supplier);
assertTrue(logger2.list.isEmpty());
assertFalse(supplier.invoked);
logger2.enable().log(Level.WARN, supplier);
assertEquals(1, logger2.list.size());
assertTrue(supplier.invoked);
final LogEvent event = logger2.list.get(0);
assertEquals(Level.WARN, event.level);
assertSame(stringMessage, event.message.getFormattedMessage());
}
代码示例来源:origin: org.apache.logging.log4j/log4j-api
@Test
public void testInfoStringParamSupplier() {
logger2.disable().info("abc {}", supplierArray1);
assertTrue(logger2.list.isEmpty());
assertFalse(supplier.invoked);
logger2.enable().info("abc {}", supplierArray1);
assertEquals(1, logger2.list.size());
assertTrue(supplier.invoked);
final LogEvent event = logger2.list.get(0);
assertEquals(Level.INFO, event.level);
assertEquals("abc Hi", event.message.getFormattedMessage());
}
代码示例来源:origin: org.apache.logging.log4j/log4j-api
@Test
public void testTraceStringParamSupplier() {
logger2.disable().trace("abc {}", supplierArray1);
assertTrue(logger2.list.isEmpty());
assertFalse(supplier.invoked);
logger2.enable().trace("abc {}", supplierArray1);
assertEquals(1, logger2.list.size());
assertTrue(supplier.invoked);
final LogEvent event = logger2.list.get(0);
assertEquals(Level.TRACE, event.level);
assertEquals("abc Hi", event.message.getFormattedMessage());
}
代码示例来源:origin: org.apache.logging.log4j/log4j-api
@Test
public void testDebugMarkerSupplier() {
logger2.disable().debug(marker, supplier);
assertTrue(logger2.list.isEmpty());
assertFalse(supplier.invoked);
logger2.enable().debug(marker, supplier);
assertEquals(1, logger2.list.size());
assertTrue(supplier.invoked);
final LogEvent event = logger2.list.get(0);
assertEquals(Level.DEBUG, event.level);
assertSame(stringMessage, event.message.getFormattedMessage());
assertSame(marker, event.marker);
}
代码示例来源:origin: org.apache.logging.log4j/log4j-api
@Test
public void testTraceMarkerSupplier() {
logger2.disable().trace(marker, supplier);
assertTrue(logger2.list.isEmpty());
assertFalse(supplier.invoked);
logger2.enable().trace(marker, supplier);
assertEquals(1, logger2.list.size());
assertTrue(supplier.invoked);
final LogEvent event = logger2.list.get(0);
assertEquals(Level.TRACE, event.level);
assertSame(stringMessage, event.message.getFormattedMessage());
assertSame(marker, event.marker);
}
代码示例来源:origin: org.apache.logging.log4j/log4j-api
@Test
public void testTraceMarkerStringParamSupplier() {
logger2.disable().trace(marker, "abc {}", supplierArray1);
assertTrue(logger2.list.isEmpty());
assertFalse(supplier.invoked);
logger2.enable().trace(marker, "abc {}", supplierArray1);
assertEquals(1, logger2.list.size());
assertTrue(supplier.invoked);
final LogEvent event = logger2.list.get(0);
assertEquals(Level.TRACE, event.level);
assertSame(marker, event.marker);
assertEquals("abc Hi", event.message.getFormattedMessage());
}
代码示例来源:origin: org.apache.logging.log4j/log4j-api
@Test
public void testLogMarkerSupplier() {
logger2.disable().log(Level.WARN, marker, supplier);
assertTrue(logger2.list.isEmpty());
assertFalse(supplier.invoked);
logger2.enable().log(Level.WARN, marker, supplier);
assertEquals(1, logger2.list.size());
assertTrue(supplier.invoked);
final LogEvent event = logger2.list.get(0);
assertEquals(Level.WARN, event.level);
assertSame(stringMessage, event.message.getFormattedMessage());
assertSame(marker, event.marker);
}
代码示例来源:origin: org.apache.logging.log4j/log4j-api
@Test
public void testLogMarkerStringParamSupplier() {
logger2.disable().log(Level.WARN, marker, "abc {}", supplierArray1);
assertTrue(logger2.list.isEmpty());
assertFalse(supplier.invoked);
logger2.enable().log(Level.WARN, marker, "abc {}", supplierArray1);
assertEquals(1, logger2.list.size());
assertTrue(supplier.invoked);
final LogEvent event = logger2.list.get(0);
assertEquals(Level.WARN, event.level);
assertSame(marker, event.marker);
assertEquals("abc Hi", event.message.getFormattedMessage());
}
代码示例来源:origin: org.apache.logging.log4j/log4j-core
@Test
public void testAppendWithSerializedLayout() throws Exception {
final Appender appender = ctx.getRequiredAppender("KafkaAppenderWithSerializedLayout");
final LogEvent logEvent = createLogEvent();
appender.append(logEvent);
final List<ProducerRecord<byte[], byte[]>> history = kafka.history();
assertEquals(1, history.size());
final ProducerRecord<byte[], byte[]> item = history.get(0);
assertNotNull(item);
assertEquals(TOPIC_NAME, item.topic());
assertNull(item.key());
assertEquals(LOG_MESSAGE, deserializeLogEvent(item.value()).getMessage().getFormattedMessage());
}
内容来源于网络,如有侵权,请联系作者删除!