Mercurial > hg > Members > anatofuz > Perl6_log_analyze_example
comparison java/src/main/java/com/google/anatofuz/LogAnalyzer.java @ 0:b42d5cbbd972
add
author | Takahiro SHIMIZU <anatofuz@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Wed, 18 Jul 2018 17:25:20 +0900 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:b42d5cbbd972 |
---|---|
1 package com.google.anatofuz; | |
2 | |
3 import java.io.File; | |
4 import java.io.FileReader; | |
5 import java.io.BufferedReader; | |
6 import java.io.FileNotFoundException; | |
7 import java.io.IOException; | |
8 import java.util.*; | |
9 import java.util.regex.Pattern; | |
10 import java.util.regex.Matcher; | |
11 | |
12 public class LogAnalyzer { | |
13 | |
14 public static void main(String args[]) { | |
15 | |
16 File file = new File("/var/log/system.log"); | |
17 | |
18 if (args.length != 0) { | |
19 if (args[0].equals("-f")) { | |
20 file = new File(args[1]); | |
21 } | |
22 } | |
23 | |
24 try { | |
25 FileReader filereader = new FileReader(file); | |
26 BufferedReader bufferedReader = new BufferedReader(filereader); | |
27 | |
28 String line; | |
29 Map<String,Integer> map = new HashMap<String,Integer>(0); | |
30 Pattern p = Pattern.compile("\\w+ \\d{0,2} (?:\\d{2}:?){3} (?:anatofuzMBP|anatofuz-15) ([\\w.]+)\\[\\d+\\]"); | |
31 | |
32 | |
33 while ((line = bufferedReader.readLine()) != null) { | |
34 Matcher matcher = p.matcher(line); | |
35 if (matcher.find()) { | |
36 map.merge(matcher.group(1),1,Integer::sum); | |
37 } | |
38 } | |
39 | |
40 int sum = 0; | |
41 | |
42 for (String key :map.keySet()){ | |
43 sum += map.get(key); | |
44 } | |
45 | |
46 System.out.println(sum); | |
47 | |
48 | |
49 } catch (FileNotFoundException ex){ | |
50 System.out.println(ex); | |
51 } catch (IOException ex){ | |
52 System.out.println(ex); | |
53 } | |
54 } | |
55 } |