Search in sources :

Example 1 with LogEvent

use of io.fabric8.insight.log.LogEvent in project fabric8 by jboss-fuse.

the class Logs method newInstance.

public static LogEvent newInstance(PaxLoggingEvent event) {
    LogEvent answer = new LogEvent();
    try {
        answer.setLevel(toString(event.getLevel()));
    } catch (NoClassDefFoundError error) {
        // ENTESB-2234, KARAF-3350: Ignore NoClassDefFoundError exceptions
        // Those exceptions may happen if the underlying pax-logging service
        // bundle has been refreshed somehow.
        answer.setLevel("LOG");
    }
    answer.setMessage(event.getMessage());
    answer.setLogger(event.getLoggerName());
    answer.setTimestamp(new Date(event.getTimeStamp()));
    answer.setThread(event.getThreadName());
    answer.setException(addMavenCoord(event.getThrowableStrRep()));
    Map eventProperties = event.getProperties();
    if (eventProperties != null && eventProperties.size() > 0) {
        Map<String, String> properties = new HashMap<String, String>();
        Set<Map.Entry> set = eventProperties.entrySet();
        for (Map.Entry entry : set) {
            Object key = entry.getKey();
            Object value = entry.getValue();
            if (key != null && value != null) {
                properties.put(toString(key), toString(value));
            }
        }
        MavenCoordinates.addMavenCoord(properties);
        answer.setProperties(properties);
    }
    // event.getFQNOfLoggerClass()
    try {
        PaxLocationInfo locationInformation = event.getLocationInformation();
        if (locationInformation != null) {
            answer.setClassName(locationInformation.getClassName());
            answer.setFileName(locationInformation.getFileName());
            answer.setMethodName(locationInformation.getMethodName());
            answer.setLineNumber(locationInformation.getLineNumber());
        }
    } catch (NoClassDefFoundError error) {
    // ENTESB-2234, KARAF-3350: Ignore NoClassDefFoundError exceptions
    }
    return answer;
}
Also used : LogEvent(io.fabric8.insight.log.LogEvent) HashMap(java.util.HashMap) PaxLocationInfo(org.ops4j.pax.logging.spi.PaxLocationInfo) HashMap(java.util.HashMap) Map(java.util.Map) Date(java.util.Date)

Example 2 with LogEvent

use of io.fabric8.insight.log.LogEvent in project fabric8 by jboss-fuse.

the class Log4jLogQuery method filterLogResults.

protected LogResults filterLogResults(Predicate<LogEvent> predicate, int maxCount) {
    int matched = 0;
    long from = Long.MAX_VALUE;
    long to = Long.MIN_VALUE;
    List<LogEvent> list = new ArrayList<LogEvent>();
    Iterable<LoggingEvent> elements = getEvents().getElements();
    for (LoggingEvent element : elements) {
        LogEvent logEvent = toLogEvent(element);
        long timestamp = element.getTimeStamp();
        if (timestamp > to) {
            to = timestamp;
        }
        if (timestamp < from) {
            from = timestamp;
        }
        if (logEvent != null) {
            if (predicate == null || predicate.matches(logEvent)) {
                list.add(logEvent);
                matched += 1;
                if (maxCount > 0 && matched >= maxCount) {
                    break;
                }
            }
        }
    }
    LogResults results = new LogResults();
    results.setEvents(list);
    if (from < Long.MAX_VALUE) {
        results.setFromTimestamp(from);
    }
    if (to > Long.MIN_VALUE) {
        results.setToTimestamp(to);
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Requested " + maxCount + " logging items. returning " + results.getEvents().size() + " event(s) from a possible " + getEvents().size());
    }
    return results;
}
Also used : LoggingEvent(org.apache.log4j.spi.LoggingEvent) LogResults(io.fabric8.insight.log.LogResults) LogEvent(io.fabric8.insight.log.LogEvent) ArrayList(java.util.ArrayList)

Example 3 with LogEvent

use of io.fabric8.insight.log.LogEvent in project fabric8 by jboss-fuse.

the class Log4jLogQuery method createPredicate.

private Predicate<LogEvent> createPredicate(LogFilter filter) {
    if (filter == null) {
        return null;
    }
    final List<Predicate<LogEvent>> predicates = new ArrayList<Predicate<LogEvent>>();
    final Set<String> levels = filter.getLevelsSet();
    if (levels.size() > 0) {
        predicates.add(new Predicate<LogEvent>() {

            @Override
            public boolean matches(LogEvent event) {
                String level = event.getLevel();
                return level != null && levels.contains(level.toString());
            }
        });
    }
    final Long before = filter.getBeforeTimestamp();
    if (before != null) {
        final Date date = new Date(before);
        predicates.add(new Predicate<LogEvent>() {

            @Override
            public boolean matches(LogEvent event) {
                Date time = event.getTimestamp();
                return time != null && time.before(date);
            }
        });
    }
    final Long after = filter.getAfterTimestamp();
    if (after != null) {
        final Date date = new Date(after);
        predicates.add(new Predicate<LogEvent>() {

            @Override
            public boolean matches(LogEvent event) {
                Date time = event.getTimestamp();
                return time != null && time.after(date);
            }
        });
    }
    final String matchesText = filter.getMatchesText();
    if (matchesText != null && matchesText.length() > 0) {
        predicates.add(new Predicate<LogEvent>() {

            @Override
            public boolean matches(LogEvent event) {
                if (contains(matchesText, event.getClassName(), event.getMessage(), event.getLogger(), event.getThread())) {
                    return true;
                }
                String[] throwableStrRep = event.getException();
                if (throwableStrRep != null && contains(matchesText, throwableStrRep)) {
                    return true;
                }
                Map properties = event.getProperties();
                if (properties != null && contains(matchesText, properties.toString())) {
                    return true;
                }
                return false;
            }
        });
    }
    if (predicates.size() == 0) {
        return null;
    } else if (predicates.size() == 1) {
        return predicates.get(0);
    } else {
        return new Predicate<LogEvent>() {

            @Override
            public String toString() {
                return "AndPredicate" + predicates;
            }

            @Override
            public boolean matches(LogEvent event) {
                for (Predicate<LogEvent> predicate : predicates) {
                    if (!predicate.matches(event)) {
                        return false;
                    }
                }
                return true;
            }
        };
    }
}
Also used : LogEvent(io.fabric8.insight.log.LogEvent) ArrayList(java.util.ArrayList) Map(java.util.Map) Date(java.util.Date) Predicate(io.fabric8.insight.log.support.Predicate)

