inleft
2022-02-09 9bcb19959eeb9da9bde2561e7278f6d0a55eb151
commit | author | age
9bcb19 1 /*
I 2 Copyright [2020] [https://www.xiaonuo.vip]
3
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
8   http://www.apache.org/licenses/LICENSE-2.0
9
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 Snowy采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点:
17
18 1.请不要删除和修改根目录下的LICENSE文件。
19 2.请不要删除和修改Snowy源码头部的版权声明。
20 3.请保留源码和相关描述文件的项目出处,作者声明等。
21 4.分发源码时候,请注明软件出处 https://gitee.com/xiaonuobase/snowy
22 5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/xiaonuobase/snowy
23 6.若您的项目无法满足以上几点,可申请商业授权,获取Snowy商业授权许可,请在官网购买授权,地址为 https://www.xiaonuo.vip
24  */
25 package vip.xiaonuo.sys.core.log;
26
27 import cn.hutool.core.util.ObjectUtil;
28 import org.aspectj.lang.JoinPoint;
29 import org.springframework.scheduling.concurrent.ScheduledExecutorFactoryBean;
30 import vip.xiaonuo.core.annotion.BusinessLog;
31 import vip.xiaonuo.core.context.constant.ConstantContextHolder;
32 import vip.xiaonuo.core.exception.ServiceException;
33 import vip.xiaonuo.core.exception.enums.ServerExceptionEnum;
34 import vip.xiaonuo.core.util.HttpServletUtil;
35 import vip.xiaonuo.core.util.IpAddressUtil;
36 import vip.xiaonuo.core.util.UaUtil;
37 import vip.xiaonuo.sys.core.log.factory.LogFactory;
38 import vip.xiaonuo.sys.core.log.factory.LogTaskFactory;
39 import vip.xiaonuo.sys.modular.log.entity.SysOpLog;
40 import vip.xiaonuo.sys.modular.log.entity.SysVisLog;
41
42 import javax.servlet.http.HttpServletRequest;
43 import java.util.TimerTask;
44 import java.util.concurrent.ScheduledThreadPoolExecutor;
45 import java.util.concurrent.TimeUnit;
46
47
48 /**
49  * 日志管理器
50  *
51  * @author xuyuxiang
52  * @date 2020/3/12 14:13
53  */
54 public class LogManager {
55
56     /**
57      * 异步操作记录日志的线程池
58      */
59     private static final ScheduledThreadPoolExecutor EXECUTOR = new ScheduledThreadPoolExecutor(10, new ScheduledExecutorFactoryBean());
60
61     private LogManager() {
62     }
63
64     private static final LogManager LOG_MANAGER = new LogManager();
65
66     public static LogManager me() {
67         return LOG_MANAGER;
68     }
69
70     /**
71      * 异步执行日志的方法
72      *
73      * @author xuyuxiang
74      * @date 2020/4/8 19:19
75      */
76     private void executeLog(TimerTask task) {
77
78         //如果演示模式开启,则不记录日志
79         if (ConstantContextHolder.getDemoEnvFlag()) {
80             return;
81         }
82
83         //日志记录操作延时
84         int operateDelayTime = 10;
85         EXECUTOR.schedule(task, operateDelayTime, TimeUnit.MILLISECONDS);
86     }
87
88     /**
89      * 登录日志
90      *
91      * @author xuyuxiang
92      * @date 2020/3/18 20:00
93      */
94     public void executeLoginLog(final String account, final String success, final String failMessage) {
95         SysVisLog sysVisLog = this.genBaseSysVisLog();
96         TimerTask timerTask = LogTaskFactory.loginLog(sysVisLog, account,
97                 success,
98                 failMessage);
99         executeLog(timerTask);
100     }
101
102     /**
103      * 登出日志
104      *
105      * @author xuyuxiang
106      * @date 2020/3/18 20:01
107      */
108     public void executeExitLog(final String account) {
109         SysVisLog sysVisLog = this.genBaseSysVisLog();
110         TimerTask timerTask = LogTaskFactory.exitLog(sysVisLog, account);
111         executeLog(timerTask);
112     }
113
114     /**
115      * 操作日志
116      *
117      * @author xuyuxiang
118      * @date 2020/3/18 20:01
119      */
120     public void executeOperationLog(BusinessLog businessLog, final String account, JoinPoint joinPoint, final String result) {
121         SysOpLog sysOpLog = this.genBaseSysOpLog();
122         TimerTask timerTask = LogTaskFactory.operationLog(sysOpLog, account, businessLog, joinPoint, result);
123         executeLog(timerTask);
124     }
125
126     /**
127      * 异常日志
128      *
129      * @author xuyuxiang
130      * @date 2020/3/18 20:01
131      */
132     public void executeExceptionLog(BusinessLog businessLog, final String account, JoinPoint joinPoint, Exception exception) {
133         SysOpLog sysOpLog = this.genBaseSysOpLog();
134         TimerTask timerTask = LogTaskFactory.exceptionLog(sysOpLog, account, businessLog, joinPoint, exception);
135         executeLog(timerTask);
136     }
137
138     /**
139      * 构建基础访问日志
140      *
141      * @author xuyuxiang
142      * @date 2020/3/19 14:44
143      */
144     private SysVisLog genBaseSysVisLog() {
145         HttpServletRequest request = HttpServletUtil.getRequest();
146         if (ObjectUtil.isNotNull(request)) {
147             String ip = IpAddressUtil.getIp(request);
148             String address = IpAddressUtil.getAddress(request);
149             String browser = UaUtil.getBrowser(request);
150             String os = UaUtil.getOs(request);
151             return LogFactory.genBaseSysVisLog(ip, address, browser, os);
152         } else {
153             throw new ServiceException(ServerExceptionEnum.REQUEST_EMPTY);
154         }
155     }
156
157     /**
158      * 构建基础操作日志
159      *
160      * @author xuyuxiang
161      * @date 2020/3/19 14:44
162      */
163     private SysOpLog genBaseSysOpLog() {
164         HttpServletRequest request = HttpServletUtil.getRequest();
165         if (ObjectUtil.isNotNull(request)) {
166             String ip = IpAddressUtil.getIp(request);
167             String address = IpAddressUtil.getAddress(request);
168             String browser = UaUtil.getBrowser(request);
169             String os = UaUtil.getOs(request);
170             String url = request.getRequestURI();
171             String method = request.getMethod();
172             return LogFactory.genBaseSysOpLog(ip, address, browser, os, url, method);
173         } else {
174             throw new ServiceException(ServerExceptionEnum.REQUEST_EMPTY);
175         }
176     }
177
178 }