@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(); } }
@Test public void launchCreatesAndStartsAppenderAndProcessesMessages() throws Exception { final MessageInput messageInput = mock(MessageInput.class); transport.launch(messageInput); final DirectConsumingAppender appender = transport.getAppender(); assertThat(appender.getName()).isEqualTo("graylog-plugin-internal-logs"); assertThat(appender.getLayout()).isInstanceOf(SerializedLayout.class); assertThat(appender.isStarted()).isTrue(); final MutableLogEvent logEvent = new MutableLogEvent(); logEvent.setMessage(new SimpleMessage("Processed")); logEvent.setLevel(Level.ERROR); appender.append(logEvent); verify(messageInput, times(1)).processRawMessage(any(RawMessage.class)); final MutableLogEvent ignoredLogEvent = new MutableLogEvent(); ignoredLogEvent.setMessage(new SimpleMessage("Ignored")); ignoredLogEvent.setLevel(Level.TRACE); appender.append(ignoredLogEvent); verifyNoMoreInteractions(messageInput); }
private LogEvent createLogEvent(DateTime timestamp) { final MutableLogEvent logEvent = new MutableLogEvent(); logEvent.setMessage(new SimpleMessage("Test")); logEvent.setLevel(Level.TRACE); logEvent.setLoggerName("org.example.Test"); logEvent.setMarker(MarkerManager.getMarker("TestMarker")); logEvent.setTimeMillis(timestamp.getMillis()); logEvent.setNanoTime(42L); logEvent.setContextStack(new MutableThreadContextStack(ImmutableList.of("one", "two"))); final SortedArrayStringMap contextData = new SortedArrayStringMap(1); contextData.putValue("foobar", "quux"); logEvent.setContextData(contextData); logEvent.setThreadId(23L); logEvent.setThreadName("thread-name"); logEvent.setThreadPriority(42); logEvent.setThrown(new Throwable("Test", new Throwable("cause"))); logEvent.setIncludeLocation(true); return logEvent.createMemento(); }
private LogEvent prepareEvent(final LogEvent event) { LogEvent logEvent = ensureImmutable(event); if (logEvent.getMessage() instanceof ReusableMessage) { if (logEvent instanceof Log4jLogEvent) { ((Log4jLogEvent) logEvent).makeMessageImmutable(); } else if (logEvent instanceof MutableLogEvent) { // MutableLogEvents need to be translated into the RingBuffer by the MUTABLE_TRANSLATOR. // That translator calls MutableLogEvent.initFrom to copy the event, which will makeMessageImmutable the message. if (translator != MUTABLE_TRANSLATOR) { // should not happen... // TRANSLATOR expects an immutable LogEvent logEvent = ((MutableLogEvent) logEvent).createMemento(); } } else { // custom log event, with a ReusableMessage showWarningAboutCustomLogEventWithReusableMessage(logEvent); } } else { // message is not a ReusableMessage; makeMessageImmutable it to prevent ConcurrentModificationExceptions InternalAsyncUtil.makeMessageImmutable(logEvent.getMessage()); // LOG4J2-1988, LOG4J2-1914 } return logEvent; }
@Test public void testInteractionWithReusableParameterizedMessage() { final MutableLogEvent evt = new MutableLogEvent(); final ReusableParameterizedMessage msg = new ReusableParameterizedMessage(); msg.set("Hello {} {} {}", 1, 2, 3); evt.setMessage(msg); evt.clear(); msg.set("Hello {}", new Object[]{1}); evt.setMessage(msg); evt.clear(); msg.set("Hello {}", 1); evt.setMessage(msg); evt.clear(); // Uncomment out this log event and the params gets reset correctly (No exception occurs) // msg.set("Hello {}", 1); // evt.setMessage(msg); // evt.clear(); // Exception at this log event - as the params is set to 1! msg.set("Hello {} {} {}", 1, 2, 3); evt.setMessage(msg); evt.clear(); }
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; }
@Test public void appenderSendsLogEventToConsumerIfThresholdMatches() throws Exception { final MutableLogEvent logEvent = new MutableLogEvent(); logEvent.setMessage(new SimpleMessage("Test")); logEvent.setLevel(Level.ERROR); appender.append(logEvent); assertThat(consumer.getProcessedLogEvents()).hasSize(1); }
@Test public void appenderIgnoresLogEventIfThresholdMismatches() throws Exception { final MutableLogEvent logEvent = new MutableLogEvent(); logEvent.setMessage(new SimpleMessage("Test")); logEvent.setLevel(Level.TRACE); appender.append(logEvent); assertThat(consumer.getProcessedLogEvents()).isEmpty(); }
public void add(LogEvent event) { if (event instanceof Log4jLogEvent && event.getMessage() instanceof ReusableMessage) { ((Log4jLogEvent) event).makeMessageImmutable(); } else if (event instanceof MutableLogEvent) { event = ((MutableLogEvent) event).createMemento(); } buffer.add(event); }
/** * Release references held by ring buffer to allow objects to be garbage-collected. */ public void clear() { loggerConfig = null; if (event instanceof MutableLogEvent) { ((MutableLogEvent) event).clear(); } else { event = null; } }
private static LogEvent convertMutableToLog4jEvent(final LogEvent event) { // TODO (from JsonLayout): Need to set up the same filters for MutableLogEvent but don't know how... return ((event instanceof MutableLogEvent) ? ((MutableLogEvent) event).createMemento() : event); }
public void add(LogEvent event) { // We need to make a copy as instances of LogEvent are reused by log4j MutableLogEvent copy = new MutableLogEvent(); copy.initFrom(event); messages.add(copy); }
public Log4jEventWrapper(final MutableLogEvent mutableLogEvent) { event = mutableLogEvent; }
@Override public Log4jEventWrapper newInstance() { return new Log4jEventWrapper(new MutableLogEvent()); }
@Override public void translateTo(final Log4jEventWrapper ringBufferElement, final long sequence, final LogEvent logEvent, final AsyncLoggerConfig loggerConfig) { ((MutableLogEvent) ringBufferElement.event).initFrom(logEvent); ringBufferElement.loggerConfig = loggerConfig; }