我是
靠谱客的博主
寂寞发夹,最近开发中收集的这篇文章主要介绍
Android logcat保存当前应用程序的日志并上传服务器或指定邮箱,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
分享一个项目中用到的日志统计并提交服务器的日志工具类.
通过过得当前app的PID,采用命令行的方式实用logcat工具过滤日志。
源码如下:
项目地址:http://code.google.com/p/andutils/
001 | package org.and.util; |
003 | import java.io.BufferedReader; |
005 | import java.io.FileNotFoundException; |
006 | import java.io.FileOutputStream; |
007 | import java.io.IOException; |
008 | import java.io.InputStreamReader; |
009 | import java.util.ArrayList; |
010 | import java.util.List; |
012 | import android.content.Context; |
013 | import android.os.Environment; |
017 | * TODO: log日志统计保存、上传-工具类 |
019 | * @author hljdrl@gmail.com |
021 | * @date 2012-8-27 上午11:43:52 |
025 | public class LogcatHelper { |
027 | private static LogcatHelper INSTANCE = null ; |
029 | private static String PATH_LOGCAT ; |
031 | private LogDumper mLogDumper = null ; |
033 | private Context mContext; |
043 | public static void init(Context context) |
047 | StringBuffer LogPath = new StringBuffer(); |
049 | LogPath.append(Environment.getExternalStorageDirectory()); |
051 | LogPath.append( "/Android/data/" ); |
053 | LogPath.append(context.getPackageName()).append( "/" ); |
055 | LogPath.append( "logs" ).append( "/" ); |
057 | PATH_LOGCAT = LogPath.toString(); |
061 | File file = new File(PATH_LOGCAT); |
071 | public static LogcatHelper getInstance(Context context) |
075 | if (INSTANCE == null ){ |
077 | INSTANCE = new LogcatHelper(context); |
085 | private LogcatHelper(Context context) { |
089 | mPId = android.os.Process.myPid(); |
093 | public void start() { |
095 | if (mLogDumper== null ){ |
097 | mLogDumper = new LogDumper(String.valueOf(mPId),PATH_LOGCAT); |
109 | if (mLogDumper!= null ){ |
111 | mLogDumper.stopLogs(); |
119 | public void sendLogMessage(Context context,String user) |
123 | if (mLogDumper!= null ){ |
125 | mLogDumper.setLogFileLock( true ); |
127 | String file = mLogDumper.getLogFileName(); |
129 | File sendFile = new File(file); |
131 | if (sendFile.exists() && sendFile.length()> 2000 ){ |
135 | EmailHelper.sendMail(context, user, file); |
137 | } catch (Exception ex){ |
139 | ex.printStackTrace(); |
143 | File newFile = new File(file); |
147 | newFile.createNewFile(); |
149 | } catch (IOException e) { |
157 | mLogDumper.setLogFileLock( false ); |
163 | private class LogDumper extends Thread{ |
167 | private Process logcatProc; |
169 | private BufferedReader mReader = null ; |
171 | private boolean mRunning = false ; |
175 | private final String mPID; |
177 | private FileOutputStream out = null ; |
179 | private List<String> logsMessage = new ArrayList<String>(); |
181 | private boolean mLogFileLock = false ; |
183 | private String logFileName; |
185 | public void setLogFileLock( boolean lock){ |
191 | public boolean isLogFileLock() |
199 | public LogDumper(String pid,String file) { |
201 | mPID = String.valueOf(pid); |
205 | File mFile = new File(fileName, "error.txt" ); |
211 | mFile.createNewFile(); |
213 | } catch (IOException e) { |
223 | logFileName = mFile.toString(); |
225 | out = new FileOutputStream(mFile, true ); |
227 | } catch (FileNotFoundException e) { |
235 | * 日志等级:*:v , *:d , *:w , *:e , *:f , *:s |
237 | * 显示当前mPID程序的 E和W等级的日志. |
241 | cmds = "logcat *:e *:w | grep "(" +mPID+ ")"" ; |
245 | public String getLogFileName() |
253 | public void stopLogs() { |
259 | private boolean checkFileMaxSize(String file){ |
261 | File sizefile = new File(file); |
263 | if (sizefile.exists()){ |
267 | if (sizefile.length()> 1572864 ){ |
293 | System.out.println( "LogCatHelper'" ); |
299 | logcatProc = Runtime.getRuntime() |
305 | mReader = new BufferedReader( new InputStreamReader( |
307 | logcatProc.getInputStream()), 1024 ); |
311 | while (mRunning && (line = mReader.readLine()) != null ) { |
319 | if (line.length() == 0 ) { |
329 | boolean maxSize = checkFileMaxSize(getLogFileName()); |
335 | sendLogMessage(mContext, DeviceHelper.getInstance(mContext).getImei()); |
339 | if (isLogFileLock()) { |
341 | if (line.contains(mPID)){ |
343 | logsMessage.add(line.getBytes() + "n" ); |
349 | if (logsMessage.size()> 0 ){ |
351 | for (String _log:logsMessage){ |
353 | out.write(_log.getBytes()); |
363 | * 再次过滤日志,筛选当前日志中有 mPID 则是当前程序的日志. |
367 | if (line.contains(mPID)){ |
369 | out.write(line.getBytes()); |
371 | out.write( "n" .getBytes()); |
385 | } catch (IOException e) { |
393 | if (logcatProc != null ) { |
395 | logcatProc.destroy(); |
401 | if (mReader != null ) { |
409 | } catch (IOException e) { |
423 | } catch (IOException e) { |
最后
以上就是寂寞发夹为你收集整理的Android logcat保存当前应用程序的日志并上传服务器或指定邮箱的全部内容,希望文章能够帮你解决Android logcat保存当前应用程序的日志并上传服务器或指定邮箱所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复