Java Code Examples for org.apache.log4j.spi.LoggingEvent#getNDC()

The following examples show how to use org.apache.log4j.spi.LoggingEvent#getNDC() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PatternParser.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
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;
     }
   }
 
Example 2
Source File: SMTPAppender.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
   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();
  }
  cb.add(event);
  if(evaluator.isTriggeringEvent(event)) {
    sendBuffer();
  }
}
 
Example 3
Source File: JDBCLog.java    From MultimediaDesktop with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 4
Source File: TTCCLayout.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
  In addition to the level of the statement and message, the
  returned byte array includes time, thread, category and {@link NDC}
  information.

  <p>Time, thread, category and diagnostic context are printed
  depending on options.

   @param event The event to format

 */
 public
 String format(LoggingEvent event) {

   // Reset buf
   buf.setLength(0);

   dateFormat(buf, event);

   if(this.threadPrinting) {
     buf.append('[');
     buf.append(event.getThreadName());
     buf.append("] ");
   }
   buf.append(event.getLevel().toString());
   buf.append(' ');

   if(this.categoryPrefixing) {
     buf.append(event.getLoggerName());
     buf.append(' ');
   }

   if(this.contextPrinting) {
      String ndc = event.getNDC();

     if(ndc != null) {
buf.append(ndc);
buf.append(' ');
     }
   }
   buf.append("- ");
   buf.append(event.getRenderedMessage());
   buf.append(LINE_SEP);
   return buf.toString();
 }
 
Example 5
Source File: EventDetails.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * 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);
}
 
Example 6
Source File: Log4JAppenderWrapper.java    From mrgeo with Apache License 2.0 5 votes vote down vote up
@Override
public void doAppend(LoggingEvent event)
{
  assert (wrappedAppender != null);


  String msg = event.getMessage().toString();

  Matcher m = crlf.matcher(msg);
  if (m.find())
  {
    String lines[] = msg.split("\r\n|\n|\r"); //msg.split("\\r?\\n");

    for (String line : lines)
    {
      String clean = "(Encoded) " + line;

      LoggingEvent encoded = new LoggingEvent(event.getFQNOfLoggerClass(),
          event.getLogger(), event.getTimeStamp(), event.getLevel(), clean,
          event.getThreadName(), event.getThrowableInformation(), event.getNDC(),
          event.getLocationInformation(), event.getProperties());

      wrappedAppender.doAppend(encoded);
    }
  }
  else
  {
    wrappedAppender.doAppend(event);
  }

}
 
