コード例 #1
0
  @Test
  public void callerData() {
    assertEquals(0, listAppender.list.size());

    PatternLayout pl = new PatternLayout();
    pl.setPattern("%-5level [%class] %logger - %msg");
    pl.setContext(lc);
    pl.start();
    listAppender.layout = pl;

    Logger logger = Logger.getLogger("basic-test");
    logger.trace("none");
    assertEquals(0, listAppender.list.size());

    rootLogger.setLevel(Level.TRACE);
    logger.trace(HELLO);
    assertEquals(1, listAppender.list.size());

    ILoggingEvent event = (ILoggingEvent) listAppender.list.get(0);
    assertEquals(HELLO, event.getMessage());

    assertEquals(1, listAppender.stringList.size());
    assertEquals(
        "TRACE [" + Log4jInvocation.class.getName() + "] basic-test - Hello",
        listAppender.stringList.get(0));
  }
コード例 #2
0
  private String getStackTraceFromEvent(ILoggingEvent logEvent) {
    IThrowableProxy throwable = logEvent.getThrowableProxy();
    if (null != throwable && null != throwable.getStackTraceElementProxyArray()) {
      StringBuilder sb = new StringBuilder();
      for (StackTraceElementProxy elt : throwable.getStackTraceElementProxyArray()) {
        sb.append(elt.toString());
        sb.append("\n");
      }
      return sb.toString();
    }

    if (logEvent.getCallerData() != null) {
      if (debug) {
        System.err.println(String.format("%s.append: falling back to appender stacktrace: ", this));
      }
      StringBuilder sb = new StringBuilder();
      for (StackTraceElement elt : logEvent.getCallerData()) {
        sb.append(elt);
        sb.append("\n");
      }
      return sb.toString();
    }

    return null;
  }
コード例 #3
0
 @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());
 }
コード例 #4
0
  @Test
  public void testAppendThrowable() throws SQLException {
    ILoggingEvent event = createLoggingEvent();

    appender.append(event);

    Statement stmt = connectionSource.getConnection().createStatement();
    ResultSet rs = null;
    rs = stmt.executeQuery("SELECT * FROM LOGGING_EVENT_EXCEPTION where EVENT_ID = 0");

    rs.next();
    String expected = "java.lang.Exception: test Ex";
    String firstLine = rs.getString(3);
    assertTrue(
        "[" + firstLine + "] does not match [" + expected + "]", firstLine.contains(expected));

    int i = 0;
    while (rs.next()) {
      expected = event.getThrowableProxy().getStackTraceElementProxyArray()[i].toString();
      String st = rs.getString(3);
      assertTrue("[" + st + "] does not match [" + expected + "]", st.contains(expected));
      i++;
    }
    assertTrue(i != 0);
    rs.close();
    stmt.close();
  }
コード例 #5
0
 @Override
 public String convert(ILoggingEvent event) {
   final Marker marker = event.getMarker();
   return marker == null
       ? (event.getLevel() == null ? "" : event.getLevel().toString())
       : marker.getName();
 }
コード例 #6
0
ファイル: SocketReceiver.java プロジェクト: henrikno/logback
 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");
   }
 }
コード例 #7
0
  protected synchronized void append(E event) /* throws LogbackException */ {
    timesCalled.incrementAndGet();
    ILoggingEvent logEvent = (ILoggingEvent) event;
    EventDSL rEvent;
    try {
      rEvent =
          riemannClient
              .event()
              .service(serviceName)
              .host(hostname)
              .state("error")
              .attribute("message", logEvent.getFormattedMessage());

      String stInfo = getStackTraceFromEvent(logEvent);
      if (null != stInfo) {
        rEvent.attribute("stacktrace", stInfo);
      }

      try {
        if (debug) {
          System.err.println(String.format("%s.append: sending riemann event: %s", this, rEvent));
        }
        rEvent.send();
        if (debug) {
          System.err.println(
              String.format(
                  "%s.append(logEvent): sent to riemann %s:%s",
                  this, riemannHostname, riemannPort));
        }
      } catch (Exception ex) {
        if (debug) {
          System.err.println(String.format("%s: Error sending event %s", this, ex));
          ex.printStackTrace();
        }

        riemannClient.reconnect();
        if (null != rEvent) {
          rEvent.send();
        }
      }
    } catch (Exception ex) {
      // do nothing
      if (debug) {
        System.err.println(String.format("RiemannAppender.append: Error during append(): %s", ex));
        ex.printStackTrace();
      }
    }

    // System.err.println(String.format(
    //      "RiemannAppender{serviceName=%s;riemannHostname=%s;riemannPort=%s;hostname=%s}",
    //      serviceName,
    //      riemannHostname,
    //      riemannPort,
    //      hostname));

    // System.err.println("RiemannAppender: event: " + event);
    // System.err.println("RiemannAppender: event.getClass(): " + event.getClass());
  }
コード例 #8
0
 void handle(final ILoggingEvent event) {
   final ProgramError error =
       addError(
           LogUtils.getErrorLevel(event.getLevel()),
           event.getFormattedMessage(),
           LogUtils.getThrowable(event),
           isAppError(event.getMarker()));
   handleErrorEvent(error);
 }
コード例 #9
0
  @Test
  public void basic() {
    assertEquals(0, listAppender.list.size());

    Logger logger = Logger.getLogger("basic-test");
    logger.debug(HELLO);

    assertEquals(1, listAppender.list.size());
    ILoggingEvent event = (ILoggingEvent) listAppender.list.get(0);
    assertEquals(HELLO, event.getMessage());
  }
コード例 #10
0
 @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();
 }
コード例 #11
0
 /**
  * 当日志处理队列到达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;
     }
   }
 }
コード例 #12
0
 private void stackTraceField(Map<String, Object> map, ILoggingEvent eventObject) {
   IThrowableProxy throwableProxy = eventObject.getThrowableProxy();
   if (throwableProxy != null) {
     StackTraceElementProxy[] proxyStackTraces = throwableProxy.getStackTraceElementProxyArray();
     if (proxyStackTraces != null && proxyStackTraces.length > 0) {
       StackTraceElement[] callStackTraces = eventObject.getCallerData();
       if (callStackTraces.length > 0) {
         StackTraceElement lastStack = callStackTraces[0];
         map.put("file", lastStack.getFileName());
         map.put("line", String.valueOf(lastStack.getLineNumber()));
       }
     }
   }
 }
コード例 #13
0
  public void process(ILoggingEvent event) {
    if (hold) {
      event.getThreadName(); // force "logback" to remember current thread
      events.add(event);
      if (event.getLevel().isGreaterOrEqual(Level.ERROR)) {
        hold = false;

        flushTraceLogs();

        events.clear();
      }
    } else {
      appender.doAppend(event);
    }
  }
コード例 #14
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;
  }
コード例 #15
0
  @Test
  public void settingIncludeCallerDataPropertyCausedCallerDataToBeIncluded() {
    asyncAppender.addAppender(listAppender);
    asyncAppender.setIncludeCallerData(true);
    asyncAppender.start();

    asyncAppender.doAppend(builder.build(diff));
    asyncAppender.stop();

    // check the event
    assertEquals(1, listAppender.list.size());
    ILoggingEvent e = listAppender.list.get(0);
    assertTrue(e.hasCallerData());
    StackTraceElement ste = e.getCallerData()[0];
    assertEquals(thisClassName, ste.getClassName());
  }
コード例 #16
0
  @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);
  }
コード例 #17
0
 @Override
 protected void append(ILoggingEvent event) {
   if (event == null || !isStarted()) return;
   if (includeCallerData) {
     event.getCallerData();
   }
   super.append(event);
 }
コード例 #18
0
ファイル: MarkerFilter.java プロジェクト: sdcuike/doctor-2015
 @Override
 public FilterReply decide(ILoggingEvent event) {
   Marker marker = event.getMarker();
   if (!isStarted()) return FilterReply.NEUTRAL;
   if (null == marker) return onMismatch;
   if (markerToMatch.contains(marker)) return onMatch;
   return onMismatch;
 }
コード例 #19
0
  private Throwable getThrowable(ILoggingEvent logEvent) {
    if (logEvent.getThrowableProxy() instanceof ThrowableProxy) {
      return ((ThrowableProxy) logEvent.getThrowableProxy()).getThrowable();
    }

    Object[] args = logEvent.getArgumentArray();
    if (args == null || args.length == 0) {
      return null;
    }

    Object lastObject = args[args.length - 1];
    if (lastObject instanceof Throwable) {
      return (Throwable) lastObject;
    }

    return null;
  }
コード例 #20
0
  @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();
  }
コード例 #21
0
  /** 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;
  }
コード例 #22
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));
    }
  }
コード例 #23
0
ファイル: MDCConverter.java プロジェクト: our880tom/logback
  @Override
  public String convert(ILoggingEvent event) {
    Map<String, String> mdcPropertyMap = event.getMDCPropertyMap();

    if (mdcPropertyMap == null) {
      return EMPTY_STRING;
    }

    if (key == null) {
      return outputMDCForAllKeys(mdcPropertyMap);
    } else {

      String value = event.getMDCPropertyMap().get(key);
      if (value != null) {
        return value;
      } else {
        return EMPTY_STRING;
      }
    }
  }
コード例 #24
0
  @Override
  public FilterReply decide(ILoggingEvent event) {
    if (!isStarted()) {
      return FilterReply.NEUTRAL;
    }

    if (event.getLevel().levelInt >= lowLevel.levelInt) {
      return FilterReply.ACCEPT;
    } else {
      return FilterReply.DENY;
    }
  }
コード例 #25
0
  @Test
  public void eventWasPreparedForDeferredProcessing() {
    asyncAppender.addAppender(listAppender);
    asyncAppender.start();

    String k = "k" + diff;
    MDC.put(k, "v");
    asyncAppender.doAppend(builder.build(diff));
    MDC.clear();

    asyncAppender.stop();
    assertFalse(asyncAppender.isStarted());

    // check the event
    assertEquals(1, listAppender.list.size());
    ILoggingEvent e = listAppender.list.get(0);

    // check that MDC values were correctly retained
    assertEquals("v", e.getMDCPropertyMap().get(k));
    assertFalse(e.hasCallerData());
  }
コード例 #26
0
  @Override
  public FilterReply decide(ILoggingEvent event) {
    if (!isStarted()) {
      return FilterReply.NEUTRAL;
    }

    if (event.getLevel().isGreaterOrEqual(level)) {
      return FilterReply.NEUTRAL;
    } else {
      return FilterReply.DENY;
    }
  }
コード例 #27
0
  @Override
  protected void append(ILoggingEvent logEvent) {
    int severity = 0;
    switch (logEvent.getLevel().levelInt) {
      case Level.ERROR_INT:
        severity = IStatus.ERROR;
        break;
      case Level.WARN_INT:
        severity = IStatus.WARNING;
        break;
      case Level.INFO_INT:
        severity = IStatus.INFO;
        break;
      default:
        return;
    }

    IStatus status =
        new Status(severity, BUNDLE_ID, logEvent.getFormattedMessage(), getThrowable(logEvent));
    ILog eclipseLog = Platform.getLog(getSelfBundle());
    eclipseLog.log(status);
  }
コード例 #28
0
  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();
  }
コード例 #29
0
  private String createStackTrace(final ILoggingEvent eventObject) {

    final IThrowableProxy tp = eventObject.getThrowableProxy();
    if (tp != null) {
      final StringBuilder stringBuilder =
          new StringBuilder(tp.getClassName()).append(": ").append(tp.getMessage());
      for (final StackTraceElementProxy stackTraceLine : tp.getStackTraceElementProxyArray()) {
        stringBuilder.append(stackTraceLine.toString()).append("\n");
      }

      return stringBuilder.toString();
    }
    return null;
  }
コード例 #30
0
  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;
    }
  }