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;
}
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;
}
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;
}
};
}
}
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));
}
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;
}
Aggregations