OpLogger.java
1.85 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package com.bsth.oplog.normal;
import java.net.InetAddress;
import java.net.UnknownHostException;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.bsth.entity.sys.SysUser;
import com.bsth.oplog.Log;
import com.bsth.oplog.db.DBHelper;
import com.bsth.security.util.SecurityUtils;
import com.bsth.util.IpUtils;
/**
*
* @ClassName: OpLogger
* @Description: TODO(常规的操作日志记录器)
* @author PanZhao
* @date 2016年10月21日 上午12:52:30
*
*/
@Component
public class OpLogger {
@Autowired
DBHelper dbHelper;
public void info(String path){
save(getInitLog(path));
}
public void info(String path, HttpServletRequest request){
Log log = getInitLog(path);
if(request != null){
SysUser user = SecurityUtils.getCurrentUser();
if(user != null)
log.setUserName(user.getUserName());
log.setClientIp(IpUtils.getIpAddr(request));
}
save(log);
}
public void info(String path, HttpServletRequest request, String description){
}
public static Log getInitLog(String path){
Log log = new Log();
log.setTimestamp(System.currentTimeMillis());
log.setPath(path);
log.setType("NORMAL");
//get caller
StackTraceElement[] stack = (new Throwable()).getStackTrace();
//注意调用链的顺序,此时2为实际调用者
StackTraceElement caller = stack[2];
log.setCallerClass(caller.getClassName());
log.setCallerMethod(caller.getMethodName());
log.setLineNumber(caller.getLineNumber());
try {
log.setServerIp(InetAddress.getLocalHost().getHostAddress());
} catch (UnknownHostException e) {
e.printStackTrace();
}
return log;
}
public void save(Log log){
dbHelper.save(log);
}
}