Example 7
Source File: HTMLLayout.java    From document-management-software with GNU Lesser General Public License v3.0 4 votes vote down vote up
public String format(LoggingEvent event) {

		if (sbuf.capacity() > MAX_CAPACITY) {
			sbuf = new StringBuffer(BUF_SIZE);
		} else {
			sbuf.setLength(0);
		}

		sbuf.append(Layout.LINE_SEP + "<tr>" + Layout.LINE_SEP);

		sbuf.append("<td>");
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
		sbuf.append(df.format(new Date()));
		sbuf.append("</td>" + Layout.LINE_SEP);

		/*
		 * String escapedThread = Transform.escapeTags(event.getThreadName());
		 * sbuf.append("<td title=\"" + escapedThread + " thread\">");
		 * sbuf.append(escapedThread); sbuf.append("</td>" + Layout.LINE_SEP);
		 */

		sbuf.append("<td title=\"Level\">");
		if (event.getLevel().equals(Level.DEBUG)) {
			sbuf.append("<font color=\"#339933\">");
			sbuf.append(Transform.escapeTags(String.valueOf(event.getLevel())));
			sbuf.append("</font>");
		} else if (event.getLevel().isGreaterOrEqual(Level.WARN)) {
			sbuf.append("<font color=\"#993300\"><strong>");
			sbuf.append(Transform.escapeTags(String.valueOf(event.getLevel())));
			sbuf.append("</strong></font>");
		} else {
			sbuf.append(Transform.escapeTags(String.valueOf(event.getLevel())));
		}
		sbuf.append("</td>" + Layout.LINE_SEP);

		String escapedLogger = Transform.escapeTags(event.getLoggerName());
		sbuf.append("<td title=\"" + escapedLogger + " category\">");
		sbuf.append(escapedLogger);
		sbuf.append("</td>" + Layout.LINE_SEP);

		if (locationInfo) {
			LocationInfo locInfo = event.getLocationInformation();
			sbuf.append("<td>");
			sbuf.append(Transform.escapeTags(locInfo.getFileName()));
			sbuf.append(':');
			sbuf.append(locInfo.getLineNumber());
			sbuf.append("</td>" + Layout.LINE_SEP);
		}

		sbuf.append("<td title=\"Message\">");

		if (event != null && event.getRenderedMessage() != null)
			sbuf.append(Transform.escapeTags(event.getRenderedMessage()).replace("\n", "<br/>"));
		sbuf.append("</td>" + Layout.LINE_SEP);
		sbuf.append("</tr>" + Layout.LINE_SEP);

		if (event.getNDC() != null) {
			sbuf.append(
					"<tr><td bgcolor=\"#EEEEEE\" style=\"font-size : xx-small;\" colspan=\"6\" title=\"Nested Diagnostic Context\">");
			sbuf.append("NDC: " + Transform.escapeTags(event.getNDC()));
			sbuf.append("</td></tr>" + Layout.LINE_SEP);
		}

		String[] s = event.getThrowableStrRep();
		if (s != null) {
			/*
			 * sbuf.append(
			 * "<tr><td bgcolor=\"#993300\" style=\"color:White; font-size : xx-small;\" colspan=\"6\">"
			 * );
			 */
			sbuf.append("<tr><td bgcolor=\"#993300\" style=\"color:White; font-size : xx-small;\" colspan=\"5\">");
			appendThrowableAsHTML(s, sbuf);
			sbuf.append("</td></tr>" + Layout.LINE_SEP);
		}

		return sbuf.toString();
	}
 
Example 8
Source File: AsyncAppender.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public void append(final LoggingEvent event) {
  //
  //   if dispatcher thread has died then
  //      append subsequent events synchronously
  //   See bug 23021
  if ((dispatcher == null) || !dispatcher.isAlive() || (bufferSize <= 0)) {
    synchronized (appenders) {
      appenders.appendLoopOnAppenders(event);
    }

    return;
  }

  // Set the NDC and thread name for the calling thread as these
  // LoggingEvent fields were not set at event creation time.
  event.getNDC();
  event.getThreadName();
  // Get a copy of this thread's MDC.
  event.getMDCCopy();
  if (locationInfo) {
    event.getLocationInformation();
  }

  synchronized (buffer) {
    while (true) {
      int previousSize = buffer.size();

      if (previousSize < bufferSize) {
        buffer.add(event);

        //
        //   if buffer had been empty
        //       signal all threads waiting on buffer
        //       to check their conditions.
        //
        if (previousSize == 0) {
          buffer.notifyAll();
        }

        break;
      }

      //
      //   Following code is only reachable if buffer is full
      //
      //
      //   if blocking and thread is not already interrupted
      //      and not the dispatcher then
      //      wait for a buffer notification
      boolean discard = true;
      if (blocking
              && !Thread.interrupted()
              && Thread.currentThread() != dispatcher) {
        try {
          buffer.wait();
          discard = false;
        } catch (InterruptedException e) {
          //
          //  reset interrupt status so
          //    calling code can see interrupt on
          //    their next wait or sleep.
          Thread.currentThread().interrupt();
        }
      }

      //
      //   if blocking is false or thread has been interrupted
      //   add event to discard map.
      //
      if (discard) {
        String loggerName = event.getLoggerName();
        DiscardSummary summary = (DiscardSummary) discardMap.get(loggerName);

        if (summary == null) {
          summary = new DiscardSummary(event);
          discardMap.put(loggerName, summary);
        } else {
          summary.add(event);
        }

        break;
      }
    }
  }
}
 
