/** Return the message for this logging event. <p>Before serialization, the returned object is the message passed by the user to generate the logging event. After serialization, the returned value equals the String form of the message possibly after object rendering. @since 1.1 */ public Object getMessage() { if(message != null) { return message; } else { return getRenderedMessage(); } }
/** * {@inheritDoc} */ public void format(final LoggingEvent event, final StringBuffer toAppendTo) { toAppendTo.append(event.getRenderedMessage()); } }
/** Returns {@link Filter#NEUTRAL} is there is no string match. */ public int decide(LoggingEvent event) { String msg = event.getRenderedMessage(); if(msg == null || stringToMatch == null) return Filter.NEUTRAL; if( msg.indexOf(stringToMatch) == -1 ) { return Filter.NEUTRAL; } else { // we've got a match if(acceptOnMatch) { return Filter.ACCEPT; } else { return Filter.DENY; } } } }
/** Returns the log statement in a format consisting of the <code>level</code>, followed by " - " and then the <code>message</code>. For example, <pre> INFO - "A message" </pre> <p>The <code>category</code> parameter is ignored. <p> @return A byte array in SimpleLayout format. */ public String format(LoggingEvent event) { sbuf.setLength(0); sbuf.append(event.getLevel().toString()); sbuf.append(" - "); sbuf.append(event.getRenderedMessage()); sbuf.append(LINE_SEP); return sbuf.toString(); }
@Override public boolean matches(LoggingEvent argument) { return argument.getRenderedMessage().contains("Regionserver not serving any regions"); } }));
@Override public boolean matches(LoggingEvent argument) { return argument.getRenderedMessage().contains("The configured write timeout was"); } }));
@Override public boolean matches(LoggingEvent argument) { return argument.getRenderedMessage().contains("The configured read timeout was"); } }));
@Override public boolean matches(LoggingEvent argument) { return ((LoggingEvent) argument).getRenderedMessage().contains("exceeded the configured read timeout."); } }));
@Override public boolean matches(LoggingEvent argument) { return argument.getRenderedMessage().contains("Regionserver not serving any regions"); } }));
/** * Determines whether a message with the given pattern was logged. */ public synchronized boolean wasLogged(Pattern pattern) { for (LoggingEvent e : mEvents) { if (pattern.matcher(e.getRenderedMessage()).matches()) { return true; } } return false; }
/** * Counts the number of log message with a given pattern. */ public synchronized int logCount(Pattern pattern) { int logCount = 0; for (LoggingEvent e: mEvents) { if (pattern.matcher(e.getRenderedMessage()).matches()) { logCount++; } } return logCount; }
buf.append(event.getRenderedMessage()); buf.append(LINE_SEP); return buf.toString();
public String convert(LoggingEvent event) { switch(type) { case RELATIVE_TIME_CONVERTER: return (Long.toString(event.timeStamp - LoggingEvent.getStartTime())); case THREAD_CONVERTER: return event.getThreadName(); case LEVEL_CONVERTER: return event.getLevel().toString(); case NDC_CONVERTER: return event.getNDC(); case MESSAGE_CONVERTER: { return event.getRenderedMessage(); } default: return null; } } }
/** * Append a log event at the appropriate JUL level, depending on the log4j level. */ @Override protected void append(LoggingEvent loggingEvent) { java.util.logging.Logger logger = java.util.logging.Logger.getLogger(loggingEvent.getLoggerName()); if (logger == null) { LogLog.warn(format("Cannot obtain JUL %s. Verify that this appender is used while an appropriate LogManager is active.", loggingEvent.getLoggerName())); return; } Level level = loggingEvent.getLevel(); java.util.logging.Level julLevel = convertLog4jLevel(level); LogRecord record = new LogRecord(julLevel, loggingEvent.getRenderedMessage()); record.setMillis(loggingEvent.getTimeStamp()); LocationInfo location = loggingEvent.getLocationInformation(); if (location != null) { record.setSourceClassName(location.getClassName()); record.setSourceMethodName(location.getMethodName()); } logger.log(record); }
/** * Adds the event to the buffer. When full the buffer is flushed. */ public void append(LoggingEvent event) { event.getNDC(); event.getThreadName(); // Get a copy of this thread's MDC. event.getMDCCopy(); if (locationInfo) { event.getLocationInformation(); } event.getRenderedMessage(); event.getThrowableStrRep(); buffer.add(event); if (buffer.size() >= bufferSize) flushBuffer(); }
private void writeObject(ObjectOutputStream oos) throws java.io.IOException { // Aside from returning the current thread name the wgetThreadName // method sets the threadName variable. this.getThreadName(); // This sets the renders the message in case it wasn't up to now. this.getRenderedMessage(); // This call has a side effect of setting this.ndc and // setting ndcLookupRequired to false if not already false. this.getNDC(); // This call has a side effect of setting this.mdcCopy and // setting mdcLookupRequired to false if not already false. this.getMDCCopy(); // This sets the throwable sting representation of the event throwable. this.getThrowableStrRep(); oos.defaultWriteObject(); // serialize this event's level writeLevel(oos); }
/** * Creates a new <code>EventDetails</code> instance. * * @param aEvent a <code>LoggingEvent</code> value */ EventDetails(LoggingEvent aEvent) { this(aEvent.timeStamp, aEvent.getLevel(), aEvent.getLoggerName(), aEvent.getNDC(), aEvent.getThreadName(), aEvent.getRenderedMessage(), aEvent.getThrowableStrRep(), (aEvent.getLocationInformation() == null) ? null : aEvent.getLocationInformation().fullInfo); }
@Test public void testKafkaReporter() { TestAppender testAppender = new TestAppender(); Logger logger = LogManager.getLogger(LoggingPusher.class.getName()); logger.addAppender(testAppender); LoggingPusher<String> loggingPusher = new LoggingPusher<String>("broker", "topic", Optional.absent()); loggingPusher.pushMessages(ImmutableList.of("message1", "message2")); Assert.assertEquals(testAppender.events.size(), 2); Assert.assertEquals(testAppender.events.get(0).getRenderedMessage(), "Pushing to broker:topic: message1"); Assert.assertEquals(testAppender.events.get(1).getRenderedMessage(), "Pushing to broker:topic: message2"); logger.removeAppender(testAppender); }
@Test public void testAddToFailedServersLogging() { Throwable nullException = new NullPointerException(); FailedServers fs = new FailedServers(new Configuration()); addr = new InetSocketAddress(TEST_PORT); fs.addToFailedServers(addr, nullException); Mockito.verify(mockAppender).doAppend((LoggingEvent) captorLoggingEvent.capture()); LoggingEvent loggingEvent = (LoggingEvent) captorLoggingEvent.getValue(); assertThat(loggingEvent.getLevel(), is(Level.DEBUG)); assertEquals("Added failed server with address " + addr.toString() + " to list caused by " + nullException.toString(), loggingEvent.getRenderedMessage()); }
/** Perform SMTPAppender specific appending actions, mainly adding the event to a cyclic buffer and checking if the event triggers an e-mail to be sent. */ public void append(LoggingEvent event) { if(!checkEntryConditions()) { return; } event.getThreadName(); event.getNDC(); event.getMDCCopy(); if(locationInfo) { event.getLocationInformation(); } event.getRenderedMessage(); event.getThrowableStrRep(); cb.add(event); if(evaluator.isTriggeringEvent(event)) { sendBuffer(); } }