/**
   * 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;
  }
Beispiel #2
0
  // See http://bugzilla.slf4j.org/show_bug.cgi?id=114
  public void testLocationExtraction_Bug114() {
    XLogger logger = XLoggerFactory.getXLogger("UnitTest");
    int line = 137; // next line is line number 134
    logger.exit();
    logger.debug("hello");

    assertEquals(2, listAppender.list.size());

    {
      LoggingEvent e = listAppender.list.get(0);
      LocationInfo li = e.getLocationInformation();
      assertEquals(this.getClass().getName(), li.getClassName());
      assertEquals("" + line, li.getLineNumber());
    }

    {
      LoggingEvent e = listAppender.list.get(1);
      LocationInfo li = e.getLocationInformation();
      assertEquals(this.getClass().getName(), li.getClassName());
      assertEquals("" + (line + 1), li.getLineNumber());
    }
  }
 public String convert(LoggingEvent event) {
   LocationInfo locationInfo = event.getLocationInformation();
   switch (type) {
     case FULL_LOCATION_CONVERTER:
       return locationInfo.fullInfo;
     case METHOD_LOCATION_CONVERTER:
       return locationInfo.getMethodName();
     case LINE_LOCATION_CONVERTER:
       return locationInfo.getLineNumber();
     case FILE_LOCATION_CONVERTER:
       return locationInfo.getFileName();
     default:
       return null;
   }
 }
 /*     */ public String convert(LoggingEvent event) /*     */ {
   /* 483 */ LocationInfo locationInfo = event.getLocationInformation();
   /* 484 */ switch (this.type) {
       /*     */ case 1000:
       /* 486 */ return locationInfo.fullInfo;
       /*     */ case 1001:
       /* 488 */ return locationInfo.getMethodName();
       /*     */ case 1003:
       /* 490 */ return locationInfo.getLineNumber();
       /*     */ case 1004:
       /* 492 */ return locationInfo.getFileName();
       /* 493 */ case 1002:
   }
   return null;
   /*     */ }
 /**
  * @param event
  * @return
  */
 protected Name createName(LoggingEvent event, String name) {
   JavaName jName = new JavaName(name == null ? measurePointNameType : name);
   if (locationInfo) {
     LocationInfo locInfo = event.getLocationInformation();
     jName.setClassName(locInfo.getClassName());
     jName.setMethodName(locInfo.getMethodName());
     Integer lineNum = null;
     try {
       lineNum = Integer.parseInt(locInfo.getLineNumber());
     } catch (Exception e) {
     } // line number is not applicable, ignore it.
     if (lineNum != null) jName.setLineNumber(lineNum);
   } else {
     jName.setClassName(event.getLoggerName());
   }
   return jName;
 }
  private ErrorEventStat convert(LoggingEvent loggingEvent) {
    ErrorEventStat errorEventStat = new ErrorEventStat();
    LocationInfo locationInformation = loggingEvent.getLocationInformation();

    errorEventStat.setEmail(email);
    errorEventStat.setClientVersion(getArtifactId());
    errorEventStat.setOs(System.getProperty(SystemConstants.OS_NAME_PROP_KEY));
    errorEventStat.setOsVersion(System.getProperty(SystemConstants.OS_VERSION_PROP_KEY));
    errorEventStat.setJvm(System.getProperty(SystemConstants.JAVA_VERSION_PROP_KEY));
    errorEventStat.setThreadName(loggingEvent.getThreadName());
    errorEventStat.setMessage(loggingEvent.getRenderedMessage());
    errorEventStat.setFileName(locationInformation.getFileName());
    errorEventStat.setLineNumber(locationInformation.getLineNumber());
    errorEventStat.setMethodName(locationInformation.getMethodName());
    errorEventStat.setClassName(locationInformation.getClassName());

    ThrowableInformation throwableInformation = loggingEvent.getThrowableInformation();
    if (throwableInformation != null) {
      errorEventStat.setStackTrace(throwableInformation.getThrowable().getMessage());
    }

    return errorEventStat;
  }
