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

View File

@ -1,5 +1,7 @@
package com.sismics.util.log4j; package com.sismics.util.log4j;
import org.apache.log4j.Level;
/** /**
* Log entry. * Log entry.
* *
@ -14,7 +16,7 @@ public class LogEntry {
/** /**
* Logging level (DEBUG, WARN)... * Logging level (DEBUG, WARN)...
*/ */
private String level; private Level level;
/** /**
* Logger name / tag. * Logger name / tag.
@ -34,46 +36,37 @@ public class LogEntry {
* @param tag Logger name / tag * @param tag Logger name / tag
* @param message Message logged * @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.timestamp = timestamp;
this.level = level; this.level = level;
this.tag = tag; this.tag = tag;
this.message = message; this.message = message;
} }
/**
* Getter of timestamp.
*
* @return timestamp
*/
public long getTimestamp() { public long getTimestamp() {
return timestamp; return timestamp;
} }
/** public Level getLevel() {
* Getter of level.
*
* @return level
*/
public String getLevel() {
return level; return level;
} }
/**
* Getter of tag.
*
* @return tag
*/
public String getTag() { public String getTag() {
return tag; return tag;
} }
/**
* Getter of message.
*
* @return message
*/
public String getMessage() { public String getMessage() {
return message; 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; 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.Iterator;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Queue; import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue; 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. * Memory appender for Log4J.
* *
@ -54,7 +56,7 @@ public class MemoryAppender extends AppenderSkeleton {
String loggerName = getLoggerName(event); 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); logQueue.add(logEntry);
} }
@ -98,13 +100,13 @@ public class MemoryAppender extends AppenderSkeleton {
*/ */
public void find(LogCriteria criteria, PaginatedList<LogEntry> list) { public void find(LogCriteria criteria, PaginatedList<LogEntry> list) {
List<LogEntry> logEntryList = new LinkedList<LogEntry>(); List<LogEntry> logEntryList = new LinkedList<LogEntry>();
final String level = criteria.getLevel(); final Level minLevel = criteria.getMinLevel();
final String tag = criteria.getTag(); final String tag = criteria.getTag();
final String message = criteria.getMessage(); final String message = criteria.getMessage();
int resultCount = 0; int resultCount = 0;
for (Iterator<LogEntry> it = logQueue.iterator(); it.hasNext();) { for (Iterator<LogEntry> it = logQueue.iterator(); it.hasNext();) {
LogEntry logEntry = it.next(); 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)) && (tag == null || logEntry.getTag().toLowerCase().equals(tag)) &&
(message == null || logEntry.getMessage().toLowerCase().contains(message))) { (message == null || logEntry.getMessage().toLowerCase().contains(message))) {
logEntryList.add(logEntry); logEntryList.add(logEntry);

View File

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