Browse Source

优化日志打印级别

dengsixing 2 years ago
parent
commit
4dbfc2af3f

+ 15 - 3
4dkankan-common-utils/src/main/java/com/fdkankan/common/util/FileUtils.java

@@ -200,9 +200,13 @@ public class FileUtils {
     //按行读文件
     public static String readFile(String path) {
         try {
+            if(!FileUtil.exist(path)){
+                log.warn("文件不存在,path:{}", path);
+                return null;
+            }
             return FileUtil.readUtf8String(path);
         } catch (IORuntimeException e) {
-            log.warn("读取文件失败,文件不存在,path:"+path, e);
+            log.error("读取文件失败,文件不存在,path:"+path, e);
         }
         return null;
     }
@@ -918,11 +922,15 @@ public class FileUtils {
 
     public static JSONObject readJson(String filePath) throws IOException {
         try {
+            if(!FileUtil.exist(filePath)){
+                log.warn("文件不存在,path:{}", filePath);
+                return null;
+            }
             String content = FileUtil.readUtf8String(filePath);
             JSONObject jsonObj = JSON.parseObject(content);
             return jsonObj;
         } catch (Exception e) {
-            log.warn("读取json失败,filePath:" + filePath, e);
+            log.error("读取json失败,filePath:" + filePath, e);
         }
         return null;
     }
@@ -934,9 +942,13 @@ public class FileUtils {
      */
     public static String readUtf8String(String path){
         try {
+            if(!FileUtil.exist(path)){
+                log.warn("文件不存在,path:{}", path);
+                return null;
+            }
             return FileUtil.readUtf8String(path);
         }catch (Exception e){
-            log.warn("读取文件失败,path:{}", path);
+            log.error("读取文件失败,path:"+path, e);
         }
         return null;
     }

+ 13 - 14
4dkankan-utils-fyun-oss/src/main/java/com/fdkankan/fyun/oss/OssFileService.java

@@ -34,7 +34,7 @@ public class OssFileService extends AbstractFYunFileService {
         try {
             ossClient.putObject(bucket, remoteFilePath, new ByteArrayInputStream(data));
         } catch (Exception e) {
-            log.warn("oss上传文件失败,remoteFilePath:" + remoteFilePath, e);
+            log.error("oss上传文件失败,remoteFilePath:" + remoteFilePath, e);
         }
         return null;
     }
@@ -50,8 +50,7 @@ public class OssFileService extends AbstractFYunFileService {
             ossClient.putObject(bucket, remoteFilePath, inputStream);
             log.info("文件流上传成功,目标路径:remoteFilePath:{}", remoteFilePath);
         } catch (Exception e) {
-            log.warn("oss上传文件失败,remoteFilePath:"+remoteFilePath, e);
-            e.printStackTrace();
+            log.error("oss上传文件失败,remoteFilePath:"+remoteFilePath, e);
         }
         return null;
     }
@@ -82,7 +81,7 @@ public class OssFileService extends AbstractFYunFileService {
             ossClient.putObject(bucket, remoteFilePath, file, metadata);
             log.info("文件上传成功,path:{}", filePath);
         } catch (Exception e) {
-            log.warn("oss上传文件失败,filePath:"+filePath, e);
+            log.error("oss上传文件失败,filePath:"+filePath, e);
         }
         return null;
     }
@@ -96,7 +95,7 @@ public class OssFileService extends AbstractFYunFileService {
             callshell(command);
             log.info("上传文件完毕, ossPath:{}, srcPath:{}", remoteFilePath, filePath);
         } catch (Exception e) {
-            log.warn(String.format("上传文件失败, ossPath:%s, srcPath:%s", remoteFilePath, filePath), e);
+            log.error(String.format("上传文件失败, ossPath:%s, srcPath:%s", remoteFilePath, filePath), e);
         }
         return null;
     }
@@ -110,7 +109,7 @@ public class OssFileService extends AbstractFYunFileService {
             callshell(command);
             log.info("下载文件完毕, ossPath:{}, srcPath:{}", remoteFilePath, filePath);
         } catch (Exception e) {
-            log.warn(String.format("下载文件失败, ossPath:%s, srcPath:%s", remoteFilePath, filePath), e);
+            log.error(String.format("下载文件失败, ossPath:%s, srcPath:%s", remoteFilePath, filePath), e);
         }
     }
 
@@ -134,7 +133,7 @@ public class OssFileService extends AbstractFYunFileService {
             request.setKeys(remoteFiles);
             ossClient.deleteObjects(request);
         } catch (Exception e) {
-            log.warn("OSS删除文件失败,key:" + remoteFolderPath, e);
+            log.error("OSS删除文件失败,key:" + remoteFolderPath, e);
         }
     }
 
@@ -145,7 +144,7 @@ public class OssFileService extends AbstractFYunFileService {
                 uploadFile(bucket, entry.getKey(), entry.getValue(), null);
             }
         } catch (Exception e) {
-            log.warn("OSS批量上传文件失败!");
+            log.error("OSS批量上传文件失败!");
         }
     }
 
@@ -174,7 +173,7 @@ public class OssFileService extends AbstractFYunFileService {
                 flag = objectListing.isTruncated();
             } while (flag);
         } catch (Exception e) {
-            log.warn("获取文件列表失败,path:" + sourcePath, e);
+            log.error("获取文件列表失败,path:" + sourcePath, e);
         }
         return keyList;
     }
@@ -190,7 +189,7 @@ public class OssFileService extends AbstractFYunFileService {
                 ossClient.copyObject(sourceBucketName, file, targetBucketName, file.replace(sourcePath, targetPath));
             });
         } catch (Exception e) {
-            log.warn("列举文件目录失败,key:" + sourcePath, e);
+            log.error("列举文件目录失败,key:" + sourcePath, e);
         }
     }
 
@@ -204,7 +203,7 @@ public class OssFileService extends AbstractFYunFileService {
                 copyFileBetweenBucket(sourceBucketName, entry.getKey(), targetBucketName, entry.getValue());
             }
         } catch (Exception e) {
-            log.warn(String.format("批量复制文件失败, sourceBucketName:%s, targetBucketName:%s", sourceBucketName, targetBucketName), e);
+            log.error(String.format("批量复制文件失败, sourceBucketName:%s, targetBucketName:%s", sourceBucketName, targetBucketName), e);
         }
     }
 
@@ -224,7 +223,7 @@ public class OssFileService extends AbstractFYunFileService {
             }
             return contentJson.toString();
         } catch (Exception e) {
-            log.warn("获取文件内容失败:key:"+remoteFilePath, e);
+            log.error("获取文件内容失败:key:"+remoteFilePath, e);
         }
         return null;
     }
@@ -234,7 +233,7 @@ public class OssFileService extends AbstractFYunFileService {
         try {
             return ossClient.doesObjectExist(bucket, objectName);
         } catch (Exception e) {
-            log.warn("判断文件是否存在失败,key:"+objectName, e);
+            log.error("判断文件是否存在失败,key:"+objectName, e);
         }
         return false;
     }
@@ -259,7 +258,7 @@ public class OssFileService extends AbstractFYunFileService {
             request.setEnableCheckpoint(true);
             ossClient.downloadFile(request);
         } catch (Throwable throwable) {
-            log.warn("文件下载失败,key:"+remoteFilePath, throwable);
+            log.error("文件下载失败,key:"+remoteFilePath, throwable);
         }
     }