예제 #1
0
    @Override
    public Object invoke(final Object o, final Method method, final Object[] objects)
        throws Throwable {
      if (method.getName().equals("logEvent")) {

        final StringBuilder missing = new StringBuilder();

        final Method[] methods = intrface.getMethods();

        for (final Method _method : methods) {
          final String name =
              NamingUtils.lowerFirst(NamingUtils.getMethodShortName(_method.getName()));

          final Annotation[] annotations = _method.getDeclaredAnnotations();
          for (final Annotation annotation : annotations) {
            final Constraint constraint = (Constraint) annotation;

            if (constraint.required() && msg.get(name) == null) {
              if (missing.length() > 0) {
                missing.append(", ");
              }
              missing.append(name);
            }
          }
        }

        if (missing.length() > 0) {
          throw new IllegalStateException(
              "Event " + msg.getId().getName() + " is missing required attributes " + missing);
        }
        EventLogger.logEvent(msg);
      }
      if (method.getName().equals("setCompletionStatus")) {
        final String name =
            NamingUtils.lowerFirst(NamingUtils.getMethodShortName(method.getName()));
        msg.put(name, objects[0].toString());
      }
      if (method.getName().startsWith("set")) {
        final String name =
            NamingUtils.lowerFirst(NamingUtils.getMethodShortName(method.getName()));

        /*
         * Perform any validation here. Currently the catalog doesn't
         * contain any information on validation rules.
         */
        msg.put(name, objects[0].toString());
      }

      return null;
    }
예제 #2
0
  private void appendStructuredElements(final StringBuilder buffer, final LogEvent event) {
    final Message message = event.getMessage();
    final boolean isStructured = message instanceof StructuredDataMessage;

    if (!isStructured && (fieldFormatters != null && fieldFormatters.isEmpty()) && !includeMdc) {
      buffer.append('-');
      return;
    }

    final Map<String, StructuredDataElement> sdElements =
        new HashMap<String, StructuredDataElement>();
    final Map<String, String> contextMap = event.getContextMap();

    if (mdcRequired != null) {
      checkRequired(contextMap);
    }

    if (fieldFormatters != null) {
      for (final Map.Entry<String, FieldFormatter> sdElement : fieldFormatters.entrySet()) {
        final String sdId = sdElement.getKey();
        final StructuredDataElement elem = sdElement.getValue().format(event);
        sdElements.put(sdId, elem);
      }
    }

    if (includeMdc && contextMap.size() > 0) {
      if (sdElements.containsKey(mdcSdId.toString())) {
        final StructuredDataElement union = sdElements.get(mdcSdId.toString());
        union.union(contextMap);
        sdElements.put(mdcSdId.toString(), union);
      } else {
        final StructuredDataElement formattedContextMap =
            new StructuredDataElement(contextMap, false);
        sdElements.put(mdcSdId.toString(), formattedContextMap);
      }
    }

    if (isStructured) {
      final StructuredDataMessage data = (StructuredDataMessage) message;
      final Map<String, String> map = data.getData();
      final StructuredDataId id = data.getId();
      final String sdId = getId(id);

      if (sdElements.containsKey(sdId)) {
        final StructuredDataElement union = sdElements.get(id.toString());
        union.union(map);
        sdElements.put(sdId, union);
      } else {
        final StructuredDataElement formattedData = new StructuredDataElement(map, false);
        sdElements.put(sdId, formattedData);
      }
    }

    if (sdElements.isEmpty()) {
      buffer.append('-');
      return;
    }

    for (final Map.Entry<String, StructuredDataElement> entry : sdElements.entrySet()) {
      formatStructuredElement(entry.getKey(), mdcPrefix, entry.getValue(), buffer, checker);
    }
  }