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

The following examples show how to use org.apache.log4j.spi.LoggingEvent#getTimeStamp() . 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: BenderLayout.java    From bender with Apache License 2.0 5 votes vote down vote up
@Override
public String format(LoggingEvent event) {
  BenderLogEntry entry = new BenderLogEntry();
  entry.threadName = event.getThreadName();
  entry.posixTimestamp = event.getTimeStamp();
  entry.timestamp = FORMATTER.print(entry.posixTimestamp);
  entry.message = event.getRenderedMessage();
  entry.level = event.getLevel().toString();
  entry.logger = event.getLogger().getName();
  entry.alias = ALIAS;
  entry.version = VERSION;

  if (event.getThrowableInformation() != null) {
    final ThrowableInformation throwableInfo = event.getThrowableInformation();
    ExceptionLog ex = new ExceptionLog();

    if (throwableInfo.getThrowable().getClass().getCanonicalName() != null) {
      ex.clazz = throwableInfo.getThrowable().getClass().getCanonicalName();
    }
    if (throwableInfo.getThrowable().getMessage() != null) {
      ex.message = throwableInfo.getThrowable().getMessage();
    }
    if (throwableInfo.getThrowableStrRep() != null) {
      Arrays.asList(throwableInfo.getThrowableStrRep()).forEach(m -> {
        ex.stacktrace.add(m.replaceAll("\\t", "   "));
      });
    }
    entry.exception = ex;
  }

  LocationInfo locinfo = event.getLocationInformation();
  entry.file = locinfo.getFileName();
  entry.lineNumber = Integer.parseInt(locinfo.getLineNumber());
  entry.method = locinfo.getMethodName();
  entry.clazz = locinfo.getClassName();

  return GSON.toJson(entry) + "\n";
}
 
Example 2
Source File: LoggingTester.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected void append(final LoggingEvent event) {
  String _renderedMessage = event.getRenderedMessage();
  String _loggerName = event.getLoggerName();
  long _timeStamp = event.getTimeStamp();
  Level _level = event.getLevel();
  final LoggingTester.LogEntry entry = new LoggingTester.LogEntry(_renderedMessage, _loggerName, _timeStamp, _level);
  this.events.add(entry);
}
 
Example 3
Source File: LoggingTester.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected void append(final LoggingEvent event) {
  String _renderedMessage = event.getRenderedMessage();
  String _loggerName = event.getLoggerName();
  long _timeStamp = event.getTimeStamp();
  Level _level = event.getLevel();
  final LoggingTester.LogEntry entry = new LoggingTester.LogEntry(_renderedMessage, _loggerName, _timeStamp, _level);
  this.events.add(entry);
}
 
Example 4
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 5
Source File: Log4jRecord.java    From Sparkngin with GNU Affero General Public License v3.0 5 votes vote down vote up
public Log4jRecord(LoggingEvent event) {
  this.timestamp = event.getTimeStamp();
  this.threadName = event.getThreadName() ;
  this.loggerName = event.getLoggerName();
  this.level = event.getLevel().toString();
  this.message = event.getRenderedMessage();
}
 
Example 6
Source File: LoghubAppender.java    From aliyun-log-log4j-appender with Apache License 2.0 4 votes vote down vote up
@Override
protected void append(LoggingEvent event) {
  LogItem logItem = new LogItem();
  logItem.SetTime((int) (event.getTimeStamp() / 1000));
  DateTime dateTime = new DateTime(event.getTimeStamp());
  logItem.PushBack("time", dateTime.toString(formatter));
  logItem.PushBack("level", event.getLevel().toString());
  logItem.PushBack("thread", event.getThreadName());
  logItem.PushBack("location", event.getLocationInformation().fullInfo);
  Object message = event.getMessage();
  if(message==null){
    logItem.PushBack("message", "");
  }else{
    logItem.PushBack("message", event.getMessage().toString());
  }

  String throwable = getThrowableStr(event);
  if (throwable != null) {
    logItem.PushBack("throwable", throwable);
  }

  if (getLayout() != null) {
    logItem.PushBack("log", getLayout().format(event));
  }

  Map properties = event.getProperties();
  if (properties.size() > 0) {
    Object[] keys = properties.keySet().toArray();
    Arrays.sort(keys);
    for (int i = 0; i < keys.length; i++) {
      logItem.PushBack(keys[i].toString(), properties.get(keys[i])
          .toString());
    }
  }
  try {
    producer.send(projectConfig.getProject(), logStore, topic, source, logItem,
        new LoghubAppenderCallback(projectConfig.getProject(), logStore, topic, source, logItem));
  } catch (Exception e) {
    LogLog.error(
        "Failed to send log, project=" + project
            + ", logStore=" + logStore
            + ", topic=" + topic
            + ", source=" + source
            + ", logItem=" + logItem, e);
  }
}
 
Example 7
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 8
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 9
Source File: GlogLayout.java    From xio with Apache License 2.0 4 votes vote down vote up
@Override
public long getTimeStamp(LoggingEvent record) {
  return record.getTimeStamp();
}