private String getColor(PaxLoggingEvent event, boolean noColor) {
   String color = null;
   if (!noColor
       && event != null
       && event.getLevel() != null
       && event.getLevel().toString() != null) {
     String lvl = event.getLevel().toString().toLowerCase();
     if (FATAL.equals(lvl)) {
       color = fatalColor;
     } else if (ERROR.equals(lvl)) {
       color = errorColor;
     } else if (WARN.equals(lvl)) {
       color = warnColor;
     } else if (INFO.equals(lvl)) {
       color = infoColor;
     } else if (DEBUG.equals(lvl)) {
       color = debugColor;
     } else if (TRACE.equals(lvl)) {
       color = traceColor;
     }
     if (color != null && color.length() == 0) {
       color = null;
     }
   }
   return color;
 }
 /* (non-Javadoc)
  * @see org.apache.karaf.log.core.internal.LogEventFormatter#format(org.ops4j.pax.logging.spi.PaxLoggingEvent, java.lang.String, boolean)
  */
 @Override
 public String format(PaxLoggingEvent event, String overridenPattern, boolean noColor) {
   final PatternConverter cnv =
       new PatternParser(overridenPattern != null ? overridenPattern : pattern).parse();
   String color = getColor(event, noColor);
   StringBuffer sb = new StringBuffer();
   sb.setLength(0);
   if (color != null) {
     sb.append(FIRST_ESC_CHAR);
     sb.append(SECOND_ESC_CHAR);
     sb.append(color);
     sb.append(COMMAND_CHAR);
   }
   for (PatternConverter pc = cnv; pc != null; pc = pc.next) {
     pc.format(sb, event);
   }
   if (event.getThrowableStrRep() != null) {
     for (String r : event.getThrowableStrRep()) {
       sb.append(r).append('\n');
     }
   }
   if (color != null) {
     sb.append(FIRST_ESC_CHAR);
     sb.append(SECOND_ESC_CHAR);
     sb.append("0");
     sb.append(COMMAND_CHAR);
   }
   return sb.toString();
 }
  private void testLog4j(CustomAppender customAppender) {
    // Ensure there's no events
    List events = customAppender.getEvents();
    events.clear();

    Class clazz = getClass();
    Logger log4j = Logger.getLogger(clazz);
    log4j.debug(EXPECTED_MESSAGE);

    assertEquals(1, events.size());

    PaxLoggingEvent event = (PaxLoggingEvent) events.get(0);
    assertEquals(Level.DEBUG.getSyslogEquivalent(), event.getLevel().getSyslogEquivalent());
    assertEquals(EXPECTED_MESSAGE, event.getMessage());

    events.clear();
  }
예제 #4
0
  protected void sendExchange(PaxLoggingEvent paxLoggingEvent) {
    MDC.put(PaxLoggingConsumer.class.getName(), endpoint.getName());
    if (paxLoggingEvent.getProperties().containsKey(PaxLoggingConsumer.class.getName())) {
      return;
    }

    Exchange exchange = endpoint.createExchange();
    // TODO: populate exchange headers
    exchange.getIn().setBody(paxLoggingEvent);

    LOG.trace("PaxLogging {} is firing", endpoint.getName());
    try {
      getProcessor().process(exchange);
      // log exception if an exception occurred and was not handled
      if (exchange.getException() != null) {
        getExceptionHandler()
            .handleException("Error processing exchange", exchange, exchange.getException());
      }
    } catch (Exception e) {
      getExceptionHandler()
          .handleException("Error processing exchange", exchange, exchange.getException());
    }
  }
예제 #5
0
  public void doAppend(final PaxLoggingEvent paxLoggingEvent) {
    try {

      // Only store TRACE events which have a trace-id.
      if ("TRACE".equals(paxLoggingEvent.getLevel().toString().toUpperCase())
          && !paxLoggingEvent.getProperties().containsKey("trace-id")) {
        return;
      }
      StringBuilder writer = new StringBuilder();
      writer.append("{ \"host\" : ");
      quote(name, writer);
      writer.append(",\n  \"timestamp\" : ");
      quote(formatDate(paxLoggingEvent.getTimeStamp()), writer);
      writer.append(",\n  \"level\" : ");
      quote(paxLoggingEvent.getLevel().toString(), writer);
      writer.append(",\n  \"logger\" : ");
      quote(paxLoggingEvent.getLoggerName(), writer);
      writer.append(",\n  \"thread\" : ");
      quote(paxLoggingEvent.getThreadName(), writer);
      writer.append(",\n  \"message\" : ");
      quote(paxLoggingEvent.getMessage(), writer);

      String[] throwable = paxLoggingEvent.getThrowableStrRep();
      if (throwable != null) {
        throwable = addMavenCoord(throwable);
        writer.append(",\n  \"exception\" : [");
        for (int i = 0; i < throwable.length; i++) {
          if (i != 0) writer.append(", ");
          quote(throwable[i], writer);
        }
        writer.append("]");
      }

      writer.append(",\n  \"properties\" : { ");
      boolean first = true;
      Map<String, String> properties = new HashMap<String, String>();
      Set<Map.Entry> set = paxLoggingEvent.getProperties().entrySet();
      for (Map.Entry entry : set) {
        Object key = entry.getKey();
        Object value = entry.getValue();
        if (key != null && value != null) {
          properties.put(key.toString(), value.toString());
        }
      }
      addMavenCoord(properties);

      for (Object key : properties.keySet()) {
        if (first) {
          first = false;
        } else {
          writer.append(", ");
        }
        quote(key.toString(), writer);
        writer.append(": ");
        quote(properties.get(key).toString(), writer);
      }
      writer.append(" }");
      writer.append("\n}");

      storage.store(type, paxLoggingEvent.getTimeStamp(), writer.toString());
    } catch (Exception e) {
      LOGGER.warn("Error appending log to storage", e);
    }
  }