JsonLayout extends JsonLayoutBase<IAccessEvent> { public JsonLayout() { super(); this.includeRemoteAddr = true; this.includeRemoteUser = true; this.includeRequestTime = true; this.includeRequestURI = true; this.includeStatusCode = true; this.includeMethod = true; this.includeProtocol = true; this.includeRequestURL = false; this.includeContentLength = false; this.includeRemoteHost = true; this.includeServerName = true; this.includeRequestHeader = true; this.includeRequestParameter = true; this.includeLocalPort = false; this.includeRequestContent = false; this.includeResponseContent = false; } JsonLayout(); boolean isIncludeRemoteAddr(); void setIncludeRemoteAddr(boolean includeRemoteAddr); boolean isIncludeRemoteUser(); void setIncludeRemoteUser(boolean includeRemoteUser); boolean isIncludeRequestTime(); void setIncludeRequestTime(boolean includeRequestTime); boolean isIncludeRequestURI(); void setIncludeRequestURI(boolean includeRequestURI); boolean isIncludeStatusCode(); void setIncludeStatusCode(boolean includeStatusCode); boolean isIncludeMethod(); void setIncludeMethod(boolean includeMethod); boolean isIncludeProtocol(); void setIncludeProtocol(boolean includeProtocol); boolean isIncludeContentLength(); void setIncludeContentLength(boolean includeContentLength); boolean isIncludeRequestURL(); void setIncludeRequestURL(boolean includeRequestURL); boolean isIncludeRemoteHost(); void setIncludeRemoteHost(boolean includeRemoteHost); boolean isIncludeServerName(); void setIncludeServerName(boolean includeServerName); boolean isIncludeRequestHeader(); void setIncludeRequestHeader(boolean includeRequestHeader); boolean isIncludeRequestParameter(); void setIncludeRequestParameter(boolean includeRequestParameter); boolean isIncludeLocalPort(); void setIncludeLocalPort(boolean includeLocalPort); boolean isIncludeRequestContent(); void setIncludeRequestContent(boolean includeRequestContent); boolean isIncludeResponseContent(); void setIncludeResponseContent(boolean includeResponseContent); static final String TIMESTAMP_ATTR_NAME; static final String REMOTEADDR_ATTR_NAME; static final String REMOTEUSER_ATTR_NAME; static final String REQUESTTIME_ATTR_NAME; static final String REQUESTURI_ATTR_NAME; static final String STATUSCODE_ATTR_NAME; static final String METHOD_ATTR_NAME; static final String PROTOCOL_ATTR_NAME; static final String CONTENTLENGTH_ATTR_NAME; static final String REQUESTURL_ATTR_NAME; static final String REMOTEHOST_ATTR_NAME; static final String SERVERNAME_ATTR_NAME; static final String REQUESTHEADER_ATTR_NAME; static final String REQUESTPARAMETER_ATTR_NAME; static final String LOCALPORT_ATTR_NAME; static final String REQUESTCONTENT_ATTR_NAME; static final String RESPONSECONTENT_ATTR_NAME; }
@Test public void jsonLayout() throws Exception { configure(TEST_DIR_PREFIX + "input/json/jsonLayout.xml"); ListAppender<IAccessEvent> listAppender = (ListAppender<IAccessEvent>) context.getAppender("STR_LIST"); IAccessEvent event = DummyAccessEventBuilder.buildNewAccessEvent(); listAppender.doAppend(event); assertEquals(1, listAppender.list.size()); IAccessEvent iAccessEvent = listAppender.list.get(0); JsonLayout jsonLayout = new JsonLayout(); jsonLayout.setContext(context); String log = jsonLayout.doLayout(iAccessEvent); assertThat(log, containsString(String.format("%s=%s", JsonLayout.REMOTEHOST_ATTR_NAME, event.getRemoteHost()))); assertThat(log, containsString(String.format("%s=%s", JsonLayout.REMOTEUSER_ATTR_NAME, event.getRemoteUser()))); assertThat(log, containsString(String.format("%s=%s", JsonLayout.REMOTEADDR_ATTR_NAME, event.getRemoteAddr()))); assertThat(log, containsString(String.format("%s=%s", JsonLayout.METHOD_ATTR_NAME, event.getMethod()))); if (event.getRequestHeaderMap().size() == 2) { Iterator<Map.Entry<String, String>> iterator = event.getRequestHeaderMap().entrySet().iterator(); Map.Entry<String, String> firstInMap = iterator.next(); Map.Entry<String, String> secondInMap = iterator.next(); assertThat(log, containsString(String.format("%s={%s=%s, %s=%s}", JsonLayout.REQUESTHEADER_ATTR_NAME, firstInMap.getKey(), firstInMap.getValue(), secondInMap.getKey(), secondInMap.getValue()))); } assertThat(log, containsString(String.format("%s=%s", JsonLayout.PROTOCOL_ATTR_NAME, event.getProtocol()))); assertThat(log, containsString(String.format("%s=%s", JsonLayout.SERVERNAME_ATTR_NAME, event.getServerName()))); assertThat(log, containsString(String.format("%s=%s", JsonLayout.STATUSCODE_ATTR_NAME, event.getStatusCode()))); }