zoukankan      html  css  js  c++  java
  • android开发必备日志打印工具类

    今天给大家献上一款好用的日志打印工具。大家在平时的开发中用的最多的可能就是Log.i("",""),Log.e("","")...,在要查看的日志比较少的情况下,这种方法用起来确实方便,很容易写,也很容易查看,然而不知道大家有没有遇到过这样一种情况,如果你要查看的数据量非常大,然后用Log类打印出来以后,却发现只显示了一部分数据,大部分数据被截断了。
    是的,log打印出来的日志长度是有限的,我之前由于要分析一段从服务器获取的数据,数据量比较大,用log类打印之后只能看到一部分数据,大部分数据都被截断了,后来就自己写了一个日志打印工具类,把日志打印到手机SD卡上,这样就能看到完整的数据,该工具我已封装好,分享给大家。

    public class LogTools {
    
        public static void dailyLog(String title, String log) {
            try {
                // 如果外部存储卡存在且可读写
                if (Environment.getExternalStorageState().equals(
                        Environment.MEDIA_MOUNTED)) {
                    Date date = new Date();
                    SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
                    SimpleDateFormat tf = new SimpleDateFormat("HH:mm:ss:SSS");
                    String path = Environment.getExternalStorageDirectory()
                            .getAbsolutePath()
                            + File.separator
                            + "mylog"
                            + File.separator
                            + "debuglog["
                            + df.format(date)
                            + "]"
                            + ".txt";
                    File file = new File(path);
                    // 如果文件不存在,则重新创建
                    if (!file.exists()) {
                        // 最后一级是文件,前面是路径,如果路径不存在则创建路径
                        if (!file.getParentFile().exists()) {
                            file.getParentFile().mkdirs();
                        }
                        // 创建日志文件
                        file.createNewFile();
                    }
                    //写日志
                    FileWriter fw = new FileWriter(file, true);
                    fw.flush();
                    fw.write("
    [" + df.format(date) +"--"+ tf.format(date) + "]
    ");
                    fw.write(title + ": " + log);
                    fw.write("
    
    ");
                    fw.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    调用:

    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            LogTools.dailyLog("我的日志", "----33222211111111118838777777777777766666666666444444444");
        }
    }

    本工具源码下载

  • 相关阅读:
    Log4J输出日志到WEB工程目录的实现方法
    MyEclipse 10 中增加svn插件
    Web 项目添加log4j
    ruby on rails 之旅 第一章 ubuntu11.10安装
    ruby on rails 之旅 第一章 ubuntu12.04安装
    centos 6.3 server 安装mysql5
    技术实践第四期|解读移动开发者日常性能监控平台应用
    电脑一族预防颈椎劳损八法
    方便的使用单击和双击更新DataGrid中的数据的例子(转载)
    不重复随机数列生成算法
  • 原文地址:https://www.cnblogs.com/qitian1/p/6461767.html
Copyright © 2011-2022 走看看