提交 | 用户 | 时间
|
1ac2bc
|
1 |
/** |
懒 |
2 |
* Copyright 2018-2020 stylefeng & fengshuonan (https://gitee.com/stylefeng) |
|
3 |
* <p> |
|
4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 |
* you may not use this file except in compliance with the License. |
|
6 |
* You may obtain a copy of the License at |
|
7 |
* <p> |
|
8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
|
9 |
* <p> |
|
10 |
* Unless required by applicable law or agreed to in writing, software |
|
11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
|
12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 |
* See the License for the specific language governing permissions and |
|
14 |
* limitations under the License. |
|
15 |
*/ |
|
16 |
package cn.stylefeng.guns.sys.core.log.factory; |
|
17 |
|
|
18 |
import cn.stylefeng.guns.sys.core.constant.state.LogSucceed; |
|
19 |
import cn.stylefeng.guns.sys.core.constant.state.LogType; |
|
20 |
import cn.stylefeng.guns.sys.core.log.LogManager; |
|
21 |
import cn.stylefeng.guns.sys.modular.system.entity.LoginLog; |
|
22 |
import cn.stylefeng.guns.sys.modular.system.entity.OperationLog; |
|
23 |
import cn.stylefeng.guns.sys.modular.system.mapper.LoginLogMapper; |
|
24 |
import cn.stylefeng.guns.sys.modular.system.mapper.OperationLogMapper; |
|
25 |
import cn.stylefeng.roses.core.util.SpringContextHolder; |
|
26 |
import cn.stylefeng.roses.core.util.ToolUtil; |
|
27 |
import org.slf4j.Logger; |
|
28 |
import org.slf4j.LoggerFactory; |
|
29 |
|
|
30 |
import java.util.TimerTask; |
|
31 |
|
|
32 |
/** |
|
33 |
* 日志操作任务创建工厂 |
|
34 |
* |
|
35 |
* @author fengshuonan |
|
36 |
* @date 2016年12月6日 下午9:18:27 |
|
37 |
*/ |
|
38 |
public class LogTaskFactory { |
|
39 |
|
|
40 |
private static Logger logger = LoggerFactory.getLogger(LogManager.class); |
|
41 |
private static LoginLogMapper loginLogMapper = SpringContextHolder.getBean(LoginLogMapper.class); |
|
42 |
private static OperationLogMapper operationLogMapper = SpringContextHolder.getBean(OperationLogMapper.class); |
|
43 |
|
|
44 |
public static TimerTask loginLog(final Long userId, final String ip) { |
|
45 |
return new TimerTask() { |
|
46 |
@Override |
|
47 |
public void run() { |
|
48 |
try { |
|
49 |
LoginLog loginLog = LogFactory.createLoginLog(LogType.LOGIN, userId, null, ip); |
|
50 |
loginLogMapper.insert(loginLog); |
|
51 |
} catch (Exception e) { |
|
52 |
logger.error("创建登录日志异常!", e); |
|
53 |
} |
|
54 |
} |
|
55 |
}; |
|
56 |
} |
|
57 |
|
|
58 |
public static TimerTask loginLog(final String username, final String msg, final String ip) { |
|
59 |
return new TimerTask() { |
|
60 |
@Override |
|
61 |
public void run() { |
|
62 |
LoginLog loginLog = LogFactory.createLoginLog( |
|
63 |
LogType.LOGIN_FAIL, null, "账号:" + username + "," + msg, ip); |
|
64 |
try { |
|
65 |
loginLogMapper.insert(loginLog); |
|
66 |
} catch (Exception e) { |
|
67 |
logger.error("创建登录失败异常!", e); |
|
68 |
} |
|
69 |
} |
|
70 |
}; |
|
71 |
} |
|
72 |
|
|
73 |
public static TimerTask exitLog(final Long userId, final String ip) { |
|
74 |
return new TimerTask() { |
|
75 |
@Override |
|
76 |
public void run() { |
|
77 |
LoginLog loginLog = LogFactory.createLoginLog(LogType.EXIT, userId, null, ip); |
|
78 |
try { |
|
79 |
loginLogMapper.insert(loginLog); |
|
80 |
} catch (Exception e) { |
|
81 |
logger.error("创建退出日志异常!", e); |
|
82 |
} |
|
83 |
} |
|
84 |
}; |
|
85 |
} |
|
86 |
|
|
87 |
public static TimerTask bussinessLog(final Long userId, final String bussinessName, final String clazzName, final String methodName, final String msg) { |
|
88 |
return new TimerTask() { |
|
89 |
@Override |
|
90 |
public void run() { |
|
91 |
OperationLog operationLog = LogFactory.createOperationLog( |
|
92 |
LogType.BUSSINESS, userId, bussinessName, clazzName, methodName, msg, LogSucceed.SUCCESS); |
|
93 |
try { |
|
94 |
operationLogMapper.insert(operationLog); |
|
95 |
} catch (Exception e) { |
|
96 |
logger.error("创建业务日志异常!", e); |
|
97 |
} |
|
98 |
} |
|
99 |
}; |
|
100 |
} |
|
101 |
|
|
102 |
public static TimerTask exceptionLog(final Long userId, final Throwable exception) { |
|
103 |
return new TimerTask() { |
|
104 |
@Override |
|
105 |
public void run() { |
|
106 |
String msg = ToolUtil.getExceptionMsg(exception); |
|
107 |
OperationLog operationLog = LogFactory.createOperationLog( |
|
108 |
LogType.EXCEPTION, userId, "", null, null, msg, LogSucceed.FAIL); |
|
109 |
try { |
|
110 |
operationLogMapper.insert(operationLog); |
|
111 |
} catch (Exception e) { |
|
112 |
logger.error("创建异常日志异常!", e); |
|
113 |
} |
|
114 |
} |
|
115 |
}; |
|
116 |
} |
|
117 |
} |