use of org.apache.logging.log4j.message.Message in project logging-log4j2 by apache.
the class AbstractLogger method printf.
@Override
public void printf(final Level level, final String format, final Object... params) {
if (isEnabled(level, null, format, params)) {
final Message msg = new StringFormattedMessage(format, params);
logMessageSafely(FQCN, level, null, msg, msg.getThrowable());
}
}
use of org.apache.logging.log4j.message.Message in project logging-log4j2 by apache.
the class AsyncLoggerConfigTest3 method testNoConcurrentModificationException.
@Test
public void testNoConcurrentModificationException() throws Exception {
System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, "AsyncLoggerConfigTest2.xml");
final File file = new File("target", "AsyncLoggerConfigTest2.log");
assertTrue("Deleted old file before test", !file.exists() || file.delete());
final Logger log = LogManager.getLogger("com.foo.Bar");
log.info("initial message");
Thread.sleep(500);
final Map<String, String> map = new HashMap<>();
for (int j = 0; j < 3000; j++) {
map.put(String.valueOf(j), String.valueOf(System.nanoTime()));
}
final Message msg = new ParameterizedMessage("{}", map);
Log4jLogEvent event = Log4jLogEvent.newBuilder().setLevel(Level.WARN).setLoggerName(getClass().getName()).setMessage(msg).setTimeMillis(0).build();
for (int i = 0; i < 100; i++) {
((AsyncLoggerConfig) ((org.apache.logging.log4j.core.Logger) log).get()).callAppenders(event);
for (int j = 0; j < 3000; j++) {
map.remove(String.valueOf(j));
}
for (int j = 0; j < 3000; j++) {
map.put(String.valueOf(j), String.valueOf(System.nanoTime()));
}
}
}
use of org.apache.logging.log4j.message.Message in project logging-log4j2 by apache.
the class AsyncLogger method translateTo.
/*
* (non-Javadoc)
*
* @see com.lmax.disruptor.EventTranslatorVararg#translateTo(java.lang.Object, long, java.lang.Object[])
*/
@Override
public void translateTo(final RingBufferLogEvent event, final long sequence, final Object... args) {
// Implementation note: candidate for optimization: exceeds 35 bytecodes.
final AsyncLogger asyncLogger = (AsyncLogger) args[0];
final StackTraceElement location = (StackTraceElement) args[1];
final String fqcn = (String) args[2];
final Level level = (Level) args[3];
final Marker marker = (Marker) args[4];
final Message message = (Message) args[5];
final Throwable thrown = (Throwable) args[6];
// needs shallow copy to be fast (LOG4J2-154)
final ContextStack contextStack = ThreadContext.getImmutableStack();
final Thread currentThread = Thread.currentThread();
final String threadName = THREAD_NAME_CACHING_STRATEGY.getThreadName();
event.setValues(asyncLogger, asyncLogger.getName(), marker, fqcn, level, message, thrown, // in the AsyncLogger#actualAsyncLog method
CONTEXT_DATA_INJECTOR.injectContextData(null, (StringMap) event.getContextData()), contextStack, currentThread.getId(), threadName, currentThread.getPriority(), location, CLOCK, nanoClock);
}
use of org.apache.logging.log4j.message.Message in project logging-log4j2 by apache.
the class CategoryTest method testMessageImplementation.
private <M extends Message> void testMessageImplementation(final Object messagePayload, final Class<M> expectedMessageClass, final Consumer<M> messageTester) {
// Setup the logger and the appender.
final Category category = Category.getInstance("TestCategory");
final org.apache.logging.log4j.core.Logger logger = (org.apache.logging.log4j.core.Logger) category.getLogger();
logger.addAppender(appender);
// Log the message payload.
category.info(messagePayload);
// Verify collected log events.
final List<LogEvent> events = appender.getEvents();
assertEquals("was expecting a single event", 1, events.size());
final LogEvent logEvent = events.get(0);
// Verify the collected message.
final Message message = logEvent.getMessage();
final Class<? extends Message> actualMessageClass = message.getClass();
assertTrue("was expecting message to be instance of " + expectedMessageClass + ", found: " + actualMessageClass, expectedMessageClass.isAssignableFrom(actualMessageClass));
@SuppressWarnings("unchecked") final M typedMessage = (M) message;
messageTester.accept(typedMessage);
}
use of org.apache.logging.log4j.message.Message in project logging-log4j2 by apache.
the class Log4jLogEventTest method testBuilderCorrectlyCopiesAllEventAttributes.
@Test
public void testBuilderCorrectlyCopiesAllEventAttributes() {
final StringMap contextData = ContextDataFactory.createContextData();
contextData.putValue("A", "B");
final ContextStack contextStack = ThreadContext.getImmutableStack();
final Exception exception = new Exception("test");
final Marker marker = MarkerManager.getMarker("EVENTTEST");
final Message message = new SimpleMessage("foo");
final StackTraceElement stackTraceElement = new StackTraceElement("A", "B", "file", 123);
final String fqcn = "qualified";
final String name = "Ceci n'est pas une pipe";
final String threadName = "threadName";
final Log4jLogEvent event = //
Log4jLogEvent.newBuilder().setContextData(//
contextData).setContextStack(//
contextStack).setEndOfBatch(//
true).setIncludeLocation(//
true).setLevel(//
Level.FATAL).setLoggerFqcn(//
fqcn).setLoggerName(//
name).setMarker(//
marker).setMessage(//
message).setNanoTime(//
1234567890L).setSource(//
stackTraceElement).setThreadName(//
threadName).setThrown(//
exception).setTimeMillis(987654321L).build();
assertEquals(contextData, event.getContextData());
assertSame(contextStack, event.getContextStack());
assertTrue(event.isEndOfBatch());
assertTrue(event.isIncludeLocation());
assertSame(Level.FATAL, event.getLevel());
assertSame(fqcn, event.getLoggerFqcn());
assertSame(name, event.getLoggerName());
assertSame(marker, event.getMarker());
assertSame(message, event.getMessage());
assertEquals(1234567890L, event.getNanoTime());
assertSame(stackTraceElement, event.getSource());
assertSame(threadName, event.getThreadName());
assertSame(exception, event.getThrown());
assertEquals(987654321L, event.getTimeMillis());
final LogEvent event2 = new Log4jLogEvent.Builder(event).build();
assertEquals(event2, event, "copy constructor builder");
assertEquals(event2.hashCode(), event.hashCode(), "same hashCode");
}
Aggregations