Reputation: 23
I am trying to filter a log according to the type of log. This is an example of a log
--------- beginning of main
04-30 01:16:36.307 458 458 I ADB_SERVICES: serv
04-30 01:16:36.363 458 458 I ADB_SERVICES: service_to_fd
04-30 01:16:36.371 6181 6181 W sh : type=140 ino=2 scon
04-30 01:16:36.401 6181 6181 W getprop : type=10 audit(0.0:36): avc: denied
04-30 01:16:36.433 458 6182 I ADB_SERVICES: post waitpid (
04-30 01:16:36.454 458 458 I ADB_SERVICES: service_to_fd
in this example for example I Want to filter only the String line that contains the letter W after the numbers and printed.
This is how I print the log.
List<LogEntry> logEntries = sytemLogs.getLogs();
for (LogEntry entry : sysLogEntries) {
System.out.println(sysLogEntries.getMessage());
}
Upvotes: 1
Views: 102
Reputation: 3728
get a list with the corresponding messages in lambda
List<LogEntry> list = logEntries.stream()
.filter(l -> Pattern.matches("[^A-Z]*W.*", l.getMessage())).collect(toList());
if runtime matters extract the regex as compiled Pattern
Upvotes: 1
Reputation: 385
Regular expressions can help here.
Here is a regex that can be used. Read the logs line by line and using this regex perform further operations.
public void processLogs() {
String regex = "[\\d] +[W]{1} .*\\b";
Pattern pattern = Pattern.compile(regex);
List<LogEntry> logEntries = sytemLogs.getLogs();
for (LogEntry entry : logEntries) {
String line = entry.getMessage();
System.out.println(line);
Matcher matcher = pattern.matcher(line);
boolean matchFound = matcher.find();
if (matchFound) {
System.out.println("Match found");
//Other operations
}
}
}
Upvotes: 0
Reputation: 9284
Assuming that all the logs contain the equal count of numbers before the character W, say 32, this can be done as follows.
List<LogEntry> logEntries = systemLogs.getLogs();
for(LogEntry entry: logEntries){
int index = entry.getMessage().indexOf("W");
if(index == 32){
System.out.println(entry.getMessage());
}
}
Upvotes: 1