use of org.apache.logging.log4j.message.ObjectMessage in project logging-log4j2 by apache.
the class Category method log.
public void log(final String fqcn, final Priority priority, final Object message, final Throwable t) {
if (isEnabledFor(priority)) {
final Message msg = new ObjectMessage(message);
forcedLog(fqcn, priority, msg, t);
}
}
use of org.apache.logging.log4j.message.ObjectMessage in project logging-log4j2 by apache.
the class Category method log.
public void log(final Priority priority, final Object message) {
if (isEnabledFor(priority)) {
@SuppressWarnings("unchecked") final Message msg = message instanceof Map ? new MapMessage((Map) message) : new ObjectMessage(message);
forcedLog(FQCN, priority, msg, null);
}
}
use of org.apache.logging.log4j.message.ObjectMessage in project logging-log4j2 by apache.
the class LoggerTest method flowTracingString_SupplierOfObjectMessages.
@Test
public void flowTracingString_SupplierOfObjectMessages() {
final EntryMessage msg = logger.traceEntry("doFoo(a={}, b={})", new Supplier<Message>() {
@Override
public Message get() {
return new ObjectMessage(1);
}
}, new Supplier<Message>() {
@Override
public Message get() {
return new ObjectMessage(2);
}
});
logger.traceExit(msg, 3);
assertEquals(2, results.size());
assertThat("Incorrect Entry", results.get(0), startsWith("ENTER[ FLOW ] TRACE Enter"));
assertThat("Missing entry data", results.get(0), containsString("doFoo(a=1, b=2)"));
assertThat("Incorrect Exit", results.get(1), startsWith("EXIT[ FLOW ] TRACE Exit"));
assertThat("Missing exit data", results.get(1), containsString("doFoo(a=1, b=2): 3"));
}
use of org.apache.logging.log4j.message.ObjectMessage in project logging-log4j2 by apache.
the class LoggerSupplierTest method flowTracing_SupplierOfObjectMessage.
@Test
public void flowTracing_SupplierOfObjectMessage() {
logger.traceEntry(() -> new ObjectMessage(1234567890));
assertThat(results).hasSize(1);
String entry = results.get(0);
assertThat(entry).startsWith("ENTER[ FLOW ] TRACE Enter").contains("(1234567890)").doesNotContain("ObjectMessage");
}
use of org.apache.logging.log4j.message.ObjectMessage in project logging-log4j2 by apache.
the class LevelTest method testLevelLogging.
@Test
public void testLevelLogging(final LoggerContext context) {
final Marker marker = MarkerManager.getMarker("marker");
final Message msg = new ObjectMessage("msg");
final Throwable t = new Throwable("test");
final Level[] levels = new Level[] { Level.TRACE, Level.DEBUG, Level.INFO, Level.WARN, Level.ERROR, Level.FATAL };
final String[] names = new String[] { "levelTest", "levelTest.Trace", "levelTest.Debug", "levelTest.Info", "levelTest.Warn", "levelTest.Error", "levelTest.Fatal" };
for (final Level level : levels) {
for (final String name : names) {
final Logger logger = context.getLogger(name);
// Message
logger.log(level, msg);
// Object
logger.log(level, 123);
// String
logger.log(level, name);
// Marker, Message
logger.log(level, marker, msg);
// Marker, Object
logger.log(level, marker, 123);
// marker, String
logger.log(level, marker, name);
// Message, Throwable
logger.log(level, msg, t);
// Object, Throwable
logger.log(level, 123, t);
// String, Object...
logger.log(level, name, "param1", "param2");
// String, Throwable
logger.log(level, name, t);
// Marker, Message, Throwable
logger.log(level, marker, msg, t);
// Marker, Object, Throwable
logger.log(level, marker, 123, t);
// Marker, String, Object...
logger.log(level, marker, name, "param1", "param2");
// Marker, String, Throwable
logger.log(level, marker, name, t);
}
}
// Logger "levelTest" will not receive same events as "levelTest.Trace"
int levelCount = names.length - 1;
final int UNIT = 14;
final Expected[] expectedResults = new Expected[] { //
new Expected(listAll, UNIT * levelCount, "TRACE", "All"), //
new Expected(listTrace, UNIT * levelCount--, "TRACE", "Trace"), //
new Expected(listDebug, UNIT * levelCount--, "DEBUG", "Debug"), //
new Expected(listInfo, UNIT * levelCount--, "INFO", "Info"), //
new Expected(listWarn, UNIT * levelCount--, "WARN", "Warn"), //
new Expected(listError, UNIT * levelCount--, "ERROR", "Error"), //
new Expected(listFatal, UNIT * levelCount--, "FATAL", "Fatal") };
for (final Expected expected : expectedResults) {
final String description = expected.description;
final List<LogEvent> events = expected.appender.getEvents();
assertNotNull(events, description + ": No events");
assertThat(events, hasSize(expected.expectedEventCount));
final LogEvent event = events.get(0);
assertEquals(event.getLevel().name(), expected.expectedInitialEventLevel, description + ": Expected level " + expected.expectedInitialEventLevel + ", got" + event.getLevel());
}
}
Aggregations