分享一个Android 分类打印,及日志记录工具

本文介绍了一款自定义的日志打印工具,该工具能够全局控制打印,方便调试过程中的信息筛选,并提供多种类型的日志输出。此外,还支持将日志信息输出到指定文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

打印工具

public class LogI {
    /**全局输出*/
    private static boolean IS_SHOW = true;

    private static final String PROJECT_NAME = PROJECT_NAME;//项目名

    /**图片文件架路径*/
    public static final String PIC_FOLDER = Environment.getExternalStorageDirectory().getAbsolutePath()
            +"/"+PROJECT_NAME;
    /**语音文件架路径*/
    public static final String VOICE_FOLDER = Environment.getExternalStorageDirectory().getAbsolutePath()
            +"/"+PROJECT_NAME;

    /**错误输出的文件路径*/
    private static final String ERROR_PATH = Environment.getExternalStorageDirectory().getAbsolutePath()
            +"/"+PROJECT_NAME+"/error.txt";
    /**输出记录的文件路径*/
    private static final String LOG_PATH = Environment.getExternalStorageDirectory().getAbsolutePath()
            +"/"+PROJECT_NAME+"/log.txt";
    //  /**输出调试信息的文件路径*/
    private static final String DEBUG_PATH = Environment.getExternalStorageDirectory().getAbsolutePath()
            +"/"+PROJECT_NAME+"/debug.txt";
    /**时间格式:2015-12-16 13:15:20 999,999为毫秒值*/
    private static final String TIME_SYTLE = "yyyy-MM-dd HH:mm:ss SSS";
    private static final String TIME_SYTLE_SHORT = "dd日 HH:mm:ss SSS";
    /**格式化字符串*/
    private static final SimpleDateFormat formate = new SimpleDateFormat(TIME_SYTLE);
    private static final SimpleDateFormat formateShort = new SimpleDateFormat(TIME_SYTLE_SHORT);

    private static String errorPath = ERROR_PATH;
    private static String logPath = LOG_PATH;
    private static String debugPath = DEBUG_PATH;

    private static final String I = "i";
    private static final String E = "e";
    private static final String W = "w";
    private static final String V = "v";
    private static final String D = "d";

    /**打印map*/
    public static String mapToString(Map map){
        StringBuilder sb = new StringBuilder();
        Iterator iter = map.entrySet().iterator();
        while(iter.hasNext()){
            Map.Entry entry = (Map.Entry) iter.next();
            String key = (String) entry.getKey();
            Object val = (Object) entry.getValue();
            sb.append(key+"="+val+" ");
        }
        return sb.toString();
    }


    /**设置全局显示*/
    public static void setGlobalShow(boolean isShow){
        IS_SHOW = isShow;
        i("LogI", "setGlobalShow", false, "设置全局Log打印:"+IS_SHOW);
    }
    /**获取全局显示*/
    public static boolean isGlobalShow(){
        return IS_SHOW;
    }
    /**设置错误输出的文件路径*/
    public static void setErrorPath(String _errorPath){
        errorPath = _errorPath;
        i("LogI", "setErrorPath", false, "设置错误日志输出路径:"+errorPath);
    }
    /**获取错误输出的文件路径*/
    public static String getErrorPath(){
        return errorPath;
    }
    /**设置输出的文件路径*/
    public static void setLogPath(String _logPath){
        logPath = _logPath;
        i("LogI", "setLogPath", false, "设置普通日志输出路径:"+logPath);
    }
    /**获取输出的文件路径*/
    public static String getLogPath(){
        return logPath;
    }

