Example #1
0
  /**
   * Creates a map of properties that represent the GELF message.
   *
   * @param logEvent The log event
   * @return map of gelf properties
   */
  private Map<String, Object> mapFields(ILoggingEvent logEvent) {
    Map<String, Object> map = new HashMap<String, Object>();

    map.put("facility", facility);

    map.put("host", hostname);

    String message = patternLayout.doLayout(logEvent);

    map.put("full_message", message);
    map.put("short_message", truncateToShortMessage(message, logEvent));

    // Ever since version 0.9.6, GELF accepts timestamps in decimal form.
    double logEventTimeTimeStamp = logEvent.getTimeStamp() / 1000.0;

    stackTraceField(map, logEvent);

    map.put("timestamp", logEventTimeTimeStamp);

    map.put("version", "1.0");

    map.put("level", LevelToSyslogSeverity.convert(logEvent));

    additionalFields(map, logEvent);

    staticAdditionalFields(map);

    return map;
  }
  @Test
  public void testAppendLoggingEvent() throws SQLException {
    ILoggingEvent event = createLoggingEvent();

    appender.append(event);
    StatusPrinter.printInCaseOfErrorsOrWarnings(lc);

    Statement stmt = connectionSource.getConnection().createStatement();
    ResultSet rs = null;
    rs = stmt.executeQuery("SELECT * FROM logging_event");
    if (rs.next()) {
      assertEquals(event.getTimeStamp(), rs.getLong(DBAppender.TIMESTMP_INDEX));
      assertEquals(event.getFormattedMessage(), rs.getString(DBAppender.FORMATTED_MESSAGE_INDEX));
      assertEquals(event.getLoggerName(), rs.getString(DBAppender.LOGGER_NAME_INDEX));
      assertEquals(event.getLevel().toString(), rs.getString(DBAppender.LEVEL_STRING_INDEX));
      assertEquals(event.getThreadName(), rs.getString(DBAppender.THREAD_NAME_INDEX));
      assertEquals(
          DBHelper.computeReferenceMask(event), rs.getShort(DBAppender.REFERENCE_FLAG_INDEX));
      assertEquals(String.valueOf(diff), rs.getString(DBAppender.ARG0_INDEX));
      StackTraceElement callerData = event.getCallerData()[0];
      assertEquals(callerData.getFileName(), rs.getString(DBAppender.CALLER_FILENAME_INDEX));
      assertEquals(callerData.getClassName(), rs.getString(DBAppender.CALLER_CLASS_INDEX));
      assertEquals(callerData.getMethodName(), rs.getString(DBAppender.CALLER_METHOD_INDEX));
    } else {
      fail("No row was inserted in the database");
    }

    rs.close();
    stmt.close();
  }
  public String convert(ILoggingEvent event) {
    final StringBuilder sb = new StringBuilder();

    final int pri = facility + LevelToSyslogSeverity.convert(event);

    sb.append("<");
    sb.append(pri);
    sb.append(">");
    sb.append(computeTimeStampString(event.getTimeStamp()));
    sb.append(' ');
    sb.append(localHostName);
    sb.append(' ');

    return sb.toString();
  }
  @Override
  protected void append(ILoggingEvent p) {
    if (!enable || eventWriter == null || !eventWriter.isEnable()) {
      return;
    }

    Level logLevel = p.getLevel();
    if (logLevel.toInt() < minimumLevel) {
      return;
    }

    RecordEventSummary eventInfo = new RecordEventSummary(-1);

    eventInfo.setEventType(eventType);
    eventInfo.setThreadName(p.getThreadName());
    eventInfo.setEventDate(new Date(p.getTimeStamp()));
    eventInfo.setEventClassName(p.getLoggerName());
    eventInfo.setEventMethodName(logLevel.toString().toLowerCase());

    // use message instead of formattedMessage for header!!
    // => use "... {0} .. {1} .." to compress event summary encoding
    eventInfo.setEventMethodDetail(p.getMessage());

    if (!eventWriter.isEnable(eventInfo)) {
      return;
    }

    LogbackEventData eventData = new LogbackEventData();
    eventData.setLevel(eventInfo, logLevel.toString());
    eventData.setFormattedMessage(p.getFormattedMessage());
    eventData.setArgumentArray(p.getArgumentArray());

    if (p.getThrowableProxy() != null) {
      IThrowableProxy throwableProxy = p.getThrowableProxy();
      // throwableProxy.getClassName()
      // throwableProxy.getMessage()
      StackTraceElementProxy[] traceElts = throwableProxy.getStackTraceElementProxyArray();
      eventData.setStackTraceElements(traceElts);
    }

    Map<String, String> mdcPropertyMap = p.getMDCPropertyMap();
    if (mdcPropertyMap != null && !mdcPropertyMap.isEmpty()) {
      eventData.setMDCPropertyMap(mdcPropertyMap);
    }

    eventWriter.addEvent(eventInfo, eventData, null);
  }
  /** Not Thread safe */
  @Override
  public boolean evaluate(ILoggingEvent event) throws NullPointerException, EvaluationException {
    long now = event.getTimeStamp();
    if (lastEvalutorTime == 0) {
      lastEvalutorTime = now;
    }

    recentLoggerName = event.getLoggerName();

    if (directSend
        || errorCount.incrementAndGet() >= errorCountLimit
        || now - lastEvalutorTime >= evalutorTimeOffset) {
      directSend = false;
      errorCount.set(0);
      lastEvalutorTime = now;
      return true;
    }
    return false;
  }
