android自带日志放的什么地方,Android 收集日志存放文件夹

有时候我们需要将打包好的apk给远端的客户使用测试,我们就需要将log打印到手机内置的文件夹中,以便于后面自己在解决bug的时候快速定位。不过我们当然不希望自己的项目有何种无理由的bug和致命的崩溃

收集log到文件夹中

有时候程序出现异常停止,而logcat 的相关信息很快会被冲掉,因而不能够及时的获取异常信息。(所有logUtil封装的也可以保存)

在这推荐单例模式对这个类进行操作

/**

* - Created by Luke on 2017/8/16

*/

public class LogcatManager {

private static LogcatManager INSTANCE = null;

//logcat的路径地址

private static String PATH_LOGCAT;

private LogDumper mLogDumper = null;

private int mPId;

private SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyyMMdd");

private SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

public static LogcatManager getInstance() {

if (INSTANCE == null) {

INSTANCE = new LogcatManager();

}

return INSTANCE;

}

private LogcatManager() {

mPId = android.os.Process.myPid();

}

private void setFolderPath(String folderPath) {

File folder = new File(folderPath);

if (!folder.exists()) {

folder.mkdirs();

}

if (!folder.isDirectory())

throw new IllegalArgumentException("The logcat folder path is not a directory: " + folderPath);

PATH_LOGCAT = folderPath.endsWith("/") ? folderPath : folderPath + "/";

}

public void start(String saveDirectoy) {

setFolderPath(saveDirectoy);

if (mLogDumper == null)

mLogDumper = new LogDumper(String.valueOf(mPId), PATH_LOGCAT);

mLogDumper.start();

}

public void stop() {

if (mLogDumper != null) {

mLogDumper.stopLogs();

mLogDumper = null;

}

}

private class LogDumper extends Thread {

private Process logcatProc;

private BufferedReader mReader = null;

private boolean mRunning = true;

String cmds = null;

private String mPID;

private FileOutputStream out = null;

public LogDumper(String pid, String dir) {

mPID = pid;

try {

out = new FileOutputStream(new File(dir, "logcat-" + simpleDateFormat1.format(new Date()) + ".log"), true);

} catch (FileNotFoundException e) {

e.printStackTrace();

}

cmds = "logcat *:e *:i | grep \"(" + mPID + ")\"";

}

public void stopLogs() {

mRunning = false;

}

@Override

public void run() {

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;

}

if (out != null && line.contains(mPID)) {

out.write((simpleDateFormat2.format(new Date()) + " " + line + "\n").getBytes());

}

}

} catch (IOException e) {

e.printStackTrace();

} 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;

}

}

}

}

}

收集错误崩溃日志到文件夹中

这个一般是出现程序出现错误的时候才会自动保存到手机里的文件夹中,比如常见的NullPointerException,ArrayIndexOutOfBoundsException,SQLException ... and so on.

这个类的代码量有点多,不利于直观的看出来,在这只贴出重要的逻辑部分

还是推荐单例模式对这个类进行操作

/**

* 收集设备参数信息

*/

public void collectDeviceInfo(Context context) {

try {

PackageManager pm = context.getPackageManager();

PackageInfo pi = pm.getPackageInfo(context.getPackageName(), PackageManager.GET_ACTIVITIES);

if (pi != null) {

String versionName = pi.versionName == null ? "null" : pi.versionName;

String versionCode = pi.versionCode + "";

infos.put("versionName", versionName);

infos.put("versionCode", versionCode);

}

} catch (PackageManager.NameNotFoundException e) {

Logger.getLogger(TAG, "收集包信息时出错");

}

Field[] fields = Build.class.getDeclaredFields();

for (Field field : fields) {

try {

field.setAccessible(true);

infos.put(field.getName(), field.get(null).toString());

} catch (Exception e) {

Logger.getLogger(TAG, "收集包信息时出错");

}

}

}

-----------------------------------------保存错误信息到文件中---------------------------------------------------------------

/**

* @return 返回文件名称, 便于将文件传送到服务器

*/

private String saveCrashInfo2File(Throwable ex) {

StringBuffer sb = new StringBuffer();

for (Map.Entry entry : infos.entrySet()) {

String key = entry.getKey();

String value = entry.getValue();

sb.append(key).append("=").append(value).append("\n");

}

Writer writer = new StringWriter();

PrintWriter printWriter = new PrintWriter(writer);

ex.printStackTrace(printWriter);

Throwable cause = ex.getCause();

while (cause != null) {

cause.printStackTrace(printWriter);

cause = cause.getCause();

}

printWriter.close();

String result = writer.toString();

sb.append(result);

try {

long timestamp = System.currentTimeMillis();

String time = formatter.format(new Date());

String fileName = "crash-" + time + "-" + timestamp + ".log";

if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {

String path = "/sdcard/crash_police/";

File dir = new File(path);

if (!dir.exists()) {

dir.mkdirs();

}

FileOutputStream fos = new FileOutputStream(path + fileName);

sb.append("\r\n报错日期:");

sb.append(new Date(System.currentTimeMillis()).toLocaleString()).append("\r\n");

printStackTrace(sb, ex);

fos.write(sb.toString().getBytes());

fos.close();

}

return fileName;

} catch (Exception e) {

Logger.getLogger(TAG, "写入文件时出错...");

}

return null;

}

使用方法:

建议在自己的Application中进行开启log和Crash的收集

2.在onCreate里开始收集log startLogcat(),和CrashHandler.的初始化就可以了

3.onTerminate() 在程序终止的时候讲logcat的收集关闭,直接调用stopLogcat();就O 了

4.下面是开始和终止的方法

private void startLogcat() {

String folderPath = null;

if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {

folderPath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "Police-Logcat";

} else {

folderPath = this.getFilesDir().getAbsolutePath() + File.separator + "Police-Logcat";

}

LogcatManager.getInstance().start(folderPath);

}

private void stopLogcat() {

LogcatManager.getInstance().stop();

}

大功告成,经测试是木有任何问题的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值