Procházet zdrojové kódy

mongo 日志输出优化

刘凯雄 před 2 roky
rodič
revize
f1b5694087

+ 5 - 14
elab-mongodb/src/main/java/com/elab/mongodb/BaseMongodb.java

@@ -1,7 +1,7 @@
 package com.elab.mongodb;
 
 import cn.hutool.core.bean.BeanUtil;
-import com.alibaba.fastjson.JSON;
+import com.elab.mongodb.utils.LogUtils;
 import com.mongodb.WriteResult;
 import org.apache.commons.beanutils.BeanUtils;
 import org.slf4j.Logger;
@@ -39,7 +39,7 @@ public abstract class BaseMongodb<T> {
      * @param entity
      */
     public void save(T entity) {
-        LOGGER.debug(" save request : {}", jsonToString(entity));
+        LOGGER.debug(" save request : {}", LogUtils.getJsonString(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 : {} ", jsonToString(entity));
+        LOGGER.debug(" mongo exists : {} ", LogUtils.getJsonString(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方法 : {}", jsonToString(entity));
+        LOGGER.debug(" 调用mongo的count方法 : {}", LogUtils.getJsonString(entity));
         Query query = beanForQuery(entity);
         long count = 0;
         if (collectionName == null) {
@@ -139,10 +139,9 @@ public abstract class BaseMongodb<T> {
 
         Assert.notNull(id, "id is not null!");
         Assert.notNull(entity, "entity is not null!");
-        LOGGER.debug(" 调用mongo的updateById方法 : {}", jsonToString(entity));
+        LOGGER.debug(" 调用mongo的updateById方法 : {}", LogUtils.getJsonString(entity));
         Query query = new Query(Criteria.where("id").is(id));
         Update update = new Update();
-        //Map<String, String> describe = BeanUtils.describe(entity);
         Map<String, Object> describe = BeanUtil.beanToMap(entity);
         // 遍历map集合
         for (Map.Entry<String, Object> entry : describe.entrySet()) {
@@ -247,12 +246,4 @@ public abstract class BaseMongodb<T> {
         return query;
     }
 
-    private String jsonToString(Object obj) {
-        try {
-            return JSON.toJSONString(obj);
-        } catch (Exception e) {
-            LOGGER.debug("json转换失败:{} , {}", obj.toString(), e.getMessage());
-        }
-        return "";
-    }
 }

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

@@ -1,7 +1,6 @@
 package com.elab.mongodb.chain;
 
 import com.alibaba.fastjson.JSON;
-import com.dianping.cat.Cat;
 import com.elab.core.componts.chains.ComponentConsumerHandlerChain;
 import com.elab.core.componts.chains.ComponentSupplierHandlerChain;
 import com.elab.log.utils.CatCrossProcess;
@@ -9,6 +8,7 @@ import com.elab.mongodb.chain.model.MongoExecutorRequest;
 import com.elab.mongodb.chain.model.MongoInsertRequest;
 import com.elab.mongodb.chain.model.MongoQueryRequest;
 import com.elab.mongodb.chain.model.MongoUpdateRequest;
+import com.elab.mongodb.utils.LogUtils;
 import com.mongodb.DBObject;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -55,7 +55,6 @@ public class CatMongoInterceptInvoke implements MongoInterceptInvoke {
             String type = String.format("%s - %s", collectionName, MongoActionOperation.INSERT.name());
             CatCrossProcess.processTransactionMsg("mongodb", type, (t) -> {
                 logger.info("请求入参: " + JSON.toJSONString(objectToSave));
-                logger.info("---------->>>>>>>>>>>>>>>" + Cat.getCurrentMessageId());
                 chain.run(request, MongoInterceptInvoke::insert);
                 return null;
             });
@@ -74,11 +73,10 @@ public class CatMongoInterceptInvoke implements MongoInterceptInvoke {
         try {
             String type = String.format("%s - %s", collectionName, MongoActionOperation.UPDATE.name());
             return CatCrossProcess.processTransactionMsg("mongodb", type, (t) -> {
-                logger.info("---------->>>>>>>>>>>>>>>" + Cat.getCurrentMessageId());
                 logger.debug(" query : {}", getString(query));
                 logger.debug(" update :  {} ", getString(update));
                 T executor = chain.executor(request, MongoInterceptInvoke::update);
-                logger.info("mongo result : {}", getJsonString(executor));
+                logger.info("mongo result : {}", LogUtils.getJsonString(executor));
                 return executor;
             });
         } catch (Exception e) {
@@ -97,9 +95,8 @@ public class CatMongoInterceptInvoke implements MongoInterceptInvoke {
             String queryList = "QueryList";
             String type = String.format("%s - %s", collectionName, queryList);
             return CatCrossProcess.processTransactionMsg("mongodb", type, (t) -> {
-                logger.debug(" field :  {} ", getString(query));
+                logger.debug(" query :  {} ", getString(query));
                 logger.debug(" field :  {} ", getString(fields));
-                logger.info("---------->>>>>>>>>>>>>>>" + Cat.getCurrentMessageId());
                 List<T> executor = chain.executor(request, MongoInterceptInvoke::queryList);
                 logger.info("mongo result size : {} ", executor.size());
                 return executor;
@@ -123,9 +120,8 @@ public class CatMongoInterceptInvoke implements MongoInterceptInvoke {
             return CatCrossProcess.processTransactionMsg("mongodb", type, (t) -> {
                 logger.debug(" query : {}", getString(query));
                 logger.debug(" field :  {} ", getString(fields));
-                logger.info("---------->>>>>>>>>>>>>>>" + Cat.getCurrentMessageId());
                 T executor = chain.executor(request, MongoInterceptInvoke::queryOne);
-                logger.info("mongo result : {}", getJsonString(executor));
+                logger.info("mongo result : {}", LogUtils.getJsonString(executor));
                 return executor;
             });
         } catch (Exception e) {
@@ -141,23 +137,4 @@ public class CatMongoInterceptInvoke implements MongoInterceptInvoke {
         return obj.toString();
     }
 
-    private String getJsonString(Object obj) {
-        if (obj == null) {
-            return "";
-        } else if (obj instanceof String) {
-            return (String)obj;
-        }
-        try {
-            int maxLength = 1000;
-            String result = JSON.toJSONString(obj);
-            // 优化日志输出
-            if (result.length() > maxLength) {
-                return result.substring(0, maxLength);
-            }
-            return result;
-        } catch (Exception e) {
-            logger.warn("格式转换异常:{}", e.getMessage());
-        }
-        return "";
-    }
 }

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

@@ -0,0 +1,35 @@
+package com.elab.mongodb.utils;
+
+import com.alibaba.fastjson.JSON;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * @author liukaixiong
+ * @date 2022/10/21 - 10:39
+ */
+public class LogUtils {
+    private static final Logger logger = LoggerFactory.getLogger(LogUtils.class);
+
+    public static String getJsonString(Object obj) {
+        String stringValue = "";
+        if (obj == null) {
+            return "";
+        } else if (obj instanceof String) {
+            stringValue = (String)obj;
+        } else {
+            stringValue = JSON.toJSONString(obj);
+        }
+        try {
+            int maxLength = 1000;
+            // 优化日志输出
+            if (stringValue.length() > maxLength) {
+                return stringValue.substring(0, maxLength);
+            }
+            return stringValue;
+        } catch (Exception e) {
+            logger.warn("格式转换异常:{}", e.getMessage());
+        }
+        return "";
+    }
+}