Beispiel #7
0
    @Override
    public void run() {
      try {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        while (true) {
          final Event event = events.take();
          LoggingEvent logEvent = event.getEvent();

          String name = logEvent.getLogger().getName();
          Level level = logEvent.getLevel();

          MessageProperties amqpProps = new MessageProperties();
          amqpProps.setDeliveryMode(deliveryMode);
          amqpProps.setContentType(contentType);
          if (null != contentEncoding) {
            amqpProps.setContentEncoding(contentEncoding);
          }
          amqpProps.setHeader(CATEGORY_NAME, name);
          amqpProps.setHeader(CATEGORY_LEVEL, level.toString());
          if (generateId) {
            amqpProps.setMessageId(UUID.randomUUID().toString());
          }

          // Set applicationId, if we're using one
          if (null != applicationId) {
            amqpProps.setAppId(applicationId);
          }

          // Set timestamp
          Calendar tstamp = Calendar.getInstance();
          tstamp.setTimeInMillis(logEvent.getTimeStamp());
          amqpProps.setTimestamp(tstamp.getTime());

          // Copy properties in from MDC
          @SuppressWarnings("rawtypes")
          Map props = event.getProperties();
          @SuppressWarnings("unchecked")
          Set<Entry<?, ?>> entrySet = props.entrySet();
          for (Entry<?, ?> entry : entrySet) {
            amqpProps.setHeader(entry.getKey().toString(), entry.getValue());
          }
          LocationInfo locInfo = logEvent.getLocationInformation();
          if (!"?".equals(locInfo.getClassName())) {
            amqpProps.setHeader(
                "location",
                String.format(
                    "%s.%s()[%s]",
                    locInfo.getClassName(), locInfo.getMethodName(), locInfo.getLineNumber()));
          }

          StringBuilder msgBody;
          String routingKey;
          synchronized (layoutMutex) {
            msgBody = new StringBuilder(layout.format(logEvent));
            routingKey = routingKeyLayout.format(logEvent);
          }
          if (layout.ignoresThrowable() && null != logEvent.getThrowableInformation()) {
            ThrowableInformation tinfo = logEvent.getThrowableInformation();
            for (String line : tinfo.getThrowableStrRep()) {
              msgBody.append(String.format("%s%n", line));
            }
          }

          // Send a message
          try {
            Message message = null;
            if (AmqpAppender.this.charset != null) {
              try {
                message =
                    new Message(msgBody.toString().getBytes(AmqpAppender.this.charset), amqpProps);
              } catch (UnsupportedEncodingException e) {
                /* fall back to default */
              }
            }
            if (message == null) {
              message =
                  new Message(
                      msgBody.toString().getBytes(), amqpProps); // NOSONAR (default charset)
            }
            message = postProcessMessageBeforeSend(message, event);
            rabbitTemplate.send(exchangeName, routingKey, message);
          } catch (AmqpException e) {
            int retries = event.incrementRetries();
            if (retries < maxSenderRetries) {
              // Schedule a retry based on the number of times I've tried to re-send this
              retryTimer.schedule(
                  new TimerTask() {
                    @Override
                    public void run() {
                      events.add(event);
                    }
                  },
                  (long) (Math.pow(retries, Math.log(retries)) * 1000));
            } else {
              errorHandler.error(
                  "Could not send log message "
                      + logEvent.getRenderedMessage()
                      + " after "
                      + maxSenderRetries
                      + " retries",
                  e,
                  ErrorCode.WRITE_FAILURE,
                  logEvent);
            }
          }
        }
      } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
      }
    }