Example 9
Source File: LF5Appender.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Appends a <code>LoggingEvent</code> record to the
 * <code>LF5Appender</code>.
 * @param event The <code>LoggingEvent</code>
 * to be appended.
 */
public void append(LoggingEvent event) {
  // Retrieve the information from the log4j LoggingEvent.
  String category = event.getLoggerName();
  String logMessage = event.getRenderedMessage();
  String nestedDiagnosticContext = event.getNDC();
  String threadDescription = event.getThreadName();
  String level = event.getLevel().toString();
  long time = event.timeStamp;
  LocationInfo locationInfo = event.getLocationInformation();

  // Add the logging event information to a LogRecord
  Log4JLogRecord record = new Log4JLogRecord();

  record.setCategory(category);
  record.setMessage(logMessage);
  record.setLocation(locationInfo.fullInfo);
  record.setMillis(time);
  record.setThreadDescription(threadDescription);

  if (nestedDiagnosticContext != null) {
    record.setNDC(nestedDiagnosticContext);
  } else {
    record.setNDC("");
  }

  if (event.getThrowableInformation() != null) {
    record.setThrownStackTrace(event.getThrowableInformation());
  }

  try {
    record.setLevel(LogLevel.valueOf(level));
  } catch (LogLevelFormatException e) {
    // If the priority level doesn't match one of the predefined
    // log levels, then set the level to warning.
    record.setLevel(LogLevel.WARN);
  }

  if (_logMonitor != null) {
    _logMonitor.addMessage(record);
  }
}
 
Example 10
Source File: HTMLLayout.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public
String format(LoggingEvent event) {

  if(sbuf.capacity() > MAX_CAPACITY) {
    sbuf = new StringBuffer(BUF_SIZE);
  } else {
    sbuf.setLength(0);
  }

  sbuf.append(Layout.LINE_SEP + "<tr>" + Layout.LINE_SEP);

  sbuf.append("<td>");
  sbuf.append(event.timeStamp - LoggingEvent.getStartTime());
  sbuf.append("</td>" + Layout.LINE_SEP);

  String escapedThread = Transform.escapeTags(event.getThreadName());
  sbuf.append("<td title=\"" + escapedThread + " thread\">");
  sbuf.append(escapedThread);
  sbuf.append("</td>" + Layout.LINE_SEP);

  sbuf.append("<td title=\"Level\">");
  if (event.getLevel().equals(Level.DEBUG)) {
    sbuf.append("<font color=\"#339933\">");
    sbuf.append(Transform.escapeTags(String.valueOf(event.getLevel())));
    sbuf.append("</font>");
  }
  else if(event.getLevel().isGreaterOrEqual(Level.WARN)) {
    sbuf.append("<font color=\"#993300\"><strong>");
    sbuf.append(Transform.escapeTags(String.valueOf(event.getLevel())));
    sbuf.append("</strong></font>");
  } else {
    sbuf.append(Transform.escapeTags(String.valueOf(event.getLevel())));
  }
  sbuf.append("</td>" + Layout.LINE_SEP);

  String escapedLogger = Transform.escapeTags(event.getLoggerName());
  sbuf.append("<td title=\"" + escapedLogger + " category\">");
  sbuf.append(escapedLogger);
  sbuf.append("</td>" + Layout.LINE_SEP);

  if(locationInfo) {
    LocationInfo locInfo = event.getLocationInformation();
    sbuf.append("<td>");
    sbuf.append(Transform.escapeTags(locInfo.getFileName()));
    sbuf.append(':');
    sbuf.append(locInfo.getLineNumber());
    sbuf.append("</td>" + Layout.LINE_SEP);
  }

  sbuf.append("<td title=\"Message\">");
  sbuf.append(Transform.escapeTags(event.getRenderedMessage()));
  sbuf.append("</td>" + Layout.LINE_SEP);
  sbuf.append("</tr>" + Layout.LINE_SEP);

  if (event.getNDC() != null) {
    sbuf.append("<tr><td bgcolor=\"#EEEEEE\" style=\"font-size : xx-small;\" colspan=\"6\" title=\"Nested Diagnostic Context\">");
    sbuf.append("NDC: " + Transform.escapeTags(event.getNDC()));
    sbuf.append("</td></tr>" + Layout.LINE_SEP);
  }

  String[] s = event.getThrowableStrRep();
  if(s != null) {
    sbuf.append("<tr><td bgcolor=\"#993300\" style=\"color:White; font-size : xx-small;\" colspan=\"6\">");
    appendThrowableAsHTML(s, sbuf);
    sbuf.append("</td></tr>" + Layout.LINE_SEP);
  }

  return sbuf.toString();
}
 
