我的应用程序中有一些日志语句,如果应用程序在模拟器设备上运行,则会在Android Studio的LogCat中显示。 E.g:
public VocabularyTrainerModelImpl() {
...
Log.d(TAG, "First line index is -1");
...
}
如果理解正确建议运行的那个:
$ adb logcat -d > log.txt
从真实设备获取日志文件。 How do I get the logfile from an Android device?
我通过USB将手机连接到电脑并运行此命令。但我看不到我的日志消息。我怎么得到它们?如果可能的话,不需要任何额外的安装。
应用中存在一个很少发生的错误。所以在这种情况下,我想将手机连接到PC,提取日志文件并进行分析。
答案 0 :(得分:0)
在我的项目中,我使用它来解决问题。
1.加上这个班级
public class LogcatHelper {
private static LogcatHelper INSTANCE = null;
private static String PATH_LOGCAT;
private LogDumper mLogDumper = null;
private int mPId;
/**
* init data
*/
public void init(Context context) {
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {// sd first
PATH_LOGCAT = Environment.getExternalStorageDirectory()
.getAbsolutePath() + File.separator + "logcat";
} else {
PATH_LOGCAT = context.getFilesDir().getAbsolutePath()
+ File.separator + "logcat";
}
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) {
init(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;
}
}
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"
+ getFileName() + ".log"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
/**
*
* Level:*:v , *:d , *:w , *:e , *:f , *:s
*
*
* */
// cmds = "logcat *:e *:w | grep \"(" + mPID + ")\""; // print e level and ilevel info
// cmds = "logcat | grep \"(" + mPID + ")\"";// print all
// cmds = "logcat -s way";// print filter info
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((getDateEN() + " " + 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;
}
}
}
}
public static String getFileName() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String date = format.format(new Date(System.currentTimeMillis()));
return date;
}
public static String getDateEN() {
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date1 = format1.format(new Date(System.currentTimeMillis()));
return date1;
}
}
在Application类中添加代码
LogcatHelper.getInstance((getApplicationContext())).start();
3.在Application类中添加权限
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if(checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
Process process = Runtime.getRuntime().exec("logcat -f "+logFile);
}
}
我希望我能帮助你。
答案 1 :(得分:0)
我在这里找到了一个解决方案:https://stackoverflow.com/a/22802081/971355
日志文件不会自动创建。我必须通过从应用程序运行logcat来自己编写。
public static void printLog(Context context){
String filename = context.getExternalFilesDir(null).getPath() + File.separator + "my_app.log";
String command = "logcat -f "+ filename + " -v time *:V";
Log.d(TAG, "command: " + command);
try{
Runtime.getRuntime().exec(command);
}
catch(IOException e){
e.printStackTrace();
}
}