[英]Android writing log to the file
By default, Log class in android writes logs to console (logcat). 默认情况下,Android中的Log类将日志写入控制台(logcat)。 Is there any simple way to write logs fe in some file?
有什么简单的方法可以在文件中写入日志fe?
A simple Log
class (development/testing purpose only) similar to android.util.Log
, but logs to sdcard. 与
android.util.Log
类似的简单Log
类(仅用于开发/测试目的),但会记录到sdcard。 Only modification to the existing code will be to change the import android.util.Log
to import com.example.Log
. 对现有代码的唯一修改将是将
import android.util.Log
更改为import com.example.Log
。 Also add permission as 还将权限添加为
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
package com.example.logger;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.os.Environment;
public class Log {
private static final String NEW_LINE = System.getProperty("line.separator") ;
public static boolean mLogcatAppender = true;
final static File mLogFile;
static {
mLogFile = new File( Environment.getExternalStorageDirectory(), "logs.log" );
if ( !mLogFile.exists() ) {
try {
mLogFile.createNewFile();
}
catch (final IOException e) {
e.printStackTrace();
}
}
logDeviceInfo();
}
public static void i( String TAG, String message ){
appendLog( TAG + " : " + message );
if ( mLogcatAppender ) {
android.util.Log.i( TAG, message );
}
}
public static void d( String TAG, String message ){
appendLog( TAG + " : " + message );
if ( mLogcatAppender ) {
android.util.Log.d( TAG, message );
}
}
public static void e( String TAG, String message ){
appendLog( TAG + " : " + message );
if ( mLogcatAppender ) {
android.util.Log.e( TAG, message );
}
}
public static void v(String TAG, String message ){
appendLog(TAG + " : " + message);
if ( mLogcatAppender ) {
android.util.Log.v( TAG, message );
}
}
public static void w( String TAG, String message ) {
appendLog( TAG + " : " + message );
if ( mLogcatAppender ) {
android.util.Log.w( TAG, message );
}
}
private static synchronized void appendLog( String text ) {
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
try {
final FileWriter fileOut = new FileWriter( mLogFile, true );
fileOut.append( sdf.format(new Date()) + " : " + text + NEW_LINE );
fileOut.close();
}
catch ( final IOException e ) {
e.printStackTrace();
}
}
private static void logDeviceInfo() {
appendLog("Model : " + android.os.Build.MODEL);
appendLog("Brand : " + android.os.Build.BRAND);
appendLog("Product : " + android.os.Build.PRODUCT);
appendLog("Device : " + android.os.Build.DEVICE);
appendLog("Codename : " + android.os.Build.VERSION.CODENAME);
appendLog("Release : " + android.os.Build.VERSION.RELEASE);
}
}
If this is just for dev purposes, you can invoke logcat on the device and redirect its output into a file. 如果这仅是出于开发目的,则可以在设备上调用logcat并将其输出重定向到文件中。 For example:
adb shell logcat > log.txt
. 例如:
adb shell logcat > log.txt
。
Alternatively, you could try redirecting stdout
and stderr
to a file, iirc this will work but I don't have my phone with me to test it. 另外,您可以尝试将
stdout
和stderr
重定向到文件,iirc可以正常工作,但是我没有手机可以测试它。
However, it would be easier to just make your own basic logging class that does the same thing as the built in one but also saves to a file. 但是,仅创建自己的基本日志记录类将变得更容易,该类既可以执行与内置日志记录类相同的操作,又可以保存到文件中。
在Eclipse ddms中,有一个选项可以将日志保存到文件中。
使用FileInputStream将所有错误消息重定向到文件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.