欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

android日志打印工具类,android日志工具类,给大家献上一款好用的日志

来源: javaer 分享于  点击 36442 次 点评:221

android日志打印工具类,android日志工具类,给大家献上一款好用的日志


给大家献上一款好用的日志打印工具。大家在平时的开发中用的最多的可能就是Log.i("",""),Log.e("","")...,在要查看的日志比较少的情况下,这种方法用起来确实方便,很容易写,也很容易查看,然而不知道大家有没有遇到过这样一种情况,如果你要查看的数据量非常大,然后用Log类打印出来以后,却发现只显示了一部分数据,大部分数据被截断了。是的,log打印出来的日志长度是有限的,我之前由于要分析一段从服务器获取的数据,数据量比较大,用log类打印之后只能看到一部分数据,大部分数据都被截断了,后来就自己写了一个日志打印工具类,把日志打印到手机SD卡上,这样就能看到完整的数据,该工具我已封装好,分享给大家。```javapublic 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("\n[" + df.format(date) +"--"+ tf.format(date) + "]\n");            fw.write(title + ": " + log);            fw.write("\n\n");            fw.close();        }    } catch (IOException e) {        e.printStackTrace();    }}

}

调用:```javapublic class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        LogTools.dailyLog("我的日志", "----33222211111111118838777777777777766666666666444444444");    }}
相关栏目:

用户点评