Example 4 with LogEvent

use of io.fabric8.insight.log.LogEvent in project fabric8 by jboss-fuse.

the class Log4jTest method assertLogQuery.

protected void assertLogQuery(LogQuerySupportMBean mBean) throws Exception {
    LogResults results = mBean.allLogResults();
    List<LogEvent> logEvents = assertNotEmpty(results);
    assertMatches(logEvents, messagePredicate("INFO", info1));
    assertMatches(logEvents, messagePredicate("INFO", info2));
    assertMatches(logEvents, messagePredicate("ERROR", error1));
    assertMatches(logEvents, messagePredicate("ERROR", error2));
    // test a filter
    LogFilter filter = new LogFilter();
    filter.setLevels(new String[] { "ERROR" });
    logEvents = assertNotEmpty(mBean.queryLogResults(filter));
    assertMatches(logEvents, messagePredicate("ERROR", error1));
    assertMatches(logEvents, messagePredicate("ERROR", error2));
    assertNotMatches(logEvents, messagePredicate("INFO", info1));
    assertNotMatches(logEvents, messagePredicate("INFO", info2));
}
Also used : LogResults(io.fabric8.insight.log.LogResults) LogEvent(io.fabric8.insight.log.LogEvent) LogFilter(io.fabric8.insight.log.LogFilter)

Example 5 with LogEvent

use of io.fabric8.insight.log.LogEvent in project fabric8 by jboss-fuse.

the class Log4jLogQuery method toLogEvent.

protected LogEvent toLogEvent(LoggingEvent element) {
    LogEvent answer = new LogEvent();
    answer.setClassName(element.getFQNOfLoggerClass());
    // TODO
    // answer.setContainerName(element.get);
    ThrowableInformation throwableInformation = element.getThrowableInformation();
    if (throwableInformation != null) {
        ThrowableFormatter renderer = new ThrowableFormatter();
        String[] stack = renderer.doRender(throwableInformation.getThrowable());
        if (stack == null) {
            stack = element.getThrowableStrRep();
        }
        answer.setException(stack);
    }
    LocationInfo locationInformation = element.getLocationInformation();
    if (locationInformation != null) {
        answer.setFileName(locationInformation.getFileName());
        answer.setClassName(locationInformation.getClassName());
        answer.setMethodName(locationInformation.getMethodName());
        answer.setLineNumber(locationInformation.getLineNumber());
    }
    Level level = element.getLevel();
    if (level != null) {
        answer.setLevel(level.toString());
    }
    // TODO
    answer.setLogger(element.getLoggerName());
    Category logger = element.getLogger();
    Object message = element.getMessage();
    if (message != null) {
        // TODO marshal differently?
        answer.setMessage(message.toString());
    }
    answer.setProperties(element.getProperties());
    // TODO
    answer.setSeq(element.getTimeStamp());
    answer.setTimestamp(new Date(element.getTimeStamp()));
    answer.setThread(element.getThreadName());
    answer.setHost(getHostName());
    return answer;
}
Also used : Category(org.apache.log4j.Category) LogEvent(io.fabric8.insight.log.LogEvent) ThrowableInformation(org.apache.log4j.spi.ThrowableInformation) Level(org.apache.log4j.Level) Date(java.util.Date) LocationInfo(org.apache.log4j.spi.LocationInfo)

Aggregations

LogEvent (io.fabric8.insight.log.LogEvent)6 LogResults (io.fabric8.insight.log.LogResults)3 Date (java.util.Date)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 LogFilter (io.fabric8.insight.log.LogFilter)1 Predicate (io.fabric8.insight.log.support.Predicate)1 HashMap (java.util.HashMap)1 Category (org.apache.log4j.Category)1 Level (org.apache.log4j.Level)1 LocationInfo (org.apache.log4j.spi.LocationInfo)1 LoggingEvent (org.apache.log4j.spi.LoggingEvent)1 ThrowableInformation (org.apache.log4j.spi.ThrowableInformation)1 Test (org.junit.Test)1 PaxLocationInfo (org.ops4j.pax.logging.spi.PaxLocationInfo)1 Logger (org.slf4j.Logger)1