服务端全局访问日志调整

This commit is contained in:
aoshiguchen
2023-03-12 13:10:38 +08:00
parent 56b216406e
commit a6b651065f
8 changed files with 170 additions and 153 deletions
@@ -49,11 +49,6 @@ public class DBInitialize implements EventListener<AppLoadEndEvent> {
@Inject
private DbConfig dbConfig;
private DbTypeEnum dbTypeEnum;
// @Inject
// private JdbcTemplate jdbcTemplate;
// @Inject
// private DataSource dataSource;
@Init
public void init() throws Throwable {
@@ -1,24 +1,3 @@
/**
* Copyright (c) 2022 aoshiguchen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package fun.asgc.neutrino.proxy.server.base.page;
import lombok.Data;
@@ -1,24 +1,3 @@
/**
* Copyright (c) 2022 aoshiguchen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package fun.asgc.neutrino.proxy.server.base.page;
import lombok.Data;
@@ -1,24 +1,3 @@
/**
* Copyright (c) 2022 aoshiguchen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package fun.asgc.neutrino.proxy.server.base.proxy;
import lombok.Data;
@@ -24,6 +24,7 @@ package fun.asgc.neutrino.proxy.server.base.rest;
import fun.asgc.neutrino.proxy.server.dal.entity.UserDO;
import lombok.Data;
import lombok.experimental.Accessors;
import org.noear.solon.core.handle.Action;
import java.util.Date;
@@ -51,4 +52,5 @@ public class SystemContext {
* 接收请求时间
*/
private Date receiveTime;
private Action action;
}
@@ -6,6 +6,7 @@ import fun.asgc.neutrino.proxy.server.constant.EnableStatusEnum;
import fun.asgc.neutrino.proxy.server.constant.ExceptionConstant;
import fun.asgc.neutrino.proxy.server.dal.entity.UserDO;
import fun.asgc.neutrino.proxy.server.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.noear.solon.Solon;
import org.noear.solon.annotation.Component;
@@ -21,12 +22,14 @@ import java.lang.reflect.Method;
* @author: aoshiguchen
* @date: 2023/3/9
*/
@Slf4j
@Component
public class BaseAuthInterceptor implements RouterInterceptor {
@Override
public void doIntercept(Context ctx, Handler mainHandler, RouterInterceptorChain chain) throws Throwable {
if (!(mainHandler instanceof Action)) {
chain.doIntercept(ctx, mainHandler);
return;
}
Action action = (Action) mainHandler;
@@ -64,18 +67,20 @@ public class BaseAuthInterceptor implements RouterInterceptor {
@Override
public Object postResult(Context ctx, Object result) throws Throwable {
SystemContextHolder.remove();
// SystemContextHolder.remove();
if (result instanceof ResponseBody) {
return result;
}
if (result instanceof ServiceException) {
ServiceException exception = (ServiceException) result;
return new ResponseBody<>()
.setCode(exception.getCode())
.setMsg(exception.getMsg());
}
if (result instanceof Throwable) {
new ResponseBody<>()
log.error("全局异常", (Throwable) result);
if (result instanceof ServiceException) {
ServiceException exception = (ServiceException) result;
return new ResponseBody<>()
.setCode(exception.getCode())
.setMsg(exception.getMsg());
}
return new ResponseBody<>()
.setCode(ExceptionConstant.SYSTEM_ERROR.getCode())
.setMsg(ExceptionConstant.SYSTEM_ERROR.getMsg())
.setStack(ExceptionUtils.getStackTrace((Throwable) result));
@@ -1,77 +1,78 @@
///**
// * Copyright (c) 2022 aoshiguchen
// *
// * Permission is hereby granted, free of charge, to any person obtaining a copy
// * of this software and associated documentation files (the "Software"), to deal
// * in the Software without restriction, including without limitation the rights
// * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// * copies of the Software, and to permit persons to whom the Software is
// * furnished to do so, subject to the following conditions:
// *
// * The above copyright notice and this permission notice shall be included in all
// * copies or substantial portions of the Software.
// *
// * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// * SOFTWARE.
// */
//package fun.asgc.neutrino.proxy.server.base.rest.interceptor;
//
//import com.alibaba.fastjson.JSONObject;
//import fun.asgc.neutrino.proxy.server.base.rest.SystemContextHolder;
//import lombok.extern.slf4j.Slf4j;
//
//import java.lang.reflect.Method;
//import java.util.Date;
//
///**
// * 访问日志拦截器
// * @author: aoshiguchen
// * @date: 2022/8/14
// */
//@Slf4j
//public class VisitLogInterceptor implements HandlerInterceptor {
//
// @Override
// public boolean preHandle(HttpRequestWrapper requestParser, HttpResponseWrapper responseWrapper, String route, Method targetMethod) throws Exception {
// if (null != SystemContextHolder.getContext()) {
// SystemContextHolder.getContext().setReceiveTime(new Date());
// }
// return true;
// }
//
// @Override
// public void postHandle(HttpRequestWrapper requestParser, HttpResponseWrapper responseWrapper, String route, Method targetMethod, Object result) throws Exception {
// if (null == SystemContextHolder.getContext() || null == SystemContextHolder.getContext().getReceiveTime()) {
// return;
// }
// Date receiveTime = SystemContextHolder.getContext().getReceiveTime();
// Date now = new Date();
// long elapsedTime = now.getTime() - receiveTime.getTime();
// log.info("\n-----------------------------------------------------------------接口请求日志:\n{} url:{} 执行耗时:{}\nURL参数:{}\n请求体参数:{}\n响应结果:{}\n客户端IP:{}\n",
// requestParser.getMethod().name(), requestParser.getUrl(), getElapsedTimeStr(elapsedTime),
// requestParser.getQueryString(),
// requestParser.getContentAsString(),
// JSONObject.toJSONString(result),
// SystemContextHolder.getIp()
// );
// }
//
// /**
// * 获取耗时描述
// * @param elapsedTime
// * @return
// */
// private static String getElapsedTimeStr(long elapsedTime) {
// if (elapsedTime < 1000) {
// return String.format("%s毫秒", elapsedTime);
// } else if (elapsedTime < 60000) {
// return String.format("%.2f秒", (elapsedTime * 1.0) / 1000);
// }
// return String.format("%.2f分钟", (elapsedTime * 1.0) / 1000 / 60);
// }
//}
package fun.asgc.neutrino.proxy.server.base.rest.interceptor;
import com.alibaba.fastjson.JSONObject;
import fun.asgc.neutrino.proxy.server.base.rest.SystemContext;
import fun.asgc.neutrino.proxy.server.base.rest.SystemContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.noear.solon.annotation.Component;
import org.noear.solon.boot.jlhttp.HTTPServer;
import org.noear.solon.boot.jlhttp.JlHttpContext;
import org.noear.solon.core.handle.Action;
import org.noear.solon.core.handle.Context;
import org.noear.solon.core.handle.Handler;
import org.noear.solon.core.route.RouterInterceptor;
import org.noear.solon.core.route.RouterInterceptorChain;
import java.util.Date;
/**
* @author: aoshiguchen
* @date: 2023/3/12
*/
@Slf4j
@Component(index = 1)
public class VisitLogInterceptor implements RouterInterceptor {
@Override
public void doIntercept(Context ctx, Handler mainHandler, RouterInterceptorChain chain) throws Throwable {
if (!(mainHandler instanceof Action)) {
chain.doIntercept(ctx, mainHandler);
return;
}
SystemContext systemContext = SystemContextHolder.getContext();
if (null == systemContext) {
chain.doIntercept(ctx, mainHandler);
return;
}
Action action = (Action) mainHandler;
systemContext.setAction(action);
systemContext.setReceiveTime(new Date());
chain.doIntercept(ctx, mainHandler);
}
@Override
public Object postResult(Context ctx, Object result) throws Throwable {
SystemContext systemContext = SystemContextHolder.getContext();
if (null != systemContext) {
// 如果更换了其他插件,则此处需要调整
JlHttpContext jlHttpContext = (JlHttpContext) ctx;
HTTPServer.Request request = (HTTPServer.Request) jlHttpContext.request();
Date receiveTime = SystemContextHolder.getContext().getReceiveTime();
Date now = new Date();
long elapsedTime = now.getTime() - receiveTime.getTime();
log.info("\n-----------------------------------------------------------------接口请求日志:\n{} url:{} 执行耗时:{}\n请求体参数:{}\n响应结果:{}\n客户端IP:{}\n",
request.getMethod(), request.getPath(), getElapsedTimeStr(elapsedTime),
JSONObject.toJSONString(request.getParams()),
JSONObject.toJSONString(result),
SystemContextHolder.getIp()
);
}
SystemContextHolder.remove();
return RouterInterceptor.super.postResult(ctx, result);
}
/**
* 获取耗时描述
* @param elapsedTime
* @return
*/
private static String getElapsedTimeStr(long elapsedTime) {
if (elapsedTime < 1000) {
return String.format("%s毫秒", elapsedTime);
} else if (elapsedTime < 60000) {
return String.format("%.2f秒", (elapsedTime * 1.0) / 1000);
}
return String.format("%.2f分钟", (elapsedTime * 1.0) / 1000 / 60);
}
}
@@ -0,0 +1,77 @@
///**
// * Copyright (c) 2022 aoshiguchen
// *
// * Permission is hereby granted, free of charge, to any person obtaining a copy
// * of this software and associated documentation files (the "Software"), to deal
// * in the Software without restriction, including without limitation the rights
// * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// * copies of the Software, and to permit persons to whom the Software is
// * furnished to do so, subject to the following conditions:
// *
// * The above copyright notice and this permission notice shall be included in all
// * copies or substantial portions of the Software.
// *
// * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// * SOFTWARE.
// */
//package fun.asgc.neutrino.proxy.server.base.rest.interceptor;
//
//import com.alibaba.fastjson.JSONObject;
//import fun.asgc.neutrino.proxy.server.base.rest.SystemContextHolder;
//import lombok.extern.slf4j.Slf4j;
//
//import java.lang.reflect.Method;
//import java.util.Date;
//
///**
// * 访问日志拦截器
// * @author: aoshiguchen
// * @date: 2022/8/14
// */
//@Slf4j
//public class VisitLogInterceptor implements HandlerInterceptor {
//
// @Override
// public boolean preHandle(HttpRequestWrapper requestParser, HttpResponseWrapper responseWrapper, String route, Method targetMethod) throws Exception {
// if (null != SystemContextHolder.getContext()) {
// SystemContextHolder.getContext().setReceiveTime(new Date());
// }
// return true;
// }
//
// @Override
// public void postHandle(HttpRequestWrapper requestParser, HttpResponseWrapper responseWrapper, String route, Method targetMethod, Object result) throws Exception {
// if (null == SystemContextHolder.getContext() || null == SystemContextHolder.getContext().getReceiveTime()) {
// return;
// }
// Date receiveTime = SystemContextHolder.getContext().getReceiveTime();
// Date now = new Date();
// long elapsedTime = now.getTime() - receiveTime.getTime();
// log.info("\n-----------------------------------------------------------------接口请求日志:\n{} url:{} 执行耗时:{}\nURL参数:{}\n请求体参数:{}\n响应结果:{}\n客户端IP:{}\n",
// requestParser.getMethod().name(), requestParser.getUrl(), getElapsedTimeStr(elapsedTime),
// requestParser.getQueryString(),
// requestParser.getContentAsString(),
// JSONObject.toJSONString(result),
// SystemContextHolder.getIp()
// );
// }
//
// /**
// * 获取耗时描述
// * @param elapsedTime
// * @return
// */
// private static String getElapsedTimeStr(long elapsedTime) {
// if (elapsedTime < 1000) {
// return String.format("%s毫秒", elapsedTime);
// } else if (elapsedTime < 60000) {
// return String.format("%.2f秒", (elapsedTime * 1.0) / 1000);
// }
// return String.format("%.2f分钟", (elapsedTime * 1.0) / 1000 / 60);
// }
//}