Example #6
0
  @Override
  protected void append(final ILoggingEvent eventObject) {

    try {
      final Event event = new Event();
      event.setApplication(application);
      event.setComponent(eventObject.getLoggerName());

      Calendar instance = Calendar.getInstance();
      instance.setTimeInMillis(eventObject.getTimeStamp());
      event.setDate(instance.getTime());
      event.setMessage(eventObject.getMessage());

      event.setStacktrace(createStackTrace(eventObject));

      engine.post(event);
    } catch (Exception e) {
      addStatus(new ErrorStatus("Error in logpile treament.", this, e));
    }
  }
  public String format(ILoggingEvent event) {
    StringBuilder sb = new StringBuilder();
    sb.append("{");
    fieldName("level", sb);
    quote(event.getLevel().levelStr, sb);
    sb.append(COMMA);
    fieldName("logger", sb);
    quote(event.getLoggerName(), sb);
    sb.append(COMMA);
    fieldName("timestamp", sb);
    sb.append(event.getTimeStamp());
    sb.append(COMMA);
    fieldName("message", sb);
    if (this.expectJson) {
      sb.append(event.getFormattedMessage());
    } else {
      quote(event.getFormattedMessage(), sb);
    }

    sb.append("}");
    return sb.toString();
  }
  public boolean evaluate(ILoggingEvent event) {
    if (event.getLevel().levelInt < level.levelInt) {
      return false;
    }

    long now = event.getTimeStamp();
    if (now - lastTime > period) {
      triggerCount = 0;
      lastTime = now;
    }
    if (tooManyMsg.equals(event.getMessage())) {
      return true;
    } else if (triggerCount > rate) {
      return false;
    } else if (triggerCount == rate) {
      LoggerFactory.getLogger(RateLimitEventEvaluator.class).error(tooManyMsg);
      ++triggerCount;
      return false;
    } else {
      ++triggerCount;
      return true;
    }
  }
Example #9
0
    @Override
    public void run() {
      try {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        while (true) {
          final Event event = events.take();
          ILoggingEvent logEvent = event.getEvent();

          String name = logEvent.getLoggerName();
          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 timestamp
          Calendar tstamp = Calendar.getInstance();
          tstamp.setTimeInMillis(logEvent.getTimeStamp());
          amqpProps.setTimestamp(tstamp.getTime());

          // Copy properties in from MDC
          Map<String, String> props = event.getProperties();
          Set<Entry<String, String>> entrySet = props.entrySet();
          for (Entry<String, String> entry : entrySet) {
            amqpProps.setHeader(entry.getKey(), entry.getValue());
          }
          String[] location = locationLayout.doLayout(logEvent).split("\\|");
          if (!"?".equals(location[0])) {
            amqpProps.setHeader(
                "location", String.format("%s.%s()[%s]", location[0], location[1], location[2]));
          }
          String msgBody;
          String routingKey = routingKeyLayout.doLayout(logEvent);
          // Set applicationId, if we're using one
          if (applicationId != null) {
            amqpProps.setAppId(applicationId);
          }

          if (abbreviator != null && logEvent instanceof LoggingEvent) {
            ((LoggingEvent) logEvent).setLoggerName(abbreviator.abbreviate(name));
            msgBody = layout.doLayout(logEvent);
            ((LoggingEvent) logEvent).setLoggerName(name);
          } else {
            msgBody = layout.doLayout(logEvent);
          }

          // Send a message
          try {
            Message message = null;
            if (AmqpAppender.this.charset != null) {
              try {
                message = new Message(msgBody.getBytes(AmqpAppender.this.charset), amqpProps);
              } catch (UnsupportedEncodingException e) {
                message = new Message(msgBody.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 {
              addError(
                  "Could not send log message "
                      + logEvent.getMessage()
                      + " after "
                      + maxSenderRetries
                      + " retries",
                  e);
            }
          }
        }
      } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
      }
    }