Beispiel #1
0
 /**
  * Convert logging event to map
  *
  * @param e
  * @return
  */
 public static Map<String, Object> toMap(LoggingEvent e) {
   final Map<String, Object> m =
       Objects.newHashMap(
           "name", e.getLoggerName(),
           "date", new Date(e.getTimeStamp()),
           "level", e.getLevel().toString(),
           "thread", e.getThreadName(),
           "message", "" + e.getMessage(),
           "fileName", e.getLocationInformation().getFileName(),
           "methodName", e.getLocationInformation().getMethodName(),
           "lineNumber", e.getLocationInformation().getLineNumber(),
           "requestId", e.getMDC("requestId"),
           "sessionId", e.getMDC("sessionId"),
           "freeMemory", Runtime.getRuntime().freeMemory(),
           "throwable", null);
   if (e.getThrowableInformation() != null && e.getThrowableInformation().getThrowable() != null) {
     Throwable t = e.getThrowableInformation().getThrowable();
     m.put(
         "throwable",
         Objects.newHashMap(
             "message", t.getMessage(),
             "class", t.getClass().getName(),
             "stackTrace", getStackTrace(t)));
   }
   return m;
 }
  /**
   * 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);
  }
 public void process(LoggingEvent loggingEvent) {
   // we ignore events for SLF4J
   if (!ORG_SLF4J_IMPL_JCL_LOGGER_ADAPTER.equals(
       loggingEvent.getLocationInformation().getClassName())) {
     queue.offer(loggingEvent);
   }
 }
  @Test
  public void appenderStoresMessages() {
    MemoryAppender appender = new MemoryAppender();
    BasicConfigurator.configure(appender);

    Logger logger = Logger.getLogger(TEST_CATEGORY);
    logger.setLevel(Level.ERROR);

    logger.error(TEST_MESSAGE);

    LinkedList<LoggingEvent> events = appender.getEvents();
    assertEquals(1, events.size());

    LoggingEvent event = events.getFirst();
    assertEquals(Level.ERROR, event.getLevel());
    assertEquals(TEST_CATEGORY, event.getLoggerName());
    assertEquals(TEST_MESSAGE, event.getMessage());
    assertEquals(TEST_LOCATION, event.getLocationInformation().getClassName());

    appender.clear();
    assertEquals(0, events.size());

    logger.error(TEST_MESSAGE);
    assertEquals(1, appender.getEvents().size());
  }
 @Override
 protected void append(LoggingEvent event) {
   try {
     if (fetchLocationInfo) {
       event.getLocationInformation();
     }
     queue.enqueue(event);
   } catch (IOException e) {
     throw new RuntimeException(e);
   }
 }
  /**
   * 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 #7
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) /*     */ {
   /* 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;
   /*     */ }
 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;
   }
 }
Beispiel #10
0
 /**
  * @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 #12
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 #13
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;
  }
Beispiel #14
0
  public String _format(LoggingEvent event) {
    String message = (String) event.getMessage();
    if (message == null) {
      message = "";
    }
    StringBuilder sb = new StringBuilder(message.length() + 80);

    long now = event.timeStamp;
    long timeFromStart = now - startTime;
    long timeSinceLast = now - lastTime;
    lastTime = now;
    String shortClassName =
        getShortClassName(
            event.getLocationInformation().getClassName(),
            event.getLocationInformation().getMethodName());

    /**
     * * sb.append(timeFromStart).append(' ').append(timeSinceLast); sb.append(' ');
     * sb.append(record.getSourceClassName()).append('.').append( record.getSourceMethodName());
     * sb.append(' '); sb.append(record.getLevel()); *
     */
    SolrRequestInfo requestInfo = SolrRequestInfo.getRequestInfo();
    SolrQueryRequest req = requestInfo == null ? null : requestInfo.getReq();
    SolrCore core = req == null ? null : req.getCore();
    ZkController zkController = null;
    CoreInfo info = null;

    if (core != null) {
      info = coreInfoMap.get(core.hashCode());
      if (info == null) {
        info = new CoreInfo();
        info.shortId = "C" + Integer.toString(CoreInfo.maxCoreNum++);
        coreInfoMap.put(core.hashCode(), info);

        if (sb.length() == 0) sb.append("ASYNC ");
        sb.append(" NEW_CORE " + info.shortId);
        sb.append(" name=" + core.getName());
        sb.append(" " + core);
      }

      zkController = core.getCoreDescriptor().getCoreContainer().getZkController();
      if (zkController != null) {
        if (info.url == null) {
          info.url = zkController.getBaseUrl() + "/" + core.getName();
          sb.append(" url=" + info.url + " node=" + zkController.getNodeName());
        }

        Map<String, Object> coreProps = getReplicaProps(zkController, core);
        if (info.coreProps == null || !coreProps.equals(info.coreProps)) {
          info.coreProps = coreProps;
          final String corePropsString =
              "coll:"
                  + core.getCoreDescriptor().getCloudDescriptor().getCollectionName()
                  + " core:"
                  + core.getName()
                  + " props:"
                  + coreProps;
          sb.append(" " + info.shortId + "_STATE=" + corePropsString);
        }
      }
    }

    if (sb.length() > 0) sb.append('\n');
    sb.append(timeFromStart);

    // sb.append("\nL").append(record.getSequenceNumber()); // log number is
    // useful for sequencing when looking at multiple parts of a log file, but
    // ms since start should be fine.
    appendThread(sb, event);

    appendMDC(sb);

    // todo: should be able to get port from core container for non zk tests

    if (info != null) {
      sb.append(' ').append(info.shortId); // core
    }

    if (shortClassName.length() > 0) {
      sb.append(' ').append(shortClassName);
    }

    if (event.getLevel() != Level.INFO) {
      sb.append(' ').append(event.getLevel());
    }

    sb.append(' ');
    appendMultiLineString(sb, message);
    ThrowableInformation thInfo = event.getThrowableInformation();
    if (thInfo != null) {
      Throwable th = event.getThrowableInformation().getThrowable();
      if (th != null) {
        sb.append(' ');
        String err = SolrException.toStr(th);
        String ignoredMsg = SolrException.doIgnore(th, err);
        if (ignoredMsg != null) {
          sb.append(ignoredMsg);
        } else {
          sb.append(err);
        }
      }
    }

    sb.append('\n');

    /**
     * * Isn't core specific... prob better logged from zkController if (info != null) {
     * ClusterState clusterState = zkController.getClusterState(); if (info.clusterState !=
     * clusterState) { // something has changed in the matrix... sb.append(zkController.getBaseUrl()
     * + " sees new ClusterState:"); } } *
     */
    return sb.toString();
  }
Beispiel #15
0
 void verify(LoggingEvent le, String expectedMsg) {
   assertEquals(expectedMsg, le.getMessage());
   assertEquals(EXPECTED_FILE_NAME, le.getLocationInformation().getFileName());
 }
 String getFullyQualifiedName(LoggingEvent event) {
   return event.getLocationInformation().getClassName();
 }
  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);
    }
  }