use of org.apache.logging.log4j.message.Message in project Railcraft by Railcraft.
the class CommandDebug method printLine.
static void printLine(ICommandSender sender, String msg, Object... args) {
Message msgObj;
if (args.length == 0) {
msgObj = msgFactory.newMessage(msg);
} else {
msgObj = msgFactory.newMessage(msg, args);
}
Game.log().msg(DEBUG_LEVEL, msgObj);
sender.sendMessage(ChatPlugin.makeMessage(msgObj.getFormattedMessage()));
}
use of org.apache.logging.log4j.message.Message 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.Message 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.Message in project logging-log4j2 by apache.
the class JsonTemplateLayoutTest method test_timestamp_epoch_resolvers.
@Test
@SuppressWarnings("FloatingPointLiteralPrecision")
void test_timestamp_epoch_resolvers() {
final List<Map<String, Object>> testCases = Arrays.asList(asMap("epochSecs", new BigDecimal("1581082727.982123456"), "epochSecsRounded", 1581082727, "epochSecsNanos", 982123456, "epochMillis", new BigDecimal("1581082727982.123456"), "epochMillisRounded", 1581082727982L, "epochMillisNanos", 123456, "epochNanos", 1581082727982123456L), asMap("epochSecs", new BigDecimal("1591177590.005000001"), "epochSecsRounded", 1591177590, "epochSecsNanos", 5000001, "epochMillis", new BigDecimal("1591177590005.000001"), "epochMillisRounded", 1591177590005L, "epochMillisNanos", 1, "epochNanos", 1591177590005000001L));
// Create the event template.
final String eventTemplate = writeJson(asMap("epochSecs", asMap("$resolver", "timestamp", "epoch", asMap("unit", "secs")), "epochSecsRounded", asMap("$resolver", "timestamp", "epoch", asMap("unit", "secs", "rounded", true)), "epochSecsNanos", asMap("$resolver", "timestamp", "epoch", asMap("unit", "secs.nanos")), "epochMillis", asMap("$resolver", "timestamp", "epoch", asMap("unit", "millis")), "epochMillisRounded", asMap("$resolver", "timestamp", "epoch", asMap("unit", "millis", "rounded", true)), "epochMillisNanos", asMap("$resolver", "timestamp", "epoch", asMap("unit", "millis.nanos")), "epochNanos", asMap("$resolver", "timestamp", "epoch", asMap("unit", "nanos"))));
// Create the layout.
final JsonTemplateLayout layout = JsonTemplateLayout.newBuilder().setConfiguration(CONFIGURATION).setEventTemplate(eventTemplate).build();
testCases.forEach(testCase -> {
// Create the log event.
final SimpleMessage message = new SimpleMessage("Hello, World!");
final Level level = Level.ERROR;
final MutableInstant instant = new MutableInstant();
final Object instantSecsObject = testCase.get("epochSecsRounded");
final long instantSecs = instantSecsObject instanceof Long ? (long) instantSecsObject : (int) instantSecsObject;
final int instantSecsNanos = (int) testCase.get("epochSecsNanos");
instant.initFromEpochSecond(instantSecs, instantSecsNanos);
final LogEvent logEvent = Log4jLogEvent.newBuilder().setLoggerName(LOGGER_NAME).setLevel(level).setMessage(message).setInstant(instant).build();
// Verify the test case.
usingSerializedLogEventAccessor(layout, logEvent, accessor -> testCase.forEach((key, expectedValue) -> Assertions.assertThat(accessor.getObject(key)).describedAs("key=%s", key).isEqualTo(expectedValue)));
});
}
use of org.apache.logging.log4j.message.Message in project logging-log4j2 by apache.
the class JsonTemplateLayoutTest method testMessageParameterResolver.
private static void testMessageParameterResolver(final MessageFactory messageFactory) {
// Create the event template.
final String eventTemplate = writeJson(asMap("po*", asMap("$resolver", "messageParameter"), "ps*", asMap("$resolver", "messageParameter", "stringified", true), "po2", asMap("$resolver", "messageParameter", "index", 2), "ps2", asMap("$resolver", "messageParameter", "index", 2, "stringified", true), "po3", asMap("$resolver", "messageParameter", "index", 3)));
// Create the layout.
final JsonTemplateLayout layout = JsonTemplateLayout.newBuilder().setConfiguration(CONFIGURATION).setEventTemplate(eventTemplate).build();
// Create the log event.
final Object[] parameters = { 1L + (long) Integer.MAX_VALUE, "foo", 56 };
final Message message = messageFactory.newMessage("foo", parameters);
final Level level = Level.FATAL;
final LogEvent logEvent = Log4jLogEvent.newBuilder().setLoggerName(LOGGER_NAME).setMessage(message).setLevel(level).build();
// Check the serialized event.
usingSerializedLogEventAccessor(layout, logEvent, accessor -> {
assertThat(accessor.getObject("po*")).isEqualTo(Arrays.asList(parameters));
List<String> stringifiedParameters = Arrays.stream(parameters).map(String::valueOf).collect(Collectors.toList());
assertThat(accessor.getObject("ps*")).isEqualTo(stringifiedParameters);
assertThat(accessor.getObject("po2")).isEqualTo(parameters[2]);
assertThat(accessor.getString("ps2")).isEqualTo(stringifiedParameters.get(2));
assertThat(accessor.getString("ps3")).isNull();
});
}
Aggregations