}
@Test
public void test() throws IOException, JoranException {
Logger logger = LoggerFactory.getLogger(this.getClass());
MDC.clear();
String message = "Testing empty MDC";
// Basic Request
logger.debug(message);
sleep();
lastRequest = server.lastRequest();
assertMapEquals(makeMap(message), removeFields(lastRequest));
assertTrue(lastRequest.containsKey("level"));
assertTrue(lastRequest.containsKey("timestamp"));
assertTrue(lastRequest.containsKey("host"));
// Test with IP and requestID in MDC
ipAddress = "87.345.23.55";
MDC.put("ipAddress", ipAddress);
requestID = String.valueOf(new Random().nextInt(100000));
MDC.put("requestId", requestID);
message = "this is a new test";
logger.debug(message);
sleep();
lastRequest = server.lastRequest();
assertMapEquals(makeMap(message), removeFields(lastRequest));
assertTrue(lastRequest.containsKey("level"));
assertTrue(lastRequest.containsKey("timestamp"));
// Test substitution works
logger.debug("this is a test with ({}) parameter", "this");
sleep();
lastRequest = server.lastRequest();
assertMapEquals(makeMap("this is a test with (this) parameter"), removeFields(lastRequest));
assertTrue(lastRequest.containsKey("level"));
assertTrue(lastRequest.containsKey("timestamp"));
// Test file and line are output for stack trace
try {
new URL("app://asdfs");
} catch (Exception e) {
int ln = Thread.currentThread().getStackTrace()[1].getLineNumber();
logger.error("expected error", new IllegalStateException(e));
sleep();
lastRequest = server.lastRequest();
assertEquals(ln + 1, Integer.parseInt((String) lastRequest.get("line")));
assertEquals(this.getClass().getSimpleName() + ".java", lastRequest.get("file"));
}
// Test field in MDC is added even if not included in additional fields
MDC.put("newField", "the val");
message = "this is a test with an MDC field (new_field) that is not included in the additional fields. " +
"However includeFullMDC is set, so it should appear in the additional fields as _newField = the val";
logger.debug(message, "this");
sleep();
lastRequest = server.lastRequest();
assertMapEquals(addField(makeMap(message), "_newField", "the val"), removeFields(lastRequest));
assertTrue(lastRequest.containsKey("level"));
assertTrue(lastRequest.containsKey("timestamp"));
// Test static additional field
message = "Testing with a static additional field";
MDC.clear();
ipAddress = null;
requestID = null;
addStaticFieldToAppender();
logger.debug(message);
sleep();
lastRequest = server.lastRequest();
assertMapEquals(addField(makeMap(message, "Testing wi"), "_node_name", "www013"), removeFields(lastRequest));
assertTrue(lastRequest.containsKey("level"));
assertTrue(lastRequest.containsKey("timestamp"));
// Test field type conversion
addTypedFieldToAppender();
int i = new Random().nextInt(100000);
MDC.put("requestId", String.valueOf(i));
message = "Testing requestId as int field conversion";
logger.debug(message);
sleep();
lastRequest = server.lastRequest();
assertTrue(lastRequest.get("_request_id").getClass().getSimpleName(), lastRequest.get("_request_id") instanceof Number);
assertEquals(i, ((Number) lastRequest.get("_request_id")).intValue());
// Test failed type conversion
MDC.put("requestId", "nonparseablenumber");
message = "Testing failed requestId as int field conversion";
logger.debug(message);
sleep();
lastRequest = server.lastRequest();
assertEquals("nonparseablenumber", lastRequest.get("_request_id"));
// Finish
server.shutdown();
logger.debug("This is a test with a really long ending: " + longMessage);
}