Beispiel #8
0
  public static final GelfMessage makeMessage(LoggingEvent event, GelfMessageProvider provider) {
    long timeStamp = Log4jVersionChecker.getTimeStamp(event);
    Level level = event.getLevel();

    String file = null;
    String lineNumber = null;
    if (provider.isIncludeLocation()) {
      LocationInfo locationInformation = event.getLocationInformation();
      file = locationInformation.getFileName();
      lineNumber = locationInformation.getLineNumber();
    }

    String renderedMessage = event.getRenderedMessage();
    String shortMessage;

    if (renderedMessage == null) {
      renderedMessage = "";
    }

    if (renderedMessage.length() > MAX_SHORT_MESSAGE_LENGTH) {
      shortMessage = renderedMessage.substring(0, MAX_SHORT_MESSAGE_LENGTH - 1);
    } else {
      shortMessage = renderedMessage;
    }

    if (provider.isExtractStacktrace()) {
      ThrowableInformation throwableInformation = event.getThrowableInformation();
      if (throwableInformation != null) {
        renderedMessage += "\n\r" + extractStacktrace(throwableInformation);
      }
    }

    GelfMessage gelfMessage =
        new GelfMessage(
            shortMessage,
            renderedMessage,
            timeStamp,
            String.valueOf(level.getSyslogEquivalent()),
            lineNumber,
            file);

    if (provider.getOriginHost() != null) {
      gelfMessage.setHost(provider.getOriginHost());
    }

    if (provider.getFacility() != null) {
      gelfMessage.setFacility(provider.getFacility());
    }

    Map<String, String> fields = provider.getFields();
    for (Map.Entry<String, String> entry : fields.entrySet()) {
      if (entry.getKey().equals(ORIGIN_HOST_KEY) && gelfMessage.getHost() == null) {
        gelfMessage.setHost(fields.get(ORIGIN_HOST_KEY));
      } else {
        gelfMessage.addField(entry.getKey(), entry.getValue());
      }
    }

    if (provider.isAddExtendedInformation()) {

      gelfMessage.addField(THREAD_NAME, event.getThreadName());
      gelfMessage.addField(LOGGER_NAME, event.getLoggerName());
      gelfMessage.addField(JAVA_TIMESTAMP, Long.toString(gelfMessage.getJavaTimestamp()));

      // Get MDC and add a GELF field for each key/value pair
      Map<String, Object> mdc = MDC.getContext();

      if (mdc != null) {
        for (Map.Entry<String, Object> entry : mdc.entrySet()) {

          gelfMessage.addField(entry.getKey(), entry.getValue().toString());
        }
      }

      // Get NDC and add a GELF field
      String ndc = event.getNDC();

      if (ndc != null) {

        gelfMessage.addField(LOGGER_NDC, ndc);
      }
    }

    return gelfMessage;
  }
  protected void append(LoggingEvent event) {
    Connection connection = null;
    try {
      connection = connectionSource.getConnection();
      connection.setAutoCommit(false);

      PreparedStatement insertStatement;
      if (cnxSupportsGetGeneratedKeys) {
        insertStatement = connection.prepareStatement(insertSQL, Statement.RETURN_GENERATED_KEYS);
      } else {
        insertStatement = connection.prepareStatement(insertSQL);
      }

      /*          insertStatement.setLong(1, event.getSequenceNumber());*/
      insertStatement.setLong(1, 0);

      insertStatement.setLong(2, event.getTimeStamp());
      insertStatement.setString(3, event.getRenderedMessage());
      insertStatement.setString(4, event.getLoggerName());
      insertStatement.setString(5, event.getLevel().toString());
      insertStatement.setString(6, event.getNDC());
      insertStatement.setString(7, event.getThreadName());
      insertStatement.setShort(8, DBHelper.computeReferenceMask(event));

      LocationInfo li;

      if (event.locationInformationExists() || locationInfo) {
        li = event.getLocationInformation();
      } else {
        li = LocationInfo.NA_LOCATION_INFO;
      }

      insertStatement.setString(9, li.getFileName());
      insertStatement.setString(10, li.getClassName());
      insertStatement.setString(11, li.getMethodName());
      insertStatement.setString(12, li.getLineNumber());

      int updateCount = insertStatement.executeUpdate();
      if (updateCount != 1) {
        LogLog.warn("Failed to insert loggingEvent");
      }

      ResultSet rs = null;
      Statement idStatement = null;
      boolean gotGeneratedKeys = false;
      if (cnxSupportsGetGeneratedKeys) {
        try {
          rs = (ResultSet) GET_GENERATED_KEYS_METHOD.invoke(insertStatement, null);
          gotGeneratedKeys = true;
        } catch (InvocationTargetException ex) {
          Throwable target = ex.getTargetException();
          if (target instanceof SQLException) {
            throw (SQLException) target;
          }
          throw ex;
        } catch (IllegalAccessException ex) {
          LogLog.warn("IllegalAccessException invoking PreparedStatement.getGeneratedKeys", ex);
        }
      }

      if (!gotGeneratedKeys) {
        insertStatement.close();
        insertStatement = null;

        idStatement = connection.createStatement();
        idStatement.setMaxRows(1);
        rs = idStatement.executeQuery(sqlDialect.getSelectInsertId());
      }

      // A ResultSet cursor is initially positioned before the first row; the
      // first call to the method next makes the first row the current row
      rs.next();
      int eventId = rs.getInt(1);

      rs.close();

      // we no longer need the insertStatement
      if (insertStatement != null) {
        insertStatement.close();
        insertStatement = null;
      }

      if (idStatement != null) {
        idStatement.close();
        idStatement = null;
      }

      Set propertiesKeys = event.getPropertyKeySet();

      if (propertiesKeys.size() > 0) {
        PreparedStatement insertPropertiesStatement =
            connection.prepareStatement(insertPropertiesSQL);

        for (Iterator i = propertiesKeys.iterator(); i.hasNext(); ) {
          String key = (String) i.next();
          String value = event.getProperty(key);

          // LogLog.info("id " + eventId + ", key " + key + ", value " + value);
          insertPropertiesStatement.setInt(1, eventId);
          insertPropertiesStatement.setString(2, key);
          insertPropertiesStatement.setString(3, value);

          if (cnxSupportsBatchUpdates) {
            insertPropertiesStatement.addBatch();
          } else {
            insertPropertiesStatement.execute();
          }
        }

        if (cnxSupportsBatchUpdates) {
          insertPropertiesStatement.executeBatch();
        }

        insertPropertiesStatement.close();
        insertPropertiesStatement = null;
      }

      String[] strRep = event.getThrowableStrRep();

      if (strRep != null) {
        LogLog.debug("Logging an exception");

        PreparedStatement insertExceptionStatement =
            connection.prepareStatement(insertExceptionSQL);

        for (short i = 0; i < strRep.length; i++) {
          insertExceptionStatement.setInt(1, eventId);
          insertExceptionStatement.setShort(2, i);
          insertExceptionStatement.setString(3, strRep[i]);
          if (cnxSupportsBatchUpdates) {
            insertExceptionStatement.addBatch();
          } else {
            insertExceptionStatement.execute();
          }
        }
        if (cnxSupportsBatchUpdates) {
          insertExceptionStatement.executeBatch();
        }
        insertExceptionStatement.close();
        insertExceptionStatement = null;
      }

      connection.commit();
    } catch (Throwable sqle) {
      LogLog.error("problem appending event", sqle);
    } finally {
      DBHelper.closeConnection(connection);
    }
  }