From 074f46a4ad024c46fd7b543f80caa1562c2ba374 Mon Sep 17 00:00:00 2001 From: zhongjun96 Date: Tue, 27 Feb 2024 14:18:58 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20=E6=97=A5=E5=BF=97=E8=B0=83=E6=95=B4?= =?UTF-8?q?=E4=B8=BA=20slf4j=20=E8=BE=93=E5=87=BA=EF=BC=8C=E4=BE=BF?= =?UTF-8?q?=E4=BA=8E=E6=9F=A5=E7=9C=8B=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../interceptor/business/LogInterceptor.java | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/mongo-plus-core/src/main/java/com/anwen/mongo/interceptor/business/LogInterceptor.java b/mongo-plus-core/src/main/java/com/anwen/mongo/interceptor/business/LogInterceptor.java index 1339f05e..22091051 100644 --- a/mongo-plus-core/src/main/java/com/anwen/mongo/interceptor/business/LogInterceptor.java +++ b/mongo-plus-core/src/main/java/com/anwen/mongo/interceptor/business/LogInterceptor.java @@ -9,6 +9,8 @@ import com.anwen.mongo.interceptor.Interceptor; import com.anwen.mongo.model.command.CommandFailed; import com.anwen.mongo.model.command.CommandStarted; import com.anwen.mongo.model.command.CommandSucceeded; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.Objects; @@ -19,6 +21,8 @@ import java.util.Objects; */ public class LogInterceptor implements Interceptor { + Logger logger = LoggerFactory.getLogger(LogInterceptor.class); + private String formattingStatement(String statement){ return PropertyCache.format ? JSON.toJSONString(JSONObject.parse(statement), SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteDateUseDateFormat) : statement; @@ -26,19 +30,17 @@ public class LogInterceptor implements Interceptor { @Override public void commandStarted(CommandStarted commandStarted) { - System.out.println(commandStarted.getCommandName()+" Statement Execution ==> "); - System.out.println(formattingStatement(commandStarted.getCommand())); + logger.info("{} Statement Execution ==> {}",commandStarted.getCommandName(),formattingStatement(commandStarted.getCommand())); } @Override public void commandSucceeded(CommandSucceeded commandSucceeded) { - System.out.println(commandSucceeded.getCommandName()+" results of execution ==> "); if (Objects.equals(commandSucceeded.getCommandName(), "find") || Objects.equals(commandSucceeded.getCommandName(), "aggregate")){ - System.out.println(commandSucceeded.getResponse().getDocument("cursor").get("firstBatch").asArray().getValues().size()); + logger.info("{} results of execution ==> {}",commandSucceeded.getCommandName(),commandSucceeded.getResponse().getDocument("cursor").get("firstBatch").asArray().getValues().size()); } else if (Objects.equals(commandSucceeded.getCommandName(), "update")) { - System.out.println(commandSucceeded.getResponse().get("nModified").asInt32().getValue()); + logger.info("{} results of execution ==> {}",commandSucceeded.getCommandName(),commandSucceeded.getResponse().get("nModified").asInt32().getValue()); } else if (Objects.equals(commandSucceeded.getCommandName(), "insert") || Objects.equals(commandSucceeded.getCommandName(), "delete")) { - System.out.println(commandSucceeded.getResponse().get("n").asInt32().getValue()); + logger.info("{} results of execution ==> {}",commandSucceeded.getCommandName(),commandSucceeded.getResponse().get("n").asInt32().getValue()); } } @@ -46,7 +48,7 @@ public class LogInterceptor implements Interceptor { public void commandFailed(CommandFailed commandFailed) { String commandName = commandFailed.getCommandName(); Throwable throwable = commandFailed.getThrowable(); - System.out.println("error ==> : " + commandName + ", " + throwable.getMessage()); + logger.error("error ==> : {} , {}", commandName, throwable.getMessage()); } @Override -- Gitee