去除冗余代码,全局访问日志处理优化

This commit is contained in:
aoshiguchen
2023-03-12 17:47:44 +08:00
parent e1abbea5d9
commit 6d3b0db745
5 changed files with 15 additions and 108 deletions
@@ -1,18 +1,16 @@
package fun.asgc.neutrino.proxy.client;
import fun.asgc.neutrino.core.annotation.NeutrinoApplication;
import fun.asgc.neutrino.core.context.NeutrinoLauncher;
import org.noear.solon.Solon;
/**
*
* @author: aoshiguchen
* @date: 2022/6/16
*/
@NeutrinoApplication(environmentVariableKey = "NeutrinoProxyClient")
public class ProxyClient {
public static void main(String[] args) {
NeutrinoLauncher.run(ProxyClient.class, args);
Solon.start(ProxyClient.class, args);
}
}
+1 -15
View File
@@ -30,32 +30,18 @@
<groupId>org.noear</groupId>
<artifactId>activerecord-solon-plugin</artifactId>
</dependency>
<!--orika orika-solon-plugin用户,server端没直接用,为何这里需要引入?-->
<dependency>
<groupId>ma.glasnost.orika</groupId>
<artifactId>orika-core</artifactId>
<version>1.5.4</version>
</dependency>
<!--orika-->
<dependency>
<groupId>fun.asgc</groupId>
<artifactId>orika-solon-plugin</artifactId>
<version>2.2.2</version>
<scope>system</scope>
<systemPath>${project.basedir}/../_solon_plugin/orika-solon-plugin/pom.xml</systemPath>
</dependency>
<!--job-->
<dependency>
<groupId>fun.asgc</groupId>
<artifactId>job-solon-plugin</artifactId>
<version>2.2.2</version>
<scope>system</scope>
<systemPath>${project.basedir}/../_solon_plugin/job-solon-plugin/pom.xml</systemPath>
</dependency>
<!-- <dependency>-->
<!-- <groupId>cn.hutool</groupId>-->
<!-- <artifactId>hutool-db</artifactId>-->
<!-- <version>5.8.15</version>-->
<!-- </dependency>-->
<dependency>
<groupId>fun.asgc.neutrino</groupId>
<artifactId>neutrino-proxy-core</artifactId>
@@ -2,7 +2,6 @@ package fun.asgc.neutrino.proxy.server;
import fun.asgc.solon.extend.job.annotation.EnableJob;
import org.noear.solon.Solon;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.SolonMain;
import org.noear.solon.web.cors.CrossFilter;
@@ -26,17 +26,18 @@ public class VisitLogInterceptor implements RouterInterceptor {
}
Date startTime = new Date();
chain.doIntercept(ctx, mainHandler);
Date now = new Date();
long elapsedTime = now.getTime() - startTime.getTime();
log.info("\n-----------------------------------------------------------------接口请求日志:\n{} url:{} 执行耗时:{}\n请求体参数:{}\n响应结果:{}\n客户端IP:{}\n",
ctx.method(), ctx.path(), getElapsedTimeStr(elapsedTime),
JSONObject.toJSONString(ctx.paramMap()),
JSONObject.toJSONString(JSONObject.toJSONString(ctx.result)),
ctx.realIp()
);
try {
chain.doIntercept(ctx, mainHandler);
} finally {
Date now = new Date();
long elapsedTime = now.getTime() - startTime.getTime();
log.info("\n-----------------------------------------------------------------接口请求日志:\n{} url:{} 执行耗时:{}\n请求体参数:{}\n响应结果:{}\n客户端IP:{}\n",
ctx.method(), ctx.path(), getElapsedTimeStr(elapsedTime),
JSONObject.toJSONString(ctx.paramMap()),
JSONObject.toJSONString(JSONObject.toJSONString(ctx.result)),
ctx.realIp()
);
}
}
/**
@@ -1,77 +0,0 @@
///**
// * 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);
// }
//}