Skip to content

Instantly share code, notes, and snippets.

@willianantunes
Last active June 1, 2017 11:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save willianantunes/0754f4afa3833b6b2119a2033f185fdc to your computer and use it in GitHub Desktop.
Save willianantunes/0754f4afa3833b6b2119a2033f185fdc to your computer and use it in GitHub Desktop.
Class used for my blog post
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.LocalDate;
import java.time.Month;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class MyFunctionalPostBlog {
private static String myAddressDirectory = "/tmp/my-logs/";
private static String myAddressSingleFile = "/tmp/my-logs/my-horcrux-app-2017-05-31.log";
private static Pattern myPattern = Pattern.compile(".*\\[Horcrux\\]");
private static DateTimeFormatter myFormat = DateTimeFormatter.ofPattern("dd MMM yyyy");
public static void main(String args[]) throws Exception {
// Step one and two
Map<LocalDate, List<String>> myByLocalDateMap = Files.lines(Paths.get(myAddressSingleFile)).filter(line -> {
try {
LocalDate myLocalDate = LocalDate.parse(line.substring(0, 6) + " 2017" , myFormat);
return line.contains("[Horcrux]") && line.contains(" ERROR ") && myLocalDate.isAfter(LocalDate.of(2017, Month.APRIL, 19));
} catch (Exception e) {
return false;
}
}).collect(Collectors.groupingBy(l -> {
return LocalDate.parse(l.substring(0, 6) + " 2017" , myFormat);
}, TreeMap::new, Collectors.toList()));
// Step three
myByLocalDateMap.keySet().stream().forEach(localDate -> {
System.out.println("#### Details regarding " + localDate);
Map<String, List<String>> myLinesByErrors = groupByErrors(myByLocalDateMap, localDate);
myLinesByErrors.keySet().stream().forEach(exceptionType -> {
System.out.println(String.format("\t- Count for \"%s\": %s", exceptionType, myLinesByErrors.get(exceptionType).size()));
});
// Step four
myLinesByErrors.keySet().stream().flatMapToInt(exceptionType -> IntStream.of(myLinesByErrors.get(exceptionType).size()))
.average().ifPresent(average -> System.out.println("\t- The average is " + average));
});
// If you want to read each log file contained in the folder
Files.list(Paths.get(myAddressDirectory)).forEach(eachLog -> {
// Your implementation here! :)
});
}
private static Map<String, List<String>> groupByErrors(Map<LocalDate, List<String>> byLocalDateMap, LocalDate localDate) {
return byLocalDateMap.get(localDate).stream().collect(Collectors.groupingBy(s -> {
Matcher m = myPattern.matcher(s);
m.find();
return s.substring(m.end()+1, s.length());
}));
}
public static class BlogExplanationOnly {
public void stepOne() throws IOException {
String myAddress = "/tmp/my-logs/my-horcrux-app-2017-05-31.log";
DateTimeFormatter myFormat = DateTimeFormatter.ofPattern("dd MMM yyyy");
Files.lines(Paths.get(myAddress)).filter(line -> {
try {
LocalDate myLocalDate = LocalDate.parse(line.substring(0, 6) + " 2017" , myFormat);
return line.contains("[Horcrux]") && line.contains(" ERROR ") && myLocalDate.isAfter(LocalDate.of(2017, Month.APRIL, 19));
} catch (Exception e) {
return false;
}
});
}
public Map<LocalDate, List<String>> stepTwoIncomplete() throws IOException {
String myAddress = "/tmp/my-logs/my-horcrux-app-2017-05-31.log";
DateTimeFormatter myFormat = DateTimeFormatter.ofPattern("dd MMM yyyy");
return Files.lines(Paths.get(myAddress)).filter(line -> {
try {
LocalDate myLocalDate = LocalDate.parse(line.substring(0, 6) + " 2017" , myFormat);
return line.contains("[Horcrux]") && line.contains(" ERROR ") && myLocalDate.isAfter(LocalDate.of(2017, Month.APRIL, 19));
} catch (Exception e) {
return false;
}
}).collect(Collectors.groupingBy(l -> {
return LocalDate.parse(l.substring(0, 6) + " 2017" , myFormat);
}));
}
public Map<LocalDate, List<String>> stepTwoComplete() throws IOException {
String myAddress = "/tmp/my-logs/my-horcrux-app-2017-05-31.log";
DateTimeFormatter myFormat = DateTimeFormatter.ofPattern("dd MMM yyyy");
return Files.lines(Paths.get(myAddress)).filter(line -> {
try {
LocalDate myLocalDate = LocalDate.parse(line.substring(0, 6) + " 2017" , myFormat);
return line.contains("[Horcrux]") && line.contains(" ERROR ") && myLocalDate.isAfter(LocalDate.of(2017, Month.APRIL, 19));
} catch (Exception e) {
return false;
}
}).collect(Collectors.groupingBy(l -> {
return LocalDate.parse(l.substring(0, 6) + " 2017" , myFormat);
}, TreeMap::new, Collectors.toList()));
}
public void stepThree(Map<LocalDate, List<String>> myMapFromStepTwoComplete) {
Pattern myPattern = Pattern.compile(".*\\[Horcrux\\]");
myMapFromStepTwoComplete.keySet().stream()
.forEach(k -> {
Map<String, List<String>> myGroupedErrors = myMapFromStepTwoComplete.get(k).stream().collect(Collectors.groupingBy(s -> {
Matcher m = myPattern.matcher(s);
m.find();
return s.substring(m.end()+1, s.length());
}));
});
}
public void stepFour(Map<String, List<String>> myMapFromStepThree) {
myMapFromStepThree.keySet().stream().flatMapToInt(exceptionType -> IntStream.of(myMapFromStepThree.get(exceptionType).size()))
.average().ifPresent(average -> System.out.println("The average is " + average));
}
}
/*
* Sample log file:
29 May 22:02:49,096 ERROR [3035] [Horcrux] org.hibernate.exception.GenericJDBCException: Could not open connection
29 May 23:03:49,096 ERROR [3035] [Horcrux] org.hibernate.exception.GenericJDBCException: Could not open connection
29 May 23:04:49,096 ERROR [3035] [Horcrux] org.hibernate.exception.GenericJDBCException: Could not open connection
29 May 23:11:49,096 ERROR [3035] [Horcrux] org.hibernate.exception.GenericJDBCException: Could not open connection
29 May 23:12:49,096 ERROR [3035] [Horcrux] org.hibernate.exception.GenericJDBCException: Could not open connection
29 May 23:13:49,096 ERROR [3035] [Horcrux] org.hibernate.exception.GenericJDBCException: Could not open connection
29 May 23:14:49,096 ERROR [3035] [Horcrux] java.net.ConnectException
29 May 23:14:51,096 ERROR [3035] [Horcrux] java.net.ConnectException
29 May 23:14:52,096 ERROR [3035] [Horcrux] java.net.ConnectException
29 May 23:14:53,096 ERROR [3035] [Horcrux] java.net.ConnectException
29 May 23:14:54,096 ERROR [3035] [Horcrux] java.net.ConnectException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
30 May 03:14:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsBudegaException
31 May 04:14:54,096 ERROR [3035] [Horcrux] java.net.ConnectException
31 May 04:14:54,096 ERROR [3035] [Horcrux] java.net.ConnectException
31 May 04:14:54,096 ERROR [3035] [Horcrux] java.net.ConnectException
31 May 04:14:54,096 ERROR [3035] [Horcrux] java.net.ConnectException
31 May 04:14:54,096 ERROR [3035] [Horcrux] java.net.ConnectException
31 May 04:14:54,096 ERROR [3035] [Horcrux] java.net.ConnectException
31 May 04:14:54,096 ERROR [3035] [Horcrux] java.net.ConnectException
31 May 04:14:54,096 ERROR [3035] [Horcrux] java.net.ConnectException
31 May 04:14:54,096 ERROR [3035] [Horcrux] java.net.ConnectException
31 May 04:15:54,096 ERROR [3035] [Horcrux] org.hibernate.exception.GenericJDBCException: Could not open connection
31 May 04:15:54,096 ERROR [3035] [Horcrux] org.hibernate.exception.GenericJDBCException: Could not open connection
31 May 04:15:54,096 ERROR [3035] [Horcrux] org.hibernate.exception.GenericJDBCException: Could not open connection
31 May 04:15:54,096 ERROR [3035] [Horcrux] org.hibernate.exception.GenericJDBCException: Could not open connection
31 May 04:15:54,096 ERROR [3035] [Horcrux] org.hibernate.exception.GenericJDBCException: Could not open connection
31 May 04:15:54,096 ERROR [3035] [Horcrux] org.hibernate.exception.GenericJDBCException: Could not open connection
31 May 04:15:54,096 ERROR [3035] [Horcrux] org.hibernate.exception.GenericJDBCException: Could not open connection
31 May 04:15:54,096 ERROR [3035] [Horcrux] org.hibernate.exception.GenericJDBCException: Could not open connection
31 May 04:15:54,096 ERROR [3035] [Horcrux] org.hibernate.exception.GenericJDBCException: Could not open connection
31 May 11:31:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsNotBudegaThisIsAbudegadoException
31 May 11:31:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsNotBudegaThisIsAbudegadoException
31 May 11:31:54,096 ERROR [3035] [Horcrux] br.com.willianantunes.exception.ThisIsNotBudegaThisIsAbudegadoException
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment