Kaynağa Gözat

增加接口调用日志功能

songw 3 ay önce
ebeveyn
işleme
656df7acfa

+ 8 - 1
app/src/main/java/com/uas/rd_equipment/application/PdaApplication.java

@@ -14,6 +14,7 @@ import com.facebook.stetho.Stetho;
 import com.uas.rd_equipment.R;
 import com.uas.rd_equipment.util.AndroidUtil;
 import com.uas.rd_equipment.util.FakeX509TrustManager;
+import com.uas.rd_equipment.util.MyLog;
 import com.uas.rd_equipment.util.SoundUtil;
 import com.umeng.analytics.MobclickAgent;
 import com.umeng.commonsdk.UMConfigure;
@@ -87,7 +88,13 @@ public class PdaApplication extends Application {
         mSoundMap.put(SoundUtil.SOUND_TETHYS, mSoundPool.load(this, R.raw.tethys, 1));
         mSoundMap.put(SoundUtil.SOUND_TITAN, mSoundPool.load(this, R.raw.titan, 1));
         initUmeng();
-//        getMessageData();
+
+        //删除非当天的Log日志, Android8.0以下用不了
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+            MyLog.delNotDayFile();
+        }else {
+            MyLog.isFileCreatedTodayDel();
+        }
     }
 
     private void initUmeng() {

+ 3 - 1
app/src/main/java/com/uas/rd_equipment/util/CommonUtil.java

@@ -657,9 +657,11 @@ public class CommonUtil {
             if (errorMsg != null && errorMsg.exceptionInfo != null) {
                 errorStr = errorMsg.exceptionInfo;
             } else {
-                errorStr = "系统错误";
+                //errorStr = "系统错误";
+                errorStr = new String(volleyError.networkResponse.data);
             }
         }
+        MyLog.d("aaa","响应失败:" + errorStr);
         //振动提示
         makeNotice();
         /*MediaPlayer mp = new MediaPlayer();

+ 63 - 0
app/src/main/java/com/uas/rd_equipment/util/FileUtils.java

@@ -23,6 +23,7 @@ import java.io.InputStream;
 import java.security.SecureRandom;
 import java.security.cert.CertificateException;
 import java.security.cert.X509Certificate;
+import java.util.Calendar;
 
 import javax.net.ssl.HostnameVerifier;
 import javax.net.ssl.SSLContext;
@@ -423,4 +424,66 @@ public class FileUtils {
             return true;
         }
     }
+
+    /**
+     * 判断文件是否为当天创建(使用最后修改时间)
+     * Android 7.0推荐使用此方法
+     */
+    public static boolean isFileCreatedToday(File file) {
+        if (file == null || !file.exists()) {
+            return false;
+        }
+
+        // 获取文件的最后修改时间
+        long lastModified = file.lastModified();
+
+        // 获取当前时间的Calendar
+        Calendar today = Calendar.getInstance();
+
+        // 获取文件时间的Calendar
+        Calendar fileDate = Calendar.getInstance();
+        fileDate.setTimeInMillis(lastModified);
+
+        // 比较年、月、日是否相同
+        return today.get(Calendar.YEAR) == fileDate.get(Calendar.YEAR) &&
+                today.get(Calendar.MONTH) == fileDate.get(Calendar.MONTH) &&
+                today.get(Calendar.DAY_OF_MONTH) == fileDate.get(Calendar.DAY_OF_MONTH);
+    }
+
+    /**
+     * 获取文件创建时间的字符串表示
+     */
+    public static String getFileCreationTimeString(File file) {
+        if (file == null || !file.exists()) {
+            return "文件不存在";
+        }
+
+        long lastModified = file.lastModified();
+        Calendar cal = Calendar.getInstance();
+        cal.setTimeInMillis(lastModified);
+
+        return String.format("%04d-%02d-%02d %02d:%02d:%02d",
+                cal.get(Calendar.YEAR),
+                cal.get(Calendar.MONTH) + 1, // 月份从0开始
+                cal.get(Calendar.DAY_OF_MONTH),
+                cal.get(Calendar.HOUR_OF_DAY),
+                cal.get(Calendar.MINUTE),
+                cal.get(Calendar.SECOND));
+    }
+
+    /**
+     * 格式化文件大小
+     */
+    public static String formatFileSize(long size) {
+        if (size < 1024) {
+            return size + " B";
+        } else if (size < 1024 * 1024) {
+            return String.format("%.2f KB", size / 1024.0);
+        } else if (size < 1024 * 1024 * 1024) {
+            return String.format("%.2f MB", size / (1024.0 * 1024));
+        } else {
+            return String.format("%.2f GB", size / (1024.0 * 1024 * 1024));
+        }
+    }
+
 }

