Bladeren bron

mongo 日志输出优化

刘凯雄 2 jaren geleden
bovenliggende
commit
e7d43d99dd

+ 4 - 4
elab-mongodb/src/main/java/com/elab/mongodb/BaseMongodb.java

@@ -39,7 +39,7 @@ public abstract class BaseMongodb<T> {
      * @param entity
      */
     public void save(T entity) {
-        LOGGER.debug(" save request : {}", LogUtils.getJsonString(entity));
+        LOGGER.debug(" save request : {}", LogUtils.getSmallJsonString(entity));
         if (getCollectionName() == null) {
             getMongoTemplate().save(entity);
         } else {
@@ -85,7 +85,7 @@ public abstract class BaseMongodb<T> {
      * @throws Exception
      */
     public boolean exists(T entity) throws Exception {
-        LOGGER.debug(" mongo exists : {} ", LogUtils.getJsonString(entity));
+        LOGGER.debug(" mongo exists : {} ", LogUtils.getSmallJsonString(entity));
         Query query = beanForQuery(entity);
         boolean exists = false;
         if (getCollectionName() == null) {
@@ -116,7 +116,7 @@ public abstract class BaseMongodb<T> {
      * @throws Exception
      */
     public Long count(T entity, String collectionName) throws Exception {
-        LOGGER.debug(" 调用mongo的count方法 : {}", LogUtils.getJsonString(entity));
+        LOGGER.debug(" 调用mongo的count方法 : {}", LogUtils.getSmallJsonString(entity));
         Query query = beanForQuery(entity);
         long count = 0;
         if (collectionName == null) {
@@ -139,7 +139,7 @@ public abstract class BaseMongodb<T> {
 
         Assert.notNull(id, "id is not null!");
         Assert.notNull(entity, "entity is not null!");
-        LOGGER.debug(" 调用mongo的updateById方法 : {}", LogUtils.getJsonString(entity));
+        LOGGER.debug(" 调用mongo的updateById方法 : {}", LogUtils.getSmallJsonString(entity));
         Query query = new Query(Criteria.where("id").is(id));
         Update update = new Update();
         Map<String, Object> describe = BeanUtil.beanToMap(entity);

+ 4 - 8
elab-mongodb/src/main/java/com/elab/mongodb/chain/CatMongoInterceptInvoke.java

@@ -1,6 +1,5 @@
 package com.elab.mongodb.chain;
 
-import com.alibaba.fastjson.JSON;
 import com.elab.core.componts.chains.ComponentConsumerHandlerChain;
 import com.elab.core.componts.chains.ComponentSupplierHandlerChain;
 import com.elab.log.utils.CatCrossProcess;
@@ -54,7 +53,7 @@ public class CatMongoInterceptInvoke implements MongoInterceptInvoke {
         try {
             String type = String.format("%s - %s", collectionName, MongoActionOperation.INSERT.name());
             CatCrossProcess.processTransactionMsg("mongodb", type, (t) -> {
-                logger.info("请求入参: " + JSON.toJSONString(objectToSave));
+                logger.info("请求入参: " + LogUtils.getSmallJsonString(objectToSave));
                 chain.run(request, MongoInterceptInvoke::insert);
                 return null;
             });
@@ -76,7 +75,7 @@ public class CatMongoInterceptInvoke implements MongoInterceptInvoke {
                 logger.debug(" query : {}", getString(query));
                 logger.debug(" update :  {} ", getString(update));
                 T executor = chain.executor(request, MongoInterceptInvoke::update);
-                logger.info("mongo result : {}", LogUtils.getJsonString(executor));
+                logger.info("mongo result : {}", LogUtils.getSmallJsonString(executor));
                 return executor;
             });
         } catch (Exception e) {
@@ -121,7 +120,7 @@ public class CatMongoInterceptInvoke implements MongoInterceptInvoke {
                 logger.debug(" query : {}", getString(query));
                 logger.debug(" field :  {} ", getString(fields));
                 T executor = chain.executor(request, MongoInterceptInvoke::queryOne);
-                logger.info("mongo result : {}", LogUtils.getJsonString(executor));
+                logger.info("mongo result : {}", LogUtils.getSmallJsonString(executor));
                 return executor;
             });
         } catch (Exception e) {
@@ -131,10 +130,7 @@ public class CatMongoInterceptInvoke implements MongoInterceptInvoke {
     }
 
     private String getString(Object obj) {
-        if (obj == null) {
-            return "";
-        }
-        return obj.toString();
+        return LogUtils.getSmallJsonString(obj);
     }
 
 }

+ 1 - 1
elab-mongodb/src/main/java/com/elab/mongodb/utils/LogUtils.java

@@ -11,7 +11,7 @@ import org.slf4j.LoggerFactory;
 public class LogUtils {
     private static final Logger logger = LoggerFactory.getLogger(LogUtils.class);
 
-    public static String getJsonString(Object obj) {
+    public static String getSmallJsonString(Object obj) {
         String stringValue = "";
         if (obj == null) {
             return "";