使用logcat让Android应用支持查看实时日志并输出至界面显示功能

Ona ·
更新时间:2024-11-14
· 540 次阅读

不使用USB线接Android设备连接电脑,也不用电脑上装Android studio和logcat工具,

如何查看应用的实时日志呢?方法还是有的。

先附图:看这功能是不是很赞?

机器强大了就是好,有好多创新可以派上用场了。后续继续探索新鲜的新功能。

运维的兄弟们可以松口气了,给你们减减压。

日志排查获取从此如此简单。甚至可以给手机互通,日志显示到你手机上也能。

这有什么用?方便现场运维人员快速的协助研发定位和找到问题。当然了,没问题也不用看日志了。看日志就是为了分析和定位问题的一种有效途径。

且日志还是实时输出的,这样从应用的后门调起查看日志的窗口,就很方便的查看实时的日志输出啦

这功能是不是很赞?且可以清空窗口,保存日志,发送日志给后台等功能。

/** Author:yangyongzhen qq:534117529 Date:20200221 */ package com.xxxx.xxx.activity; import android.annotation.SuppressLint; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.text.method.ScrollingMovementMethod; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.RandomAccessFile; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; public class LogActivity extends BaseActivity implements View.OnClickListener { String cmds = ""; StringBuilder Sb = new StringBuilder("this is log"); private BufferedReader mReader = null; private Process exec; private int mPId; private String mPID; private boolean mRunning = true; EditText etlog; Button btnStop,btnSave,btnClear; RandomAccessFile randomFile = null; private final String DIR = CommonStatus.PATH_DIR + "/log"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); toDispPageInfo("日志查看"); etlog = findViewById(R.id.et_logs); btnStop = findViewById(R.id.btn_stop); btnStop.setOnClickListener(this); btnSave = findViewById(R.id.btn_save); btnSave.setOnClickListener(this); btnClear = findViewById(R.id.btn_clear); btnClear.setOnClickListener(this); mPId = android.os.Process.myPid(); mPID = String.valueOf(mPId); cmds = "logcat *:e *:d | grep \"(" + mPID + ")\"";; } @Override protected void onResume() { super.onResume(); try { exec = Runtime.getRuntime().exec(cmds); mReader = new BufferedReader(new InputStreamReader(exec.getInputStream()), 1024); new Thread(new Runnable() { @Override public void run() { String line=""; try { while (mRunning && (line = mReader.readLine())!=null) { Sb.append(line); if (!mRunning) { break; } if (line.length()==0) { continue; } final Message msg = Message.obtain(); msg.what = 2; msg.obj = line+ "\n"; mhandler.sendMessage(msg); Thread.sleep(100); } }catch (Exception e){ Log.e(TAG, e.toString()); } finally { Log.e(TAG, "finally"); Log.e(TAG, "" + mRunning); if (line == null) { Log.e(TAG, "line is null"); } if (exec != null) { exec.destroy(); } if (mReader != null) { try { mReader.close(); mReader = null; } catch (IOException e) { } } } } }).start(); } catch (IOException e) { e.printStackTrace(); } //startShowLog(); } @Override protected void onDestroy() { super.onDestroy(); mRunning = false; if (exec != null) { exec.destroy(); } if (mReader != null) { try { mReader.close(); mReader = null; } catch (IOException e) { } } } @Override public void onClick(View v) { switch (v.getId()){ case R.id.btn_stop: mRunning = false; break; case R.id.btn_save: SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-ddHHmmss", Locale.CHINA); String date = format1.format(new Date(System.currentTimeMillis())); try { String path = DIR + "/" + date + ".log"; randomFile = new RandomAccessFile(path, "rw"); randomFile.write(etlog.getText().toString().getBytes()); Toast t = Toast.makeText(mContext, "日志保存成功,path="+path, Toast.LENGTH_SHORT); t.show(); } catch (IOException e) { //e.printStackTrace(); LogUtil.d(e.toString()); } break; case R.id.btn_clear: etlog.setText(""); break; } } @Override public int initLayout() { return R.layout.activity_log; } @Override public void initView() { toBack(); } @Override public void initData() { } @SuppressLint("HandlerLeak") Handler mhandler = new Handler() { public void handleMessage(Message msg) { switch (msg.what) { case 1: break; case 2: //显示日志 etlog.setMovementMethod(ScrollingMovementMethod.getInstance()); etlog.setSelection(etlog.getText().length(), etlog.getText().length()); etlog.setText(etlog.getText().append(msg.obj.toString())); break; } } }; }
作者:特立独行小猫咪



输出 界面 logcat Android

需要 登录 后方可回复, 如果你还没有账号请 注册新账号