+ 231 - 0
app/src/main/java/com/uas/rd_equipment/util/MyLog.java

@@ -0,0 +1,231 @@
+package com.uas.rd_equipment.util;
+
+import android.annotation.SuppressLint;
+import android.os.Build;
+import android.support.annotation.RequiresApi;
+import android.util.Log;
+
+import com.uas.rd_equipment.application.PdaApplication;
+
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.attribute.BasicFileAttributes;
+import java.nio.file.attribute.FileTime;
+import java.text.SimpleDateFormat;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.ZoneId;
+import java.util.Date;
+
+/**
+ * 带日志文件输入,又可控开关的日志调试
+ */
+@SuppressLint("SimpleDateFormat")
+public class MyLog {
+    public static Boolean MYLOG_SWITCH = true; // 日志文件总开关
+    private static Boolean MYLOG_WRITE_TO_FILE = true;// 日志写入文件开关
+    private static char MYLOG_TYPE = 'v';// 输入日志类型,w代表只输出告警信息等,v代表输出所有信息
+    @SuppressLint("SdCardPath")
+//    private static String MYLOG_PATH_SDCARD_DIR1 = "/sdcard/MSShow";    // 日志文件在sdcard中的路径
+//    private static String MYLOG_PATH_SDCARD_DIR1 = "/data/data/" + PdaApplication.getmContext().getPackageName();  //APP包下创建的文件不需要权限
+    private static String MYLOG_PATH_SDCARD_DIR1 = "/sdcard/Android/data/" + PdaApplication.getmContext().getPackageName();  //模拟器的APP包下创建的文件
+    private static String MYLOG_PATH_SDCARD_DIR2 = "/Log";
+    private static String MYLOG_PATH_SDCARD_DIR = MYLOG_PATH_SDCARD_DIR1 + MYLOG_PATH_SDCARD_DIR2;
+    private static String MYLOGFILEName = "Log.txt";// 本类输出的日志文件名称
+
+    @SuppressLint("SimpleDateFormat")
+    private static SimpleDateFormat myLogSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 日志的输出格式
+
+    private static boolean isDebug = true;        //普通log的开关
+    private static String TAG = "英唐" + "-----------";
+
+    public static void e(String msg) {
+        if (isDebug) {
+            Log.e(TAG, msg);
+        }
+    }
+
+    public static void w(String msg) {
+        if (isDebug) {
+            Log.w(TAG, msg);
+        }
+    }
+
+    public static void d(String msg) {
+        if (isDebug) {
+            Log.d(TAG, msg);
+        }
+    }
+
+    public static void i(String msg) {
+        if (isDebug) {
+            Log.i(TAG, msg);
+        }
+    }
+
+    public static void w(String tag, Object msg) { // 警告信息
+        log(tag, msg.toString(), 'w');
+    }
+
+    public static void e(String tag, Object msg) { // 错误信息
+        log(tag, msg.toString(), 'e');
+    }
+
+    public static void d(String tag, Object msg) {// 调试信息
+        log(tag, msg.toString(), 'd');
+    }
+
+    public static void i(String tag, Object msg) {//
+        log(tag, msg.toString(), 'i');
+    }
+
+    public static void v(String tag, Object msg) {
+        log(tag, msg.toString(), 'v');
+    }
+
+    public static void w(String tag, String text) {
+        log(tag, text, 'w');
+    }
+
+    public static void e(String tag, String text) {
+        log(tag, text, 'e');
+    }
+
+    public static void d(String tag, String text) {
+        log(tag, text, 'd');
+    }
+
+    public static void i(String tag, String text) {
+        log(tag, text, 'i');
+    }
+
+    public static void v(String tag, String text) {
+        log(tag, text, 'v');
+    }
+
+    /**
+     * 根据tag, msg和等级,输出日志
+     */
+    private static void log(String tag, String msg, char level) {
+        msg = "\r\n" + msg;
+        if (MYLOG_SWITCH) {
+            if ('e' == level && ('e' == MYLOG_TYPE || 'v' == MYLOG_TYPE)) { // 输出错误信息
+                Log.e(tag, msg);
+            } else if ('w' == level && ('w' == MYLOG_TYPE || 'v' == MYLOG_TYPE)) {
+                Log.w(tag, msg);
+            } else if ('d' == level && ('d' == MYLOG_TYPE || 'v' == MYLOG_TYPE)) {
+                Log.d(tag, msg);
+            } else if ('i' == level && ('d' == MYLOG_TYPE || 'v' == MYLOG_TYPE)) {
+                Log.i(tag, msg);
+            } else {
+                Log.v(tag, msg);
+            }
+            if (MYLOG_WRITE_TO_FILE && 'd' == level) {
+                writeLogtoFile(String.valueOf(level), tag, msg);
+            }
+        }
+    }
+
+    /**
+     * 打开日志文件并写入日志,
+     **/
+    private static void writeLogtoFile(String mylogtype, String tag, String text) {
+    	// 新建或打开日志文件
+        isExist(MYLOG_PATH_SDCARD_DIR1);
+        isExist(MYLOG_PATH_SDCARD_DIR);
+        Date nowtime = new Date();
+        //		String needWriteFiel = logfile.format(nowtime);
+        String needWriteMessage = myLogSdf.format(nowtime) + "    " + mylogtype
+                + "    " + tag + "    " + text;
+        File file = new File(MYLOG_PATH_SDCARD_DIR, MYLOGFILEName);
+        try {
+            FileWriter filerWriter = new FileWriter(file, true);// 后面这个参数代表是不是要接上文件中原来的数据,不进行覆盖
+            BufferedWriter bufWriter = new BufferedWriter(filerWriter);
+            bufWriter.write(needWriteMessage);
+            bufWriter.newLine();
+            bufWriter.close();
+            filerWriter.close();
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
+
+    /**
+     * 删除制定的日志文件
+     */
+    public static void delFile() {// 删除日志文件
+        File file = new File(MYLOG_PATH_SDCARD_DIR, MYLOGFILEName);
+        if (file.exists()) {
+            file.delete();
+        }
+    }
+
+    public static void isExist(String path) {
+        File file = new File(path);
+        // 判断文件夹是否存在,如果不存在则创建文件夹
+        if (!file.exists()) {
+            file.mkdir();
+        }
+    }
+
+    /**
+     * 取消Android8.0的提示限制
+     */
+    @RequiresApi(api = Build.VERSION_CODES.O)
+    public static void delNotDayFile(){
+        File fileP = new File(MYLOG_PATH_SDCARD_DIR, MYLOGFILEName);
+        if (fileP.exists()) {
+            Path path = Paths.get(MYLOG_PATH_SDCARD_DIR + "/" + MYLOGFILEName); //文件路径
+            try {
+                // 获取文件的属性
+                BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class);
+                // 获取文件的创建时间
+                FileTime creationTime = attrs.creationTime();
+                // 转换为LocalDateTime以便于比较
+                //将FileTime对象转换为LocalDateTime以便于与当前日期进行比较。这里使用了系统的默认时区(ZoneId.systemDefault())。如果需要特定时区,可以替换为相应的ZoneId。
+                //通过LocalDateTime 的 toLocalDate()方法将时间转换为仅包含日期(忽略时间),然后与当前日期进行比较。
+                LocalDateTime creationDateTime = creationTime.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
+                LocalDateTime now = LocalDateTime.now();
+
+                // 比较是否为当天创建
+                if (isToday(creationDateTime)) {
+                    //System.out.println("文件是今天创建的。");
+                } else {
+                    //System.out.println("文件不是今天创建的。");
+                    delFile();  //删除不是当天日期
+                }
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+        }
+    }
+
+    // 辅助方法,检查日期是否为今天
+    @RequiresApi(api = Build.VERSION_CODES.O)
+    private static boolean isToday(LocalDateTime dateTime) {
+        LocalDate today = LocalDate.now();
+        LocalDate fileDate = dateTime.toLocalDate();
+        return today.equals(fileDate);
+    }
+
+    /**
+     * 判断文件是否为当天创建(使用最后修改时间),不同则删除
+     * Android 7.0推荐使用此方法
+     */
+    public static void isFileCreatedTodayDel() {
+        File fileP = new File(MYLOG_PATH_SDCARD_DIR, MYLOGFILEName);
+        if (fileP.exists()) {
+            if (!FileUtils.isFileCreatedToday(fileP)) {
+                delFile();  //删除不是当天创建的日期
+            }
+        }
+    }
+
+
+
+}

+ 4 - 3
app/src/main/java/com/uas/rd_equipment/util/VolleyRequest.java

@@ -98,7 +98,7 @@ public class VolleyRequest {
                 index++;
             }
         }
-
+        MyLog.d("aaa","*************************" + "\n" + "接口地址:" + url);
         PdaApplication.mRequestQueue.cancelAll(httpParams.getUrl());
 
         stringRequest = new StringRequest(httpParams.getMethod(), url,
@@ -106,7 +106,8 @@ public class VolleyRequest {
                     @Override
                     public void onResponse(String s) {
                         try {
-                            LogUtil.prinlnLongMsg("responseSucc", s);
+                            //LogUtil.prinlnLongMsg("responseSucc", s);
+                            MyLog.d("aaa","响应成功:" + s);
                             httpCallback.onSuccess(httpParams.getFlag(), s);
                         } catch (Exception e) {
                             e.printStackTrace();
@@ -118,7 +119,7 @@ public class VolleyRequest {
                     public void onErrorResponse(VolleyError volleyError) {
                         String errorToast = CommonUtil.showErrorToast(volleyError, false);
                         try {
-                            LogUtil.e("responErr", errorToast);
+                            //LogUtil.e("responErr", errorToast);
                             if (errorToast.length() >= 500) {
                                 httpCallback.onFail(httpParams.getFlag(), "请求异常");
                             } else {

+ 4 - 3
app/src/main/java/com/uas/rd_equipment/util/VollyRequest.java

@@ -91,7 +91,7 @@ public class VollyRequest {
                 index++;
             }
         }
-
+        MyLog.d("aaa","*************************" + "\n" + "接口地址:" + url);
         PdaApplication.mRequestQueue.cancelAll(httpParams.getUrl());
 
         stringRequest = new StringRequest(httpParams.getMethod(), url,
@@ -99,7 +99,8 @@ public class VollyRequest {
                     @Override
                     public void onResponse(String s) {
                         try {
-                            LogUtil.prinlnLongMsg("responseSucc", s);
+                            //LogUtil.prinlnLongMsg("responseSucc", s);
+                            MyLog.d("aaa","响应成功:" + s);
                             httpCallback.onSuccess(httpParams.getFlag(), s);
                         } catch (Exception e) {
                             e.printStackTrace();
@@ -111,7 +112,7 @@ public class VollyRequest {
                     public void onErrorResponse(VolleyError volleyError) {
                         String errorToast = CommonUtil.showErrorToast(volleyError, false);
                         try {
-                            LogUtil.e("responErr", errorToast);
+                            //LogUtil.e("responErr", errorToast);
                             if (errorToast.length() >= 200) {
                                 httpCallback.onFail(httpParams.getFlag(), "请求异常");
                             } else {