use of org.apache.logging.log4j.message.ObjectMessage in project logging-log4j2 by apache.
the class ScriptFilter method filter.
@Override
public Result filter(final Logger logger, final Level level, final Marker marker, final Object msg, final Throwable t) {
final SimpleBindings bindings = new SimpleBindings();
bindings.put("logger", logger);
bindings.put("level", level);
bindings.put("marker", marker);
bindings.put("message", msg instanceof String ? new SimpleMessage((String) msg) : new ObjectMessage(msg));
bindings.put("parameters", null);
bindings.put("throwable", t);
bindings.putAll(configuration.getProperties());
bindings.put("substitutor", configuration.getStrSubstitutor());
final Object object = configuration.getScriptManager().execute(script.getName(), bindings);
return object == null || !Boolean.TRUE.equals(object) ? onMismatch : onMatch;
}
use of org.apache.logging.log4j.message.ObjectMessage in project logging-log4j2 by apache.
the class LevelTest method testLevelLogging.
@Test
public void testLevelLogging() {
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(description + ": No events", events);
assertThat(events, hasSize(expected.expectedEventCount));
final LogEvent event = events.get(0);
assertEquals(description + ": Expected level " + expected.expectedInitialEventLevel + ", got" + event.getLevel(), event.getLevel().name(), expected.expectedInitialEventLevel);
}
}
use of org.apache.logging.log4j.message.ObjectMessage in project logging-log4j2 by apache.
the class Category method maybeLog.
void maybeLog(final String fqcn, final org.apache.logging.log4j.Level level, final Object message, final Throwable throwable) {
if (logger.isEnabled(level)) {
final Message msg;
if (message instanceof String) {
msg = new SimpleMessage((String) message);
} else // SimpleMessage treats String and CharSequence differently, hence this else-if block.
if (message instanceof CharSequence) {
msg = new SimpleMessage((CharSequence) message);
} else if (message instanceof Map) {
@SuppressWarnings("unchecked") final Map<String, ?> map = (Map<String, ?>) message;
msg = new MapMessage<>(map);
} else {
msg = new ObjectMessage(message);
}
if (logger instanceof ExtendedLogger) {
((ExtendedLogger) logger).logMessage(fqcn, level, null, msg, throwable);
} else {
logger.log(level, msg, throwable);
}
}
}
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, final Throwable t) {
if (isEnabledFor(priority)) {
@SuppressWarnings("unchecked") final Message msg = message instanceof Map ? new MapMessage((Map) message) : new ObjectMessage(message);
forcedLog(FQCN, priority, msg, t);
}
}
use of org.apache.logging.log4j.message.ObjectMessage in project logging-log4j2 by apache.
the class MessageResolverTest method test_ObjectMessage.
@Test
void test_ObjectMessage() {
// Create the log event.
final int id = 0xDEADBEEF;
final String name = "name-" + id;
final Object attachment = new LinkedHashMap<String, Object>() {
{
put("id", id);
put("name", name);
}
};
final ObjectMessage message = new ObjectMessage(attachment);
final LogEvent logEvent = Log4jLogEvent.newBuilder().setMessage(message).build();
// Create the event template.
final String eventTemplate = writeJson(asMap("message", asMap("$resolver", "message")));
// Create the layout.
JsonTemplateLayout layout = JsonTemplateLayout.newBuilder().setConfiguration(CONFIGURATION).setStackTraceEnabled(true).setEventTemplate(eventTemplate).build();
// Check the serialized event.
usingSerializedLogEventAccessor(layout, logEvent, accessor -> {
assertThat(accessor.getInteger(new String[] { "message", "id" })).isEqualTo(id);
assertThat(accessor.getString(new String[] { "message", "name" })).isEqualTo(name);
});
}
Aggregations