Example 11
Source File: StreamAppender.java    From samza with Apache License 2.0 4 votes vote down vote up
private LoggingEvent subLog(LoggingEvent event) {
  return new LoggingEvent(event.getFQNOfLoggerClass(), event.getLogger(), event.getTimeStamp(),
      event.getLevel(), subAppend(event), event.getThreadName(), event.getThrowableInformation(),
      event.getNDC(), event.getLocationInformation(), event.getProperties());
}
 
Example 12
Source File: LoggingEventJsonSerde.java    From samza with Apache License 2.0 4 votes vote down vote up
/**
 * Encodes a LoggingEvent into a HashMap using the logstash JSON format.
 * 
 * @param loggingEvent
 *          The LoggingEvent to encode.
 * @param includeLocationInfo
 *          Whether to include LocationInfo in the map, or not.
 * @return A Map representing the LoggingEvent, which is suitable to be
 *         serialized by a JSON encoder such as Jackson.
 */
@SuppressWarnings("rawtypes")
public static Map<String, Object> encodeToMap(LoggingEvent loggingEvent, boolean includeLocationInfo) {
  Map<String, Object> logstashEvent = new LoggingEventMap();
  String threadName = loggingEvent.getThreadName();
  long timestamp = loggingEvent.getTimeStamp();
  HashMap<String, Object> exceptionInformation = new HashMap<String, Object>();
  Map mdc = loggingEvent.getProperties();
  String ndc = loggingEvent.getNDC();

  logstashEvent.put("@version", VERSION);
  logstashEvent.put("@timestamp", dateFormat(timestamp));
  logstashEvent.put("source_host", getHostname());
  logstashEvent.put("message", loggingEvent.getRenderedMessage());

  if (loggingEvent.getThrowableInformation() != null) {
    final ThrowableInformation throwableInformation = loggingEvent.getThrowableInformation();
    if (throwableInformation.getThrowable().getClass().getCanonicalName() != null) {
      exceptionInformation.put("exception_class", throwableInformation.getThrowable().getClass().getCanonicalName());
    }
    if (throwableInformation.getThrowable().getMessage() != null) {
      exceptionInformation.put("exception_message", throwableInformation.getThrowable().getMessage());
    }
    if (throwableInformation.getThrowableStrRep() != null) {
      StringBuilder stackTrace = new StringBuilder();
      for (String line : throwableInformation.getThrowableStrRep()) {
        stackTrace.append(line);
        stackTrace.append("\n");
      }
      exceptionInformation.put("stacktrace", stackTrace);
    }
    logstashEvent.put("exception", exceptionInformation);
  }

  if (includeLocationInfo) {
    LocationInfo info = loggingEvent.getLocationInformation();
    logstashEvent.put("file", info.getFileName());
    logstashEvent.put("line_number", info.getLineNumber());
    logstashEvent.put("class", info.getClassName());
    logstashEvent.put("method", info.getMethodName());
  }

  logstashEvent.put("logger_name", loggingEvent.getLoggerName());
  logstashEvent.put("mdc", mdc);
  logstashEvent.put("ndc", ndc);
  logstashEvent.put("level", loggingEvent.getLevel().toString());
  logstashEvent.put("thread_name", threadName);

  return logstashEvent;
}
 
