分享一个Android 分类打印,及日志记录工具
2017-05-31 10:55
579 查看
打印工具
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 * 9e83 @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略多,所以在使用过程的略微繁琐,但能在全局控制打印,为调试过程筛选信息有一定程度的提高,该工具还提供了各类输出日志的输出。 如有更好的建议,请留言,谢谢!
相关文章推荐
- android中定制自己的日志打印工具
- 一个打印Log日志,好用的工具logger
- Go/Python/Erlang编程语言对比分析及示例 基于RabbitMQ.Client组件实现RabbitMQ可复用的 ConnectionPool(连接池) 封装一个基于NLog+NLog.Mongo的日志记录工具类LogUtil 分享基于MemoryCache(内存缓存)的缓存工具类,C# B/S 、C/S项目均可以使用!
- Robot: 一个记录和回放Android input event(touch)的工具
- Android日志服务例子,将日志记录在文件中并每天生成一个日志文件
- android日志服务,将日志记录在log文件中并每天生成一个日志文件
- 记录和分享一个好用的小工具 Everything
- LogCook 一个简单实用的Android日志管理工具
- Android日志服务例子,将日志记录在文件中并每天生成一个日志文件
- Android日志打印工具
- Android日志服务例子,将日志记录在文件中并每天生成一个日志文件
- Android日志服务例子,将日志记录在文件中并每天生成一个日志文件
- java 一个日志记录异常信息的小工具
- 解决:Android手机在AndroidStudio或Eclipse等开发工具调试时Logcat不打印日志信息
- android的Log日志打印管理工具类(一)
- 分享一个Android工具下载地址
- 分享一个Android中的图片处理工具
- 分享一个简易的程序日志记录C++类
- 分享一个牛哄哄的Android工具集合类
- Android日志记录工具