/**
  * 当日志处理队列到达discardingThreshold时 1.丢弃掉 所有TRACE, DEBUG日志 2.保留level大于info的日志 3.保留com.tcc
  * logger下info的日志
  *
  * @param event 日志事件对象
  * @return 是否抛弃
  * @see
  *     ch.qos.logback.classic.AsyncAppender#isDiscardable(ch.qos.logback.classic.spi.ILoggingEvent)
  */
 protected boolean isDiscardable(ILoggingEvent event) {
   Level level = event.getLevel();
   if (level.toInt() < Level.INFO_INT) {
     return true;
   } else if (level.toInt() > Level.INFO_INT) {
     return false;
   } else {
     if (event.getLoggerName() != null && event.getLoggerName().startsWith(LOGGER_NAME)) {
       return false;
     } else {
       return true;
     }
   }
 }
Example #2
0
 private void dispatchEvents(LoggerContext lc) {
   try {
     socket.setSoTimeout(acceptConnectionTimeout);
     ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
     socket.setSoTimeout(0);
     addInfo(receiverId + "connection established");
     while (true) {
       ILoggingEvent event = (ILoggingEvent) ois.readObject();
       Logger remoteLogger = lc.getLogger(event.getLoggerName());
       if (remoteLogger.isEnabledFor(event.getLevel())) {
         remoteLogger.callAppenders(event);
       }
     }
   } catch (EOFException ex) {
     addInfo(receiverId + "end-of-stream detected");
   } catch (IOException ex) {
     addInfo(receiverId + "connection failed: " + ex);
   } catch (ClassNotFoundException ex) {
     addInfo(receiverId + "unknown event class: " + ex);
   } finally {
     CloseUtil.closeQuietly(socket);
     socket = null;
     addInfo(receiverId + "connection closed");
   }
 }
  @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();
  }
 @Override
 protected void append(ILoggingEvent event) {
   Logger logger = Logger.getLogger(event.getLoggerName());
   Level level = event.getLevel();
   java.util.logging.Level julLevel = takeLevel(level);
   logger.log(julLevel, event.getFormattedMessage());
 }
 @Override
 public String doLayout(ILoggingEvent event) {
   StringBuffer sbuf = new StringBuffer(128);
   sbuf.append("-- ");
   sbuf.append("[");
   sbuf.append(event.getLevel());
   sbuf.append("]");
   sbuf.append(event.getLoggerName());
   sbuf.append(" - ");
   sbuf.append(event.getFormattedMessage().replaceAll("\n", "\n\t"));
   return sbuf.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 #8
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();
  }
Example #10
0
  /**
   * Converts the additional fields into proper GELF JSON
   *
   * @param map The map of additional fields
   * @param eventObject The Logging event that we are converting to GELF
   */
  private void additionalFields(Map<String, Object> map, ILoggingEvent eventObject) {

    if (useLoggerName) {
      map.put("_loggerName", eventObject.getLoggerName());
    }

    if (useMarker && eventHasMarker(eventObject)) {
      map.put("_marker", eventObject.getMarker().toString());
    }

    if (useThreadName) {
      map.put("_threadName", eventObject.getThreadName());
    }

    Map<String, String> mdc = eventObject.getMDCPropertyMap();

    if (mdc != null) {

      if (includeFullMDC) {
        for (Entry<String, String> e : mdc.entrySet()) {
          if (additionalFields.containsKey(e.getKey())) {
            map.put(additionalFields.get(e.getKey()), e.getValue());
          } else {
            map.put("_" + e.getKey(), e.getValue());
          }
        }
      } else {
        for (String key : additionalFields.keySet()) {
          String field = mdc.get(key);
          if (field != null) {
            map.put(additionalFields.get(key), field);
          }
        }
      }
    }
  }
Example #11
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();
      }
    }