Example 13
Source File: PurgeUtilityHTMLLayout.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
public String format( LoggingEvent event ) {

    Level logLevel = event.getLevel();
    if ( sbuf.capacity() > MAX_CAPACITY ) {
      sbuf = new StringBuffer( BUF_SIZE );
    } else {
      sbuf.setLength( 0 );
    }

    if ( showTimeColumn() ) {
      DateFormat df = new SimpleDateFormat( "MM/dd/yyyy HH:mm:ss" );
      Date date = new Date();
      date.setTime( event.timeStamp );
      String time = null;
      try {
        time = df.format( date );
      } catch ( Exception ex ) {
        LogLog.error( "Error occured while converting date.", ex );
      }

      sbuf.append( Layout.LINE_SEP + "<tr>" + Layout.LINE_SEP );

      sbuf.append( "<td>" );
      sbuf.append( Transform.escapeTags( time ) );
      sbuf.append( "</td>" + Layout.LINE_SEP );
    }

    sbuf.append( "<td title=\"Purge File/Folder\">" );
    sbuf.append( Transform.escapeTags( MDC.get( PurgeUtilityLog.FILE_KEY ) ) );
    sbuf.append( "</td>" + Layout.LINE_SEP );

    if ( showLevelColumn() ) {
      sbuf.append( "<td title=\"Level\">" );
      if ( logLevel.equals( Level.DEBUG ) ) {
        sbuf.append( "<font color=\"#339933\">" );
        sbuf.append( Transform.escapeTags( String.valueOf( event.getLevel() ) ) );
        sbuf.append( "</font>" );
      } else if ( logLevel.isGreaterOrEqual( Level.WARN ) ) {
        sbuf.append( "<font color=\"#993300\"><strong>" );
        sbuf.append( Transform.escapeTags( String.valueOf( event.getLevel() ) ) );
        sbuf.append( "</strong></font>" );
      } else {
        sbuf.append( Transform.escapeTags( String.valueOf( event.getLevel() ) ) );
      }
      sbuf.append( "</td>" + Layout.LINE_SEP );
    }

    if ( showCodeLineColumn() ) {
      LocationInfo locInfo = event.getLocationInformation();
      sbuf.append( "<td>" );
      sbuf.append( Transform.escapeTags( MDC.get( PurgeUtilityLogger.CODE_LINE ) ) );
      // sbuf.append( Transform.escapeTags( locInfo.getFileName() ) );
      // sbuf.append( ':' );
      // sbuf.append( locInfo.getLineNumber() );
      sbuf.append( "</td>" + Layout.LINE_SEP );
    }

    sbuf.append( "<td title=\"Message\">" );
    sbuf.append( Transform.escapeTags( event.getRenderedMessage() ) );
    sbuf.append( "</td>" + Layout.LINE_SEP );
    sbuf.append( "</tr>" + Layout.LINE_SEP );

    if ( event.getNDC() != null ) {
      sbuf.append( "<tr><td bgcolor=\"#EEEEEE\" style=\"font-size : "
          + "xx-small;\" colspan=\"6\" title=\"Nested Diagnostic Context\">" );
      sbuf.append( "NDC: " + Transform.escapeTags( event.getNDC() ) );
      sbuf.append( "</td></tr>" + Layout.LINE_SEP );
    }

    String[] s = event.getThrowableStrRep();
    if ( s != null ) {
      sbuf.append( "<tr><td bgcolor=\"#993300\" style=\"color:White; font-size : xx-small;\" colspan=\"6\">" );
      appendThrowableAsHTML( s, sbuf );
      sbuf.append( "</td></tr>" + Layout.LINE_SEP );
    }

    return sbuf.toString();
  }