日志维度分析以及自动邮件发送

今天要统计下各种维度下的数据,访问量等等,写了几个方法,实现了维度分析,并将返回结果自动发送邮件到自定义邮箱

主要实现类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
public static void main(String[] args) {
try{

List<trackVO> tlist = new ArrayList<trackVO>();
List<String> list = ReadFile("/Users/jeyy/Documents/test_pc.access.log.2016-04-25");
//System.out.println(JsonContext);
for (int i = 0; i < list.size(); i++) {

//System.out.println(list.get(i));
trackVO model = JsonUtil.jsonUtil.jsonToModel(list.get(i), trackVO.class);
tlist.add(model);


}


//实现统计逻辑

Map<String, Integer> m = getCount(tlist,"pageUrl");


//打印统计结果|发送邮件
String rs = printMap(m);


EmailUtils.emailUtil.analyseLog("test@qq.com", rs);



}catch(Exception e){
e.printStackTrace();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/** * 读取文件返回string

* @param Path
* @return
*/
public static List<String> ReadFile(String Path){
File file = new File(Path);
List<String> list = new ArrayList<String>();
Scanner scanner = null;
try {
scanner = new Scanner(file, "utf-8");
while (scanner.hasNextLine()) {
StringBuilder buffer = new StringBuilder();
buffer.append(scanner.nextLine());
list.add(buffer.toString());
}

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block

} finally {
if (scanner != null) {
scanner.close();
}
}

return list;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/** * 实现统计逻辑

* @param tlist
*/
public static Map<String,Integer> getCount(List<trackVO> tlist,String column) {
Map<String,Integer> m = new HashMap<String,Integer>();
//用word初使化m,m中包含了所有不重复的单词
for(int i=0;i<tlist.size();i++){
String word = "";
word = caseColumn(tlist, column, i, word);
m.put(word,0);
}
Set<String> set = m.keySet();
//用word中的每个单词与m中的单词比较,发现相同的就统计一次
for(int i=0;i<tlist.size();i++){
String word = "";
word = caseColumn(tlist, column, i, word);
Iterator<String> it = set.iterator();
while(it.hasNext()){
String k = it.next();
if(word.equals(k)){
int c = m.get(k);
c++;
m.put(word,c);
}
}
}
m = sortMap(m);

//System.out.println(m);
return m;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/** * 根据column判断字段的取值

* @param tlist
* @param column
* @param i
* @param word
* @return
*/
public static String caseColumn(List<trackVO> tlist, String column, int i, String word) {
if("cookieId".equals(column)){
word = tlist.get(i).getCookieId();
}else if("currentTime".equals(column)){
word = tlist.get(i).getCurrentTime();
}else if("from".equals(column)){
word = tlist.get(i).getFrom();
}else if("ip".equals(column)){
word = tlist.get(i).getIp();
}else if("moudle".equals(column)){
word = tlist.get(i).getMoudle();
}else if("pageUrl".equals(column)){
word = tlist.get(i).getPageUrl();
}else if("spider".equals(column)){
word = tlist.get(i).getSpider();
}else if("userId".equals(column)){
word = tlist.get(i).getUserId();
}
return word;
}
----


//Map根据value排序
public static Map<String, Integer> sortMap(Map<String, Integer> map) {
List<Map.Entry<String, Integer>> list = new LinkedList<Map.Entry<String, Integer>>(map.entrySet());
Comparator<Map.Entry<String, Integer>> c = new Comparator<Map.Entry<String, Integer>>() {
public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
return -(o1.getValue() - o2.getValue()); //倒序排列
}
};
Collections.sort(list, c);
Map<String, Integer> result = new LinkedHashMap<String, Integer>();

for (Iterator<Entry<String, Integer>> it = list.iterator(); it.hasNext();) {
Map.Entry<String, Integer> entry = (Map.Entry<String, Integer>) it.next();
result.put(entry.getKey(), entry.getValue());
}
return result;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//遍历Map并且返回排版好的String字符串

public static String printMap(Map<String, Integer> map) {
StringBuilder sb = new StringBuilder();
sb.append("<html><body><p style=\"margin-left: 30px;\"><font size=\"5\" color=\"rgb(216,206,178)\" >日志分析</font></p>");

sb.append("<p style=\"background-color:rgb(163,210,202);margin-left: 30px;\">");
sb.append("统计"+"||||"+"次<br>");
sb.append("<br>");
Iterator<Map.Entry<String, Integer>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, Integer> entry = iterator.next();
sb.append(entry.getKey()+"||||"+entry.getValue().toString());
sb.append("<br>");
//System.out.println(entry.getKey() + "---->" + entry.getValue().toString());
}
sb.append("----analysed by bruce----<br/><br/>" + "</p>" + "</body></html>");

return sb.toString();
}

辅助实现方法

trackVO

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
public class trackVO {
public String cookieId ; // :9f7c9520f094e534c3313bf92405a56b
public String currentTime ; // :1461599856952
public String from ; // :
public String ip ; // :111.206.241.76
public String moudle ; // :web_access
public String pageUrl ; // :www.test.com/
public String spider ; // :false
public String userId ; // :

public String getCookieId() {
return cookieId;
}
public void setCookieId(String cookieId) {
this.cookieId = cookieId;
}
public String getCurrentTime() {
return currentTime;
}
public void setCurrentTime(String currentTime) {
this.currentTime = currentTime;
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public String getMoudle() {
return moudle;
}
public void setMoudle(String moudle) {
this.moudle = moudle;
}
public String getPageUrl() {
return pageUrl;
}
public void setPageUrl(String pageUrl) {
this.pageUrl = pageUrl;
}
public String getSpider() {
return spider;
}
public void setSpider(String spider) {
this.spider = spider;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}

EmailUtils

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
public class EmailUtils {
private static Logger log =LoggerFactory.getLogger(EmailUtils.class);
public static final EmailUtils emailUtil = new EmailUtils();

/**
日志分析auto发送

@param toEmail
@param usrId
@param authCode
*/
public static void analyseLog(String toEmail,String cont) {
try {

String host = "smtp.163.com";
String myEmail = "test@163.com";
String myName = "小布";
String myPassword = Base64Util.JIEMI("MTM0ODM3MjQwNTEwMDAwMDA=");
// 接收者邮箱
String to = toEmail;
String subject = "日志分析";
String mailConent = "日志分析";

HtmlEmail email = new HtmlEmail();
email.setAuthentication(myEmail, myPassword);
email.setHostName(host);
email.addTo(to, "亲");
email.setFrom(myEmail);
email.setSubject(subject);
// 注意,发送内容时,后面这段会让中文正常显示,否则乱码
email.setCharset("utf-8");
email.setHtmlMsg(cont); /* 邮件内容 */
// 添加附件对象
// email.attach(attachment);
// 发送
email.send();
System.err.println("邮件发送成功");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
System.err.println("邮件发送失败");
}

}
}

实现效果

enter description here

生活不止苟且,还有我喜爱的海岸.