@Test public void appenderUsesProvidedLayoutWhenMessageOnlyIsSetToFalse() throws IOException, NoSuchMethodException, SecurityException { // given AbstractStringLayout layout = PowerMockito.mock(AbstractStringLayout.class); ElasticsearchAppender.Builder builder = ElasticsearchAppenderTest.createTestElasticsearchAppenderBuilder(); builder.withMessageOnly(false); builder.withLayout(layout); LogEvent logEvent = mock(LogEvent.class); ElasticsearchAppender appender = builder.build(); // when appender.append(logEvent); // then ArgumentCaptor<LogEvent> captor = ArgumentCaptor.forClass(LogEvent.class); verify(layout, times(1)).toSerializable(captor.capture()); assertEquals(logEvent, captor.getValue()); }
@Override public ServletAppender build() { final String name = getName(); if (name == null) { LOGGER.error("No name provided for ServletAppender"); } final ServletContext servletContext = WebLoggerContextUtils.getServletContext(); if (servletContext == null) { LOGGER.error("No servlet context is available"); return null; } Layout<? extends Serializable> layout = getLayout(); if (layout == null) { layout = PatternLayout.createDefaultLayout(); } else if (!(layout instanceof AbstractStringLayout)) { LOGGER.error("Layout must be a StringLayout to log to ServletContext"); return null; } return new ServletAppender(name, layout, getFilter(), servletContext, isIgnoreExceptions(), logThrowables); }
protected ElasticsearchAppender(String name, Filter filter, AbstractStringLayout layout, boolean ignoreExceptions, BatchDelivery batchDelivery, boolean messageOnly, IndexNameFormatter indexNameFormatter) { super(name, filter, layout, ignoreExceptions); this.messageOnly = messageOnly; this.batchDelivery = batchDelivery; this.indexNameFormatter = indexNameFormatter; }
@Override protected String log(Object event, LogDetails logDetails) { AbstractStringLayout layout = Log4j2JSONLayout.createLayout(logDetails.isLocationInfo(), // location true, // properties true, // complete true, // compact false, // eventEol Charset.defaultCharset(), null); return layout.toSerializable((LogEvent) event); }
@Override public void append(final LogEvent event) { final String serialized = ((AbstractStringLayout) getLayout()).toSerializable(event); if (logThrowables) { servletContext.log(serialized, event.getThrown()); } else { servletContext.log(serialized); } }
private static AbstractStringLayout getDetailedLayout() { return DxFeedPatternLayout.createDefaultLayout(null); }
/** * Creates a JSON Layout. * * @param locationInfo * If "true", includes the location information in the generated JSON. * @param properties * If "true", includes the thread context in the generated JSON. * @param complete * If "true", includes the JSON header and footer, defaults to "false". * @param compact * If "true", does not use end-of-lines and indentation, defaults to "false". * @param eventEol * If "true", forces an EOL after each log event (even if compact is "true"), defaults to "false". This * allows one even per line, even in compact mode. * @param charset * The character set to use, if {@code null}, uses "UTF-8". * @param pairs * MDC attributes * @return A JSON Layout. */ @PluginFactory public static AbstractStringLayout createLayout( // @formatter:off @PluginAttribute(value = "locationInfo", defaultBoolean = false) final boolean locationInfo, @PluginAttribute(value = "properties", defaultBoolean = false) final boolean properties, @PluginAttribute(value = "complete", defaultBoolean = false) final boolean complete, @PluginAttribute(value = "compact", defaultBoolean = false) final boolean compact, @PluginAttribute(value = "eventEol", defaultBoolean = false) final boolean eventEol, @PluginAttribute(value = "charset", defaultString = "UTF-8") final Charset charset, @PluginElement("Pairs") final KeyValuePair[] pairs // @formatter:on ) { //Unpacke the pairs list final Map<String, String> additionalLogAttributes = unpackPairs(pairs); return new Log4j2JSONLayout(locationInfo, charset, additionalLogAttributes); }
/** * Default: {@code org.apache.logging.log4j.core.layout.JsonLayout} * * @param layout layout to be used */ public void withLayout(AbstractStringLayout layout) { this.layout = layout; }