    /**
     * 正常日志输出到文件
     * @param msgs
     */
    public static void  writeToFile(String... msgs){
        if("".equals(logPath)){
            e("LogI", "writeToFile", true, "日志路劲为空!");
            return;
        }
        //写入文件
        try {
            writeFile(logPath,getCurrentTime()+":\n"+connectStringBySign(msgs, ","),true);
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }

    /**
     * log.i输出
     * isShow:true 一定输出
     *        false 当全局输出IS_SHOW为true时有输出
     */
    public static void i(String className,String methodName,boolean isShow,String... msgs){
        if(isShow||IS_SHOW){
            Log.i(I+"_"+className+"_"+methodName, connectStringBySign(msgs, ","));
        }
    }

    /**
     * log.e输出
     * isShow:true 一定输出
     *        false 当全局输出IS_SHOW为true时有输出
     */
    public static void e(String className,String methodName,boolean isShow,String... msgs){
        if(isShow||IS_SHOW){
            Log.e(E+"_"+className+"_"+methodName, connectStringBySign(msgs, ","));
        }
    }

    /**
     * log.e输出
     * isShow:true 一定输出
     *        false 当全局输出IS_SHOW为true时有输出
     * isWriteFile:true 将信息写到文件中
     */
    public static void e(String className,String methodName,boolean isShow,boolean isWriteFile,String... msgs){
        String tag = E+"_"+className+"_"+methodName;
        String msg = connectStringBySign(msgs, ",");
        if(isShow||IS_SHOW){
            Log.e(tag, msg);
        }
        if(isWriteFile){
            if("".equals(errorPath)){
                e(className, methodName, isShow, "错误日志路劲为空!");
                return;
            }
            //写入文件
            try {
                writeFile(errorPath,tag+":\n"+msg,true);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }

    /**
     * log.e输出
     * isShow:true 一定输出
     *        false 当全局输出IS_SHOW为true时有输出
     * isWriteFile:true 将信息写到文件中
     * Exception:输出错误信息
     */

    public static void writeErrorInfoToFile(String className,String methodName,Exception ex){
        String tag = E+"_"+className+"_"+methodName;
        //添加崩溃时间
        String currentDate = getCurrentTime();
        //组织文本
        String text = "\n"+"手机型号:"+android.os.Build.MODEL +
                ",sdk版本:"+ android.os.Build.VERSION.SDK +
                ",android版本:" + android.os.Build.VERSION.RELEASE +"  时间:"
                + currentDate + ",错误信息:"+"\n" + ex.getMessage();
        //写入文件
        try {
            writeFile(ERROR_PATH,tag+":\n"+text,true);
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }

    /**
     * 将内容写入指定路径的
     * @param className
     * @param methodName
     * @param tag
     * @param path
     * @param msgs
     */
    public static void e(String className,String methodName,String tag,String path,String ...msgs){
        if(!ConfigValue.isDebug){
            return;
        }
        Date date = new Date();
        tag = formateShort.format(date)+" "+className +" "+ methodName +" "+ tag;
        String msg = connectStringBySign(msgs, ",");
        if("".equals(path)){
            e(className, methodName,true, "错误日志路劲为空!");
            return;
        }
        //写入文件
        try {
            writeFile(path,tag+":\n"+msg,true);
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }

    /**将调试信息写入调试路径*/
    public static void writeDebugInfo(String className,String methodName,String ...msgs){
        if(!ConfigValue.isDebug){
            return;
        }
        Date date = new Date();
        String tag = formate.format(date)+" "+className +" "+ methodName;
        String msg = connectStringBySign(msgs, ",");
        if("".equals(debugPath)){
            e(className, methodName,true, "错误日志路劲为空!");
            return;
        }
        //写入文件
        try {
            writeFile(debugPath,tag+":"+msg,true);
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }

    /**
     * log.w输出
     * isShow:true 一定输出
     *        false 当全局输出IS_SHOW为true时有输出
     */
    public static void w(String className,String methodName,boolean isShow,String... msgs){
        if(isShow||IS_SHOW){
            Log.w(W+"_"+className+"_"+methodName, connectStringBySign(msgs, ","));
        }
    }

    /**
     * log.v输出
     * isShow:true 一定输出
     *        false 当全局输出IS_SHOW为true时有输出
     */
    public static void v(String className,String methodName,boolean isShow,String... msgs){
        if(isShow||IS_SHOW){
            Log.v(V+"_"+className+"_"+methodName, connectStringBySign(msgs, ","));
        }
    }

    /**
     * log.d输出
     * isShow:true 一定输出
     *        false 当全局输出IS_SHOW为true时有输出
     */
    public static void d(String className,String methodName,boolean isShow,String... msgs){
        if(isShow||IS_SHOW){
            Log.d(D+"_"+className+"_"+methodName, connectStringBySign(msgs, ","));
        }
    }

    /**把strs用sign连接*/
    private static String connectStringBySign(String[] strs,String sign){
        if(strs.length<=0){
            return "";
        }
        StringBuilder sb = new StringBuilder();
        for(int i=0;i<strs.length-1;i++){
            sb.append(strs[i]);
            sb.append(sign);
        }
        sb.append(strs[strs.length-1]);
        return sb.toString();
    }

    /**
     * 向指定文件中写入字符串
     *
     * @param path
     * @param text
     * @param isAppend
     */
    private static void writeFile(String path,String text, boolean isAppend)
            throws IOException {
        File file = new File(path);
        e("LogI", "writeFile", false, path);
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        if (!file.exists()) {
            file.createNewFile();
        }

        FileWriter fw = null;
        try {
            fw = new FileWriter(file, isAppend);
            fw.write("\n"+text);
            fw.flush();
        } catch (FileNotFoundException e) {
            throw e;
        } finally {
            if (fw != null) {
                fw.close();
            }
        }
    }
    /**
     * 获取当前时间
     * @return
     */
    public static String getCurrentTime(){
        SimpleDateFormat sdf = new SimpleDateFormat(TIME_SYTLE);
        return sdf.format(new Date());
    }


    /**把对应路径的文件放到指定文件下*/
    public static void putFileToPath(String filePath,String newFilePath){
        Log.i("ico"+" filePath", filePath);
        Log.i("ico"+" newFilePath", newFilePath);
        FileInputStream is = null;
        FileOutputStream os = null;
        try {
            File file = new File(filePath);
            Log.i("ico"+" file.exists()", "file.exists():"+file.exists());
            Log.i("ico"+" file.isDirectory()", "file.isDirectory():"+file.isDirectory());
            if(!file.exists()){
                Log.e("ico", "文件不存在");
                return ;
            }
            File newFile = new File(newFilePath);
            if(!newFile.getParentFile().exists()){
                newFile.getParentFile().mkdirs();
            }
            if(!newFile.exists()){

                newFile.createNewFile();

            }
            is = new FileInputStream(file);
            //打开文件
            os = new FileOutputStream(newFile);
            byte[] buffer = new byte[1024];
            int count = 0;
            // 将文件拷贝到目的地
            while ((count = is.read(buffer)) > 0) {
                //              写入即拷贝
                os.write(buffer, 0, count);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            try {
                if(is!=null)
                    is.close();
                if(os!=null)
                    os.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }
}
该工具的参数比系统的Log略多,所以在使用过程的略微繁琐,但能在全局控制打印,为调试过程筛选信息有一定程度的提高,该工具还提供了各类输出日志的输出。
如有更好的建议,请留言,谢谢!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值