Search logs by min level instead of exact level

This commit is contained in:
jendib 2016-03-13 23:13:12 +01:00
parent d58b0e8f74
commit 0dda01269f
4 changed files with 47 additions and 77 deletions

View File

@ -1,6 +1,7 @@
package com.sismics.util.log4j;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Level;
/**
* Log search criteria.
@ -10,9 +11,9 @@ import org.apache.commons.lang.StringUtils;
public class LogCriteria {
/**
* Logging level (DEBUG, WARN)...
* Minimum logging level (DEBUG, WARN)...
*/
private String level;
private Level minLevel;
/**
* Logger name / tag.
@ -24,57 +25,30 @@ public class LogCriteria {
*/
private String message;
/**
* Getter of level.
*
* @return level
*/
public String getLevel() {
return level;
public Level getMinLevel() {
return minLevel;
}
/**
* Setter of level.
*
* @param level level
*/
public void setLevel(String level) {
this.level = StringUtils.lowerCase(level);
public LogCriteria setMinLevel(Level level) {
this.minLevel = level;
return this;
}
/**
* Getter of tag.
*
* @return tag
*/
public String getTag() {
return tag;
}
/**
* Setter of tag.
*
* @param tag tag
*/
public void setTag(String tag) {
public LogCriteria setTag(String tag) {
this.tag = StringUtils.lowerCase(tag);
return this;
}
/**
* Getter of message.
*
* @return message
*/
public String getMessage() {
return message;
}
/**
* Setter of message.
*
* @param message message
*/
public void setMessage(String message) {
public LogCriteria setMessage(String message) {
this.message = StringUtils.lowerCase(message);
return this;
}
}

View File

@ -1,5 +1,7 @@
package com.sismics.util.log4j;
import org.apache.log4j.Level;
/**
* Log entry.
*
@ -14,7 +16,7 @@ public class LogEntry {
/**
* Logging level (DEBUG, WARN)...
*/
private String level;
private Level level;
/**
* Logger name / tag.
@ -34,46 +36,37 @@ public class LogEntry {
* @param tag Logger name / tag
* @param message Message logged
*/
public LogEntry(long timestamp, String level, String tag, String message) {
public LogEntry(long timestamp, Level level, String tag, String message) {
this.timestamp = timestamp;
this.level = level;
this.tag = tag;
this.message = message;
}
/**
* Getter of timestamp.
*
* @return timestamp
*/
public long getTimestamp() {
return timestamp;
}
/**
* Getter of level.
*
* @return level
*/
public String getLevel() {
public Level getLevel() {
return level;
}
/**
* Getter of tag.
*
* @return tag
*/
public String getTag() {
return tag;
}
/**
* Getter of message.
*
* @return message
*/
public String getMessage() {
return message;
}
/**
* Compare the current log level with the provided one.
*
* @param level2 Comparison level
* @return 1 if level is greater than level2, -1 if level is lower than level2, and 0 if level equals to level2
*/
public int compareLevel(String level2) {
// TODO Auto-generated method stub
return 0;
}
}

View File

@ -1,17 +1,19 @@
package com.sismics.util.log4j;
import com.google.common.collect.Lists;
import com.sismics.docs.core.util.jpa.PaginatedList;
import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.helpers.LogLog;
import org.apache.log4j.spi.LoggingEvent;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.Level;
import org.apache.log4j.helpers.LogLog;
import org.apache.log4j.spi.LoggingEvent;
import com.google.common.collect.Lists;
import com.sismics.docs.core.util.jpa.PaginatedList;
/**
* Memory appender for Log4J.
*
@ -54,7 +56,7 @@ public class MemoryAppender extends AppenderSkeleton {
String loggerName = getLoggerName(event);
LogEntry logEntry = new LogEntry(System.currentTimeMillis(), event.getLevel().toString(), loggerName, event.getMessage().toString());
LogEntry logEntry = new LogEntry(System.currentTimeMillis(), event.getLevel(), loggerName, event.getMessage().toString());
logQueue.add(logEntry);
}
@ -98,13 +100,13 @@ public class MemoryAppender extends AppenderSkeleton {
*/
public void find(LogCriteria criteria, PaginatedList<LogEntry> list) {
List<LogEntry> logEntryList = new LinkedList<LogEntry>();
final String level = criteria.getLevel();
final Level minLevel = criteria.getMinLevel();
final String tag = criteria.getTag();
final String message = criteria.getMessage();
int resultCount = 0;
for (Iterator<LogEntry> it = logQueue.iterator(); it.hasNext();) {
LogEntry logEntry = it.next();
if ((level == null || logEntry.getLevel().toLowerCase().equals(level)) &&
if ((minLevel == null || Integer.compare(logEntry.getLevel().toInt(), minLevel.toInt()) >= 0) &&
(tag == null || logEntry.getTag().toLowerCase().equals(tag)) &&
(message == null || logEntry.getMessage().toLowerCase().contains(message))) {
logEntryList.add(logEntry);

View File

@ -22,6 +22,7 @@ import javax.ws.rs.core.Response;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Appender;
import org.apache.log4j.Level;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -81,7 +82,7 @@ public class AppResource extends BaseResource {
/**
* Retrieve the application logs.
*
* @param level Filter on logging level
* @param minLevel Filter on logging level
* @param tag Filter on logger name / tag
* @param message Filter on message
* @param limit Page limit
@ -91,7 +92,7 @@ public class AppResource extends BaseResource {
@GET
@Path("log")
public Response log(
@QueryParam("level") String level,
@QueryParam("level") String minLevel,
@QueryParam("tag") String tag,
@QueryParam("message") String message,
@QueryParam("limit") Integer limit,
@ -110,10 +111,10 @@ public class AppResource extends BaseResource {
MemoryAppender memoryAppender = (MemoryAppender) appender;
// Find the logs
LogCriteria logCriteria = new LogCriteria();
logCriteria.setLevel(StringUtils.stripToNull(level));
logCriteria.setTag(StringUtils.stripToNull(tag));
logCriteria.setMessage(StringUtils.stripToNull(message));
LogCriteria logCriteria = new LogCriteria()
.setMinLevel(Level.toLevel(StringUtils.stripToNull(minLevel)))
.setTag(StringUtils.stripToNull(tag))
.setMessage(StringUtils.stripToNull(message));
PaginatedList<LogEntry> paginatedList = PaginatedLists.create(limit, offset);
memoryAppender.find(logCriteria, paginatedList);
@ -121,7 +122,7 @@ public class AppResource extends BaseResource {
for (LogEntry logEntry : paginatedList.getResultList()) {
logs.add(Json.createObjectBuilder()
.add("date", logEntry.getTimestamp())
.add("level", logEntry.getLevel())
.add("level", logEntry.getLevel().toString())
.add("tag", logEntry.getTag())
.add("message", logEntry.getMessage()));
}