/** * Prepare log event log event. * * @param logEvent the log event * @return the log event */ public static LogEvent prepareLogEvent(final LogEvent logEvent) { final String messageModified = TicketIdSanitizationUtils.sanitize(logEvent.getMessage().getFormattedMessage()); final Message message = new SimpleMessage(messageModified); final LogEvent newLogEvent = Log4jLogEvent.newBuilder() .setLevel(logEvent.getLevel()) .setLoggerName(logEvent.getLoggerName()) .setLoggerFqcn(logEvent.getLoggerFqcn()) .setContextMap(logEvent.getContextMap()) .setContextStack(logEvent.getContextStack()) .setEndOfBatch(logEvent.isEndOfBatch()) .setIncludeLocation(logEvent.isIncludeLocation()) .setMarker(logEvent.getMarker()) .setMessage(message) .setNanoTime(logEvent.getNanoTime()) .setSource(logEvent.getSource()) .setThreadName(logEvent.getThreadName()) .setThrownProxy(logEvent.getThrownProxy()) .setThrown(logEvent.getThrown()) .setTimeMillis(logEvent.getTimeMillis()) .build(); return newLogEvent; }
@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()); }
@Test public void resolvesAppenderRefOnlyOnce() { // given Appender appender = mock(Appender.class); when(appender.isStarted()).thenReturn(true); Configuration configuration = mock(Configuration.class); String testAppenderRef = "testAppenderRef"; when(configuration.getAppender(testAppenderRef)).thenReturn(appender); FailoverPolicy<String> failoverPolicy = createTestFailoverPolicy(testAppenderRef, configuration); String failedMessage = "test failed message"; // when failoverPolicy.deliver(failedMessage); failoverPolicy.deliver(failedMessage); // then verify(configuration, times(1)).getAppender(anyString()); verify(appender, times(2)).append(any(LogEvent.class)); }
@Override public void append(LogEvent event) { Level level = event.getLevel(); if (TRACE.equals(level)) { TRACE_LABEL.inc(); } else if (DEBUG.equals(level)) { DEBUG_LABEL.inc(); } else if (INFO.equals(level)) { INFO_LABEL.inc(); } else if (WARN.equals(level)) { WARN_LABEL.inc(); } else if (ERROR.equals(level)) { ERROR_LABEL.inc(); } else if (FATAL.equals(level)) { FATAL_LABEL.inc(); } }
@Override public void append(LogEvent event) { lock.lock(); try { String json = new String(getLayout().toByteArray(event)); buffered.add(getBulkItem(json)); this.check(); } catch (Exception ex) { if (!ignoreExceptions()) { throw new AppenderLoggingException(ex); } else { LOGGER.error("Failed to process event.", ex); } } finally { lock.unlock(); } }
@Test public void test() { { Logger logger = LogManager.getLogger("test.logger"); logger.error("This is an error!"); logger.error("This is another error!"); logger.error("This is a third error!"); } assertThat(collector.getLogs()).hasSize(3) .contains("This is an error!", "This is another error!", "This is a third error!"); List<LogEvent> rawLogs = (List<LogEvent>) collector.getRawLogs(); assertThat(rawLogs).hasSize(3); assertTrue(rawLogs.stream().allMatch(l -> l.getLevel() == Level.ERROR)); }
@Override public synchronized void append(final LogEvent event) { final Layout<? extends Serializable> layout = getLayout(); if (layout == null) { if (event instanceof MutableLogEvent) { // must take snapshot or subsequent calls to logger.log() will modify this event events.add(((MutableLogEvent) event).createMemento()); } else { events.add(event); } } else if (layout instanceof SerializedLayout) { final byte[] header = layout.getHeader(); final byte[] content = layout.toByteArray(event); final byte[] record = new byte[header.length + content.length]; System.arraycopy(header, 0, record, 0, header.length); System.arraycopy(content, 0, record, header.length, content.length); data.add(record); } else { write(layout.toByteArray(event)); } if (countDownLatch != null) { countDownLatch.countDown(); } }
public void watch(Class<?> loggerClass, Level level) { this.loggerClass = loggerClass; Appender appender = new AbstractAppender(APPENDER_NAME, null, PatternLayout.createDefaultLayout()) { @Override public void append(LogEvent event) { logEvents.add(event); } }; appender.start(); final LoggerContext ctx = getLoggerContext(); LoggerConfig loggerConfig = ctx.getConfiguration().getLoggerConfig(loggerClass.getName()); oldLevel = loggerConfig.getLevel(); loggerConfig.setLevel(level); loggerConfig.addAppender(appender, level, null); ctx.updateLoggers(); }
@Test public void logEventShouldMatchCustomConfig() throws Exception { String logLogger = getClass().getName(); Level logLevel = Level.DEBUG; String logMessage = "this is a log statement"; Logger logger = LogService.getLogger(); logger.debug(logMessage); BasicAppender appender = BasicAppender.getInstance(); assertThat(appender).isNotNull(); assertThat(appender.events()).hasSize(1); LogEvent event = appender.events().get(0); assertThat(event.getLoggerName()).isEqualTo(logLogger); assertThat(event.getLevel()).isEqualTo(logLevel); assertThat(event.getMessage().getFormattedMessage()).isEqualTo(logMessage); assertThat(systemOutRule.getLog()).contains(logLevel.name()); assertThat(systemOutRule.getLog()).contains(logMessage); assertThat(systemOutRule.getLog()).contains(CONFIG_LAYOUT_PREFIX); assertThat(systemOutRule.getLog()).matches(defineLogStatementRegex(logLevel, logMessage)); }
@Override public void append(LogEvent event) { if (reader != null) { try { Writer out = reader.getOutput(); out.write(RESET_LINE); out.write(formatEvent(event)); reader.drawLine(); reader.flush(); } catch (IOException ignored) { } } else { out.print(formatEvent(event)); } }
@Test public void deliversToAppenderRef() { // given Appender appender = mock(Appender.class); when(appender.isStarted()).thenReturn(true); Configuration configuration = mock(Configuration.class); String testAppenderRef = "testAppenderRef"; when(configuration.getAppender(testAppenderRef)).thenReturn(appender); FailoverPolicy<String> failoverPolicy = createTestFailoverPolicy(testAppenderRef, configuration); String failedMessage = "test failed message"; // when failoverPolicy.deliver(failedMessage); // then verify(appender, times(1)).append(any(LogEvent.class)); }
@Override public JsonNode resolve(TemplateResolverContext context, LogEvent logEvent, String key) { ThreadContext.ContextStack contextStack = logEvent.getContextStack(); if (contextStack.getDepth() == 0) { return null; } Pattern itemPattern = context.getNdcPattern(); ArrayNode contextStackNode = context.getObjectMapper().createArrayNode(); for (String contextStackItem : contextStack.asList()) { boolean matches = itemPattern == null || itemPattern.matcher(contextStackItem).matches(); if (matches) { contextStackNode.add(contextStackItem); } } return contextStackNode; }
private void checkLogEvent(Configuration config, LogEvent logEvent, String lookupTestKey, String lookupTestVal) throws IOException { Set<String> mdcKeys = logEvent.getContextData().toMap().keySet(); String firstMdcKey = mdcKeys.iterator().next(); String firstMdcKeyExcludingRegex = mdcKeys.isEmpty() ? null : String.format("^(?!%s).*$", Pattern.quote(firstMdcKey)); List<String> ndcItems = logEvent.getContextStack().asList(); String firstNdcItem = ndcItems.get(0); String firstNdcItemExcludingRegex = ndcItems.isEmpty() ? null : String.format("^(?!%s).*$", Pattern.quote(firstNdcItem)); LogstashLayout layout = LogstashLayout .newBuilder() .setConfiguration(config) .setTemplateUri("classpath:LogstashTestLayout.json") .setStackTraceEnabled(true) .setLocationInfoEnabled(true) .setMdcKeyPattern(firstMdcKeyExcludingRegex) .setNdcPattern(firstNdcItemExcludingRegex) .build(); String serializedLogEvent = layout.toSerializable(logEvent); JsonNode rootNode = OBJECT_MAPPER.readTree(serializedLogEvent); checkConstants(rootNode); checkBasicFields(logEvent, rootNode); checkSource(logEvent, rootNode); checkException(logEvent, rootNode); checkContextData(logEvent, firstMdcKeyExcludingRegex, rootNode); checkContextStack(logEvent, firstNdcItemExcludingRegex, rootNode); checkLookupTest(lookupTestKey, lookupTestVal, rootNode); }
@Test public void test_lineSeparator_suffix() { // Create the log event. SimpleMessage message = new SimpleMessage("Hello, World!"); LogEvent logEvent = Log4jLogEvent .newBuilder() .setLoggerName(LogstashLayoutTest.class.getSimpleName()) .setLevel(Level.INFO) .setMessage(message) .build(); // Check line separators. SoftAssertions assertions = new SoftAssertions(); test_lineSeparator_suffix(logEvent, true, assertions); test_lineSeparator_suffix(logEvent, false, assertions); assertions.assertAll(); }
private void test_lineSeparator_suffix(LogEvent logEvent, boolean prettyPrintEnabled, SoftAssertions assertions) { // Create the layout. BuiltConfiguration config = ConfigurationBuilderFactory.newConfigurationBuilder().build(); LogstashLayout layout = LogstashLayout .newBuilder() .setConfiguration(config) .setTemplateUri("classpath:LogstashJsonEventLayoutV1.json") .setPrettyPrintEnabled(prettyPrintEnabled) .build(); // Check the serialized event. String serializedLogEvent = layout.toSerializable(logEvent); String assertionCaption = String.format("testing lineSeperator (prettyPrintEnabled=%s)", prettyPrintEnabled); assertions.assertThat(serializedLogEvent).as(assertionCaption).endsWith("}" + System.lineSeparator()); }
@Override public final String format(LogEvent event) { long eventTimeInMillis = event.getTimeMillis(); // handle "old" events that arrived in separate threads after rollover if (eventTimeInMillis < currentFileTime) { return doFormat(indexName, eventTimeInMillis); } // rollover if (eventTimeInMillis >= nextRolloverTime && rollingOver.compareAndSet(false, true)) { rollover(indexName, eventTimeInMillis); rollingOver.set(false); } // happy path - have to check for pending rollover to avoid race conditions if (!rollingOver.get()) { return currentName; } // fail-safe for pending rollover return doFormat(indexName, eventTimeInMillis); }
@Override public void append(LogEvent event) { List<LogItem> logItems = new ArrayList<LogItem>(); LogItem item = new LogItem(); logItems.add(item); item.SetTime((int) (event.getTimeMillis() / 1000)); item.PushBack("time", this.dateFormat.format(new Date(event.getTimeMillis()))); item.PushBack("level", event.getLevel().toString()); item.PushBack("thread", event.getThreadName()); StackTraceElement source = event.getSource(); if (source == null && (!event.isIncludeLocation())) { event.setIncludeLocation(true); source = event.getSource(); event.setIncludeLocation(false); } item.PushBack("location", source == null ? "Unknown(Unknown Source)" : source.toString()); String message = event.getMessage().getFormattedMessage(); Throwable throwable = event.getThrown(); if (throwable != null) { for (String s : Throwables.toStringList(throwable)) { message += System.getProperty("line.separator") + s; } } item.PushBack("message", message); Map<String, String> properties = event.getContextMap(); if (properties.size() > 0) { Object[] keys = properties.keySet().toArray(); Arrays.sort(keys); for (int i = 0; i < keys.length; i++) { item.PushBack(keys[i].toString(), properties.get(keys[i].toString())); } } producer.send(this.projectName, this.logstore, this.topic, null, logItems, new LoghubAppenderCallback(LOGGER, this.projectName, this.logstore, this.topic, null, logItems)); }
@Override public void append(LogEvent event) { logger.debug("appending: %s", event.getMessage().getFormattedMessage()); if (State.STARTED.equals(state)) { byte[] eventBytes = layout.toByteArray(event); throttler.push(eventBytes); } }
@Override @SuppressWarnings({ "PMD.AvoidCatchingGenericException", // Exception class is unknown "PMD.EmptyCatchBlock", // Exception can neither be logged nor be rethrown "squid:S1166" // Exception cannot be logged or rethrown }) public void append(final LogEvent event) { try { HygeneEventBus.getInstance().post(event); } catch (final RuntimeException e) { // We can't actually log the exception here since that would cause the same problem } }
@BeforeEach void setUp() { consoleMessage = new ConsoleMessage("The message"); logEvent = mock(LogEvent.class); message = mock(Message.class); when(logEvent.getMessage()).thenReturn(message); }
@Override public void append(final LogEvent logEvent) { final LogEvent newLogEvent = LoggingUtils.prepareLogEvent(logEvent); final String refName = this.appenderRef.getRef(); if (StringUtils.isNotBlank(refName)) { final Appender appender = this.config.getAppender(refName); if (appender != null) { appender.append(newLogEvent); } else { LOGGER.warn("No log appender could be found for [{}]", refName); } } else { LOGGER.warn("No log appender reference could be located in logging configuration."); } }
@Test public void appenderUsedFormattedMessageDirectlyWhenMessageOnlyIsSetToTrue() { // given BatchDelivery<String> batchDelivery = mock(BatchDelivery.class); ElasticsearchAppender.Builder builder = ElasticsearchAppenderTest.createTestElasticsearchAppenderBuilder(); builder.withMessageOnly(true); builder.withBatchDelivery(batchDelivery ); String testMessageString = "formattedTestMessage"; Message message = mock(Message.class); when(message.getFormattedMessage()).thenReturn(testMessageString); LogEvent logEvent = mock(LogEvent.class); when(logEvent.getMessage()).thenReturn(message); ElasticsearchAppender appender = builder.build(); // when appender.append(logEvent); // then ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); verify(batchDelivery, times(1)).add(anyString(), captor.capture()); assertEquals(testMessageString, captor.getValue()); }
@Override public void append(LogEvent event) { final String formattedMessage = event.getMessage().getFormattedMessage(); if (event.getLevel() == Level.TRACE && event.getMarker().getName().contains("[index][1] ")) { if (event.getLoggerName().endsWith(".IW") && formattedMessage.contains("IW: apply all deletes during flush")) { sawIndexWriterMessage = true; } if (event.getLoggerName().endsWith(".IFD")) { sawIndexWriterIFDMessage = true; } } }
private LogEvent createTestLogEvent() { return DefaultLogEventFactory.getInstance().createEvent("testLogger", null, getClass().getName(), Level.INFO, new SimpleMessage("testMessage"), null, null); }
@Override public void match(LogEvent event) { if (event.getLevel().equals(level) && event.getLoggerName().equals(logger)) { if (Regex.isSimpleMatchPattern(message)) { if (Regex.simpleMatch(message, event.getMessage().getFormattedMessage())) { saw = true; } } else { if (event.getMessage().toString().contains(message)) { saw = true; } } } }
@Override public void match(LogEvent event) { if (event.getLevel().equals(level) && event.getLoggerName().equals(loggerName) && event.getMessage() instanceof ParameterizedMessage) { final ParameterizedMessage message = (ParameterizedMessage)event.getMessage(); saw = message.getFormat().equals(messagePattern) && Arrays.deepEquals(arguments, message.getParameters()) && throwablePredicate.test(event.getThrown()); } }
/** * Test empty event. */ @Test public void testEmptyEvent(){ JSONLog4j2Layout layout = new JSONLog4j2Layout(false,false,false,false,null,Charset.forName("UTF-8")); LogEvent event = new DummyEmptyLogEvent(); System.out.println(layout.toSerializable(event)); }
/** * Test filled event. */ @Test public void testFilledEvent(){ JSONLog4j2Layout layout = new JSONLog4j2Layout(false,false,false,false,null,Charset.forName("UTF-8")); LogEvent event = new DummyFilledLogEvent(SHORT_STRING); System.out.println(layout.toSerializable(event)); }
/** * Test filled multi line event. */ @Test public void testFilledMultiLineEvent(){ JSONLog4j2Layout layout = new JSONLog4j2Layout(false,false,false,false,null,Charset.forName("UTF-8")); LogEvent event = new DummyFilledLogEvent(LONG_STRING); System.out.println(layout.toSerializable(event)); }
/** * Test filled multi line event. */ @Test public void testFilledMultiLineWithUserFieldsEvent(){ UserField[] fields = new UserField[2]; fields[0] = new UserField("A1","B1"); fields[1] = new UserField("A2","B2"); JSONLog4j2Layout layout = new JSONLog4j2Layout(false,false,false,false,fields,Charset.forName("UTF-8")); LogEvent event = new DummyFilledLogEvent(LONG_STRING); System.out.println(layout.toSerializable(event)); }
private ConcurrentLinkedQueue<TestTuple> generateLogEvents() { ConcurrentLinkedQueue<TestTuple> events = new ConcurrentLinkedQueue<>(); Random random = new Random(); for (int ii = 0; ii < 1000; ii++) { LogEvent logEvent = mock(LogEvent.class); int increment = random.nextInt(3) - 1; when(logEvent.getTimeMillis()).thenReturn(DEFAULT_TEST_TIME_IN_MILLIS + increment * 60000 + random.nextInt(60000)); events.add(new TestTuple(logEvent, increment)); } return events; }
/** * Test filled multi line event. */ @Test public void testFilledMultiLineWithUserFieldsAndContextMapEvent(){ UserField[] fields = new UserField[2]; fields[0] = new UserField("A1","B1"); fields[1] = new UserField("A2","B2"); JSONLog4j2Layout layout = new JSONLog4j2Layout(false,false,false,false,fields,Charset.forName("UTF-8")); LogEvent event = new DummyFilledLogEvent(LONG_STRING); event.getContextMap().put("CT1", "VALUE"); event.getContextMap().put("CT2", "VALUE"); System.out.println(layout.toSerializable(event)); }
public void appendersCalled(LogEvent event) { ThrowableProxy throwableProxy = event.getThrownProxy(); String throwable = (throwableProxy != null) ? throwableProxy.getName() : null; LogEventTracker.LogLevel level = LogEventTracker.LogLevel.valueOf(event.getLevel().toString()); tracker.track(level, (throwableProxy != null), throwable); }
@Override public void toSerializable(final LogEvent event, final Writer writer) throws IOException { if (complete && eventCount > 0) { writer.append(", "); } this.objectWriter.writeValue(writer, convertLog4jEventToExtendedJsonWrapper(event)); writer.write(eol); markEvent(); }
private static LogEvent convertMutableToLog4jEvent(final LogEvent event) { // TODO Jackson-based layouts have certain filters set up for Log4jLogEvent. // TODO Need to set up the same filters for MutableLogEvent but don't know how... // This is a workaround. return event instanceof MutableLogEvent ? ((MutableLogEvent) event).createMemento() : event; }
@SuppressWarnings("deprecation") static void assertEqualLogEvents(final LogEvent expected, final LogEvent actual, final boolean includeSource, final boolean includeContext, final boolean includeStacktrace) { assertEquals(expected.getClass(), actual.getClass()); assertEquals(includeContext ? expected.getContextData() : ContextDataFactory.createContextData(), actual.getContextData()); assertEquals(includeContext ? expected.getContextMap() : Collections.EMPTY_MAP, actual.getContextMap()); assertEquals(expected.getContextStack(), actual.getContextStack()); assertEquals(expected.getLevel(), actual.getLevel()); assertEquals(expected.getLoggerName(), actual.getLoggerName()); assertEquals(expected.getLoggerFqcn(), actual.getLoggerFqcn()); assertEquals(expected.getMarker(), actual.getMarker()); assertEquals(expected.getMessage(), actual.getMessage()); assertEquals(expected.getTimeMillis(), actual.getTimeMillis()); assertEquals(includeSource ? expected.getSource() : null, actual.getSource()); assertEquals(expected.getThreadName(), actual.getThreadName()); assertNotNull("original should have an exception", expected.getThrown()); assertNull("exception should not be serialized", actual.getThrown()); if (includeStacktrace) { // TODO should compare the rest of the ThrowableProxy assertEquals(expected.getThrownProxy(), actual.getThrownProxy()); } assertEquals(expected.isEndOfBatch(), actual.isEndOfBatch()); assertEquals(expected.isIncludeLocation(), actual.isIncludeLocation()); // original: non-null thrown & null thrownProxy // deserialized: null thrown & non-null thrownProxy assertNotEquals(expected.hashCode(), actual.hashCode()); assertNotEquals(expected, actual); }
/** * This method is where the appender does the work. * * @param event Log event with log data */ @Override public void append(LogEvent event) { readLock.lock(); final String message = new String(getLayout().toByteArray(event)); // append log text to TextArea try { MiscUtil.runLaterIfNeeded(() -> { try { if (textArea != null) { if (textArea.getText().length() == 0) { textArea.setText(message); } else { textArea.selectEnd(); textArea.insertText(textArea.getText().length(), message); } } } catch (final Throwable t) { OneClientLogging.logger.error("Error while append to TextArea: " + t.getMessage()); } }); } catch (final IllegalStateException ex) { ex.printStackTrace(); } finally { readLock.unlock(); } }
public void assertLogMessage(Level level, String messagePart) { for (LogEvent event : filterByLevel(logEvents, level)) { if (event.getMessage().getFormattedMessage().contains(messagePart)) { return; } } Assert.fail(String.format( "Couldn't find any message with level %s that contains '%s' in%n%s", level, messagePart, logEvents)); }
public void assertException(Level level, Class<?> exceptionType, String messagePart) { for (LogEvent event : filterByLevel(logEvents, level)) { if (exceptionType.isInstance(event.getThrown()) && event.getThrown().getMessage().contains(messagePart)) { return; } } Assert.fail(String.format( "Couldn't find any log event with level %s that contains a %s with message containing '%s' in%n%s", level, exceptionType.getSimpleName(), messagePart, logEvents)); }
@Test public void returnsCurrentTimeIfEventTimeIsBeforeRolloverTime() { // given LogEvent logEvent = mock(LogEvent.class); when(logEvent.getTimeMillis()).thenReturn(DEFAULT_TEST_TIME_IN_MILLIS); IndexNameFormatter formatter = createRollingIndexNameFormatterBuilder().build(); // when String formattedIndexName = formatter.format(logEvent); // then Assert.assertEquals("testIndexName-2017-12-20-23.54", formattedIndexName); }