首页 技术 正文
技术 2022年11月15日
0 收藏 550 点赞 2,876 浏览 4081 个字

给大家分享一个项目中用到的日志统计并提交服务器的日志工具类.
通过过得当前app的PID,采用命令行的方式实用logcat工具过滤日志。
代码区:

package org.and.util;import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;import android.content.Context;
import android.os.Environment;/**
* TODO: log日志统计保存、上传-工具类
*
* @author hljdrl@gmail.com* @date 2012-8-27 上午11:43:52*/public class LogcatHelper {private static LogcatHelper INSTANCE = null;private static String PATH_LOGCAT ;private LogDumper mLogDumper = null;private Context mContext;private int mPId;/*** 初始化目录* */public static void init(Context context){StringBuffer LogPath = new StringBuffer();LogPath.append(Environment.getExternalStorageDirectory());LogPath.append("/Android/data/");LogPath.append(context.getPackageName()).append("/");LogPath.append("logs").append("/");PATH_LOGCAT = LogPath.toString();//File file =new File(PATH_LOGCAT);if(!file.exists()){file.mkdirs();}}public static LogcatHelper getInstance(Context context){if(INSTANCE == null){INSTANCE = new LogcatHelper(context);}return INSTANCE;}private LogcatHelper(Context context) {mContext = context;mPId = android.os.Process.myPid();}public void start() {if(mLogDumper==null){mLogDumper = new LogDumper(String.valueOf(mPId),PATH_LOGCAT);mLogDumper.start();}}public void stop(){if(mLogDumper!=null){mLogDumper.stopLogs();mLogDumper = null;}}public void sendLogMessage(Context context,String user){if(mLogDumper!=null){mLogDumper.setLogFileLock(true);String file = mLogDumper.getLogFileName();File sendFile = new File(file);if(sendFile.exists() && sendFile.length()>2000){try{EmailHelper.sendMail(context, user, file);}catch(Exception ex){ex.printStackTrace();}File newFile = new File(file);try {newFile.createNewFile();} catch (IOException e) {e.printStackTrace();}}mLogDumper.setLogFileLock(false);}}private class LogDumper extends Thread{String fileName;private Process logcatProc;private BufferedReader mReader = null;private boolean mRunning = false;String cmds=null;private final String mPID;private FileOutputStream out = null;private List<String> logsMessage = new ArrayList<String>();private boolean mLogFileLock = false;private String logFileName;public void setLogFileLock(boolean lock){mLogFileLock = lock;}public boolean isLogFileLock(){return mLogFileLock;}public LogDumper(String pid,String file) {mPID = String.valueOf(pid);fileName = file;File mFile = new File(fileName,"error.txt");if(!mFile.exists()){try {mFile.createNewFile();} catch (IOException e) {e.printStackTrace();}}try {logFileName = mFile.toString();out = new FileOutputStream(mFile,true);} catch (FileNotFoundException e) {e.printStackTrace();}/*** 日志等级:*:v , *:d , *:w , *:e , *:f , *:s* 显示当前mPID程序的 E和W等级的日志.* */cmds ="logcat *:e *:w | grep \"("+mPID+")\"";}public String getLogFileName(){return logFileName;}public void stopLogs() {mRunning = false;}private boolean checkFileMaxSize(String file){File sizefile = new File(file);if(sizefile.exists()){//1.5MBif(sizefile.length()>1572864){return true;}else {return false;}}else {return false;}}@Overridepublic void run() {System.out.println("LogCatHelper'");mRunning = true;try {logcatProc = Runtime.getRuntime().exec(cmds);mReader = new BufferedReader(new InputStreamReader(logcatProc.getInputStream()), 1024);String line = null;while (mRunning && (line = mReader.readLine()) != null) {if (!mRunning) {break;}if (line.length() == 0) {continue;}synchronized (out) {if (out != null) {boolean maxSize = checkFileMaxSize(getLogFileName());if(maxSize){//文件大小超过1.5mbsendLogMessage(mContext, DeviceHelper.getInstance(mContext).getImei());}if (isLogFileLock()) {if(line.contains(mPID)){logsMessage.add(line.getBytes() + "\n");}} else {if(logsMessage.size()>0){for(String _log:logsMessage){out.write(_log.getBytes());}logsMessage.clear();}/*** 再次过滤日志,筛选当前日志中有 mPID 则是当前程序的日志.* */if(line.contains(mPID)){out.write(line.getBytes());out.write("\n".getBytes());}}}}}} catch (IOException e) {e.printStackTrace();return;} finally {if (logcatProc != null) {logcatProc.destroy();logcatProc = null;}if (mReader != null) {try {mReader.close();mReader = null;} catch (IOException e) {e.printStackTrace();}}if(out!=null){try {out.close();} catch (IOException e) {e.printStackTrace();}out = null;}}}}}

详细说明:http://android.662p.com/thread-216-1-1.html

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,491
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,493
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,294