Android logcat保存当前应用程序的日志并上传服务器或指定邮箱

来源:互联网 发布:淘宝店铺用别人的图片 编辑:程序博客网 时间:2024/06/11 10:20

分享一个项目中用到的日志统计并提交服务器的日志工具类.

通过过得当前app的PID,采用命令行的方式实用logcat工具过滤日志。

源码如下:

项目地址:http://code.google.com/p/andutils/

view source
print?
001package org.and.util;
002 
003import java.io.BufferedReader;
004import java.io.File;
005import java.io.FileNotFoundException;
006import java.io.FileOutputStream;
007import java.io.IOException;
008import java.io.InputStreamReader;
009import java.util.ArrayList;
010import java.util.List;
011 
012import android.content.Context;
013import android.os.Environment;
014  
015 
016/**
017 * TODO: log日志统计保存、上传-工具类
018 *
019 * @author hljdrl@gmail.com
020 
021 * @date 2012-8-27 上午11:43:52
022 
023 */
024 
025public class LogcatHelper {
026 
027    private static LogcatHelper INSTANCE = null;
028 
029    private static String PATH_LOGCAT ;
030 
031    private LogDumper mLogDumper = null;
032 
033    private Context mContext;
034 
035    private int mPId;
036 
037    /**
038 
039     * 初始化目录
040 
041     * */
042 
043    public static void init(Context context)
044 
045    {
046 
047    StringBuffer LogPath = new StringBuffer();
048 
049    LogPath.append(Environment.getExternalStorageDirectory());
050 
051    LogPath.append("/Android/data/");
052 
053    LogPath.append(context.getPackageName()).append("/");
054 
055    LogPath.append("logs").append("/");
056 
057    PATH_LOGCAT = LogPath.toString();
058 
059    //
060 
061File file =new  File(PATH_LOGCAT);
062 
063if(!file.exists()){
064 
065file.mkdirs();
066 
067}
068 
069    }
070 
071    public static LogcatHelper getInstance(Context context)
072 
073    {
074 
075    if(INSTANCE == null){
076 
077    INSTANCE = new LogcatHelper(context);
078 
079    }
080 
081    return INSTANCE;
082 
083    }
084 
085    private LogcatHelper(Context context) {
086 
087    mContext = context;
088 
089    mPId = android.os.Process.myPid();
090 
091}
092 
093public void start() {
094 
095if(mLogDumper==null){
096 
097mLogDumper = new LogDumper(String.valueOf(mPId),PATH_LOGCAT);
098 
099mLogDumper.start();
100 
101}
102 
103}
104 
105public void stop()
106 
107{
108 
109if(mLogDumper!=null){
110 
111mLogDumper.stopLogs();
112 
113mLogDumper = null;
114 
115}
116 
117}
118 
119public  void sendLogMessage(Context context,String user)
120 
121{
122 
123if(mLogDumper!=null){
124 
125mLogDumper.setLogFileLock(true);
126 
127String file  = mLogDumper.getLogFileName();
128 
129File sendFile = new File(file);
130 
131if(sendFile.exists() && sendFile.length()>2000){
132 
133try{
134 
135EmailHelper.sendMail(context, user, file);
136 
137}catch(Exception ex){
138 
139ex.printStackTrace();
140 
141}
142 
143File newFile = new File(file);
144 
145try {
146 
147newFile.createNewFile();
148 
149catch (IOException e) {
150 
151e.printStackTrace();
152 
153}
154 
155}
156 
157mLogDumper.setLogFileLock(false);
158 
159}
160 
161}
162 
163private class LogDumper extends Thread{
164 
165String fileName;
166 
167private Process logcatProc;
168 
169   private BufferedReader mReader = null;
170 
171private boolean mRunning = false;
172 
173String cmds=null;
174 
175private final String mPID;
176 
177private FileOutputStream out = null;
178 
179private List<String> logsMessage = new ArrayList<String>();
180 
181private boolean mLogFileLock = false;
182 
183private String logFileName;
184 
185public void setLogFileLock(boolean lock){
186 
187mLogFileLock = lock;
188 
189}
190 
191public boolean isLogFileLock()
192 
193{
194 
195return mLogFileLock;
196 
197}
198 
199public LogDumper(String pid,String file) {
200 
201mPID = String.valueOf(pid);
202 
203fileName = file;
204 
205File mFile = new File(fileName,"error.txt");
206 
207if(!mFile.exists()){
208 
209try {
210 
211mFile.createNewFile();
212 
213catch (IOException e) {
214 
215e.printStackTrace();
216 
217}
218 
219}
220 
221try {
222 
223logFileName = mFile.toString();
224 
225out = new FileOutputStream(mFile,true);
226 
227catch (FileNotFoundException e) {
228 
229e.printStackTrace();
230 
231}
232 
233/**
234 
235* 日志等级:*:v  , *:d  , *:w , *:e , *:f  , *:s
236 
237* 显示当前mPID程序的 E和W等级的日志.
238 
239* */
240 
241cmds ="logcat *:e *:w | grep \"("+mPID+")\"";
242 
243}
244 
245public String getLogFileName()
246 
247{
248 
249return logFileName;
250 
251}
252 
253public void stopLogs() {
254 
255mRunning = false;
256 
257}
258 
259private boolean checkFileMaxSize(String file){
260 
261File sizefile = new File(file);
262 
263if(sizefile.exists()){
264 
265//1.5MB
266 
267if(sizefile.length()>1572864){
268 
269return true;
270 
271}
272 
273else {
274 
275return false;
276 
277}
278 
279}else {
280 
281return false;
282 
283}
284 
285}
286 
287  
288 
289@Override
290 
291public void run() {
292 
293System.out.println("LogCatHelper'");
294 
295mRunning = true;
296 
297try {
298 
299logcatProc = Runtime.getRuntime()
300 
301.exec(cmds);
302 
303  
304 
305mReader = new BufferedReader(new InputStreamReader(
306 
307logcatProc.getInputStream()), 1024);
308 
309String line = null;
310 
311while (mRunning && (line = mReader.readLine()) != null) {
312 
313if (!mRunning) {
314 
315break;
316 
317}
318 
319if (line.length() == 0) {
320 
321continue;
322 
323}
324 
325synchronized (out) {
326 
327if (out != null) {
328 
329boolean maxSize = checkFileMaxSize(getLogFileName());
330 
331if(maxSize){
332 
333//文件大小超过1.5mb
334 
335sendLogMessage(mContext, DeviceHelper.getInstance(mContext).getImei());
336 
337}
338 
339if (isLogFileLock()) {
340 
341if(line.contains(mPID)){
342 
343logsMessage.add(line.getBytes() + "\n");
344 
345}
346 
347else {
348 
349if(logsMessage.size()>0){
350 
351for(String _log:logsMessage){
352 
353out.write(_log.getBytes());
354 
355}
356 
357logsMessage.clear();
358 
359}
360 
361/**
362 
363* 再次过滤日志,筛选当前日志中有 mPID 则是当前程序的日志.
364 
365* */
366 
367if(line.contains(mPID)){
368 
369out.write(line.getBytes());
370 
371out.write("\n".getBytes());
372 
373}
374 
375}
376 
377}
378 
379}
380 
381  
382 
383}
384 
385catch (IOException e) {
386 
387e.printStackTrace();
388 
389return;
390 
391finally {
392 
393if (logcatProc != null) {
394 
395logcatProc.destroy();
396 
397logcatProc = null;
398 
399}
400 
401if (mReader != null) {
402 
403try {
404 
405mReader.close();
406 
407mReader = null;
408 
409catch (IOException e) {
410 
411e.printStackTrace();
412 
413}
414 
415}
416 
417if(out!=null){
418 
419try {
420 
421out.close();
422 
423catch (IOException e) {
424 
425e.printStackTrace();
426 
427}
428 
429out = null;
430 
431}
432 
433}
434 
435}
436 
437}
438 
439  
440 
441}
0 0
原创粉丝点击