Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在android设备上获取csv/txt文件中的特定日志_Android_Logging_Export To Csv_Java.util.logging - Fatal编程技术网

如何在android设备上获取csv/txt文件中的特定日志

如何在android设备上获取csv/txt文件中的特定日志,android,logging,export-to-csv,java.util.logging,Android,Logging,Export To Csv,Java.util.logging,我想将日志以csv/txt文件的形式发送到我的设备。问题是我只需要一个活动的特定日志。下面是我想要获取日志的代码 final Beacon maxBeacon = Collections.max(beacons, new Comparator<Beacon>() { public int compare(Beacon b1, Beacon b2) { return Integer.compare(b1.getRssi(),

我想将日志以csv/txt文件的形式发送到我的设备。问题是我只需要一个活动的特定日志。下面是我想要获取日志的代码

  final Beacon maxBeacon = Collections.max(beacons, new Comparator<Beacon>() {
            public int compare(Beacon b1, Beacon b2) {
                return Integer.compare(b1.getRssi(), b2.getRssi());

            }


        });
        WifiManager manager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
        WifiInfo info = manager.getConnectionInfo();
        String address = info.getMacAddress();
        String Bmacstring = String.valueOf(maxBeacon.getMacAddress());
        String newString = Bmacstring.replace("[", "");
        newString = newString.replace("[", "");
        newString = newString.replace("]", "");
        String query = "?deviceMacAddress=" + info.getMacAddress() + "&beaconMacAddress=" + newString;
        Log.i("Rss", String.valueOf(maxBeacon));
        new HttpAsyncTask().execute("http://192.168.137.121:1010/api/History/CreatHistory" + query);
final Beacon maxBeacon=Collections.max(beacons,新比较器(){
公共整数比较(信标b1、信标b2){
返回整数.compare(b1.getRssi(),b2.getRssi());
}
});
WifiManager=(WifiManager)context.getSystemService(context.WIFI_服务);
WifiInfo=manager.getConnectionInfo();
字符串地址=info.getMacAddress();
String Bmacstring=String.valueOf(maxBeacon.getMacAddress());
String newString=Bmacstring.replace(“[”,”);
newString=newString.replace(“[”,”);
newString=newString.replace(“]”,“”);
字符串查询=“?deviceMacAddress=“+info.getMacAddress()+”&beaconMacAddress=“+newString;
Log.i(“Rss”,String.valueOf(maxBeacon));
新建HttpAsyncTask()。执行(“http://192.168.137.121:1010/api/History/CreatHistory“+查询);
我想要上面android设备代码中Log.I(“Rss”,String.valueOf(maxBeacon));的日志


任何帮助都将不胜感激。在我创建日志帮助器类之前,有时会感谢您将日志存储在文本文件中

import android.os.Environment;
import android.util.Log;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

/**
 * TODO: Add a class header comment!
 *
 * @author Dhaval Patel
 * @version 1.0, May 24, 2015
 * @since 1.0
 */
public final class Logger {

    private static final String LOG_PREFIX = "appname_";
    private static final int LOG_PREFIX_LENGTH = LOG_PREFIX.length();
    private static final int MAX_LOG_TAG_LENGTH = 23;

    private static final Boolean ENABLE_CONSOLE_LOG = true;  //Flag to enable or disable console log
    private static final Boolean ENABLE_FILE_LOG = true;  //Flag to enable or disable file log
    private static final LogLevel GLOBAL_LOG_LEVEL = LogLevel.VERBOSE; //Flag indicate log level
    private static final String LOG_DIRECTORY = Environment.getExternalStorageDirectory().getAbsolutePath()+ File.separator+"log"+ File.separator;

    public static String makeLogTag(String str) {
        if (str.length() > MAX_LOG_TAG_LENGTH - LOG_PREFIX_LENGTH) {
            return LOG_PREFIX + str.substring(0, MAX_LOG_TAG_LENGTH - LOG_PREFIX_LENGTH - 1);
        }
        return LOG_PREFIX + str;
    }

    private enum LogLevel{
        VERBOSE(Log.VERBOSE),
        DEBUG(Log.DEBUG),
        INFO(Log.INFO),
        WARNING(Log.WARN),
        ERROR(Log.ERROR),
        ASSERT(Log.ASSERT);

        private final int logLevel;
        LogLevel(int logLevel) {
            this.logLevel = logLevel;
        }

        public int getLogLevel() {
            return logLevel;
        }
    }


    /**
     *
     * @param tag Used to identify the source of a log message.  It usually identifies
     *            the class or activity where the log call occurs.
     * @param msg The message you would like logged.
     *
     */
    public static void v(String tag, String msg) {
        write(LogLevel.VERBOSE, tag, msg);
    }

    /**
     *
     * @param tag Used to identify the source of a log message.  It usually identifies
     *            the class or activity where the log call occurs.
     * @param msg The message you would like logged.
     * @param tr  An exception to log
     *
     */
    public static void v(String tag, String msg, Throwable tr) {
        write(LogLevel.VERBOSE, tag, msg, tr);
    }

    /**
     *
     * @param tag Used to identify the source of a log message.  It usually identifies
     *            the class or activity where the log call occurs.
     * @param msg The message you would like logged.
     *
     */
    public static void d(String tag, String msg) {
        write(LogLevel.DEBUG, tag, msg);
    }

    /**
     *
     * @param tag Used to identify the source of a log message.  It usually identifies
     *            the class or activity where the log call occurs.
     * @param msg The message you would like logged.
     * @param tr  An exception to log
     *
     */
    public static void d(String tag, String msg, Throwable tr) {
        write(LogLevel.DEBUG, tag, msg, tr);
    }

    /**
     *
     * @param tag Used to identify the source of a log message.  It usually identifies
     *            the class or activity where the log call occurs.
     * @param msg The message you would like logged.
     *
     */
    public static void i(String tag, String msg) {
        write(LogLevel.INFO, tag, msg);
    }

    /**
     *
     * @param tag Used to identify the source of a log message.  It usually identifies
     *            the class or activity where the log call occurs.
     * @param msg The message you would like logged.
     * @param tr  An exception to log
     *
     */
    public static void i(String tag, String msg, Throwable tr) {
        write(LogLevel.INFO, tag, msg, tr);
    }

    /**
     *
     * @param tag Used to identify the source of a log message.  It usually identifies
     *            the class or activity where the log call occurs.
     * @param msg The message you would like logged.
     *
     */
    public static void w(String tag, String msg) {
        write(LogLevel.WARNING, tag, msg);
    }

    /**
     *
     * @param tag Used to identify the source of a log message.  It usually identifies
     *            the class or activity where the log call occurs.
     * @param msg The message you would like logged.
     * @param tr  An exception to log
     *
     */
    public static void w(String tag, String msg, Throwable tr) {
        write(LogLevel.WARNING, tag, msg, tr);
    }

    /**
     *
     * @param tag Used to identify the source of a log message.  It usually identifies
     *            the class or activity where the log call occurs.
     * @param msg The message you would like logged.
     *
     */
    public static void e(String tag, String msg) {
        write(LogLevel.ERROR, tag, msg);
    }

    /**
     *
     * @param tag Used to identify the source of a log message.  It usually identifies
     *            the class or activity where the log call occurs.
     * @param msg The message you would like logged.
     * @param tr  An exception to log
     *
     */
    public static void e(String tag, String msg, Throwable tr) {
        write(LogLevel.ERROR, tag, msg, tr);
    }

    private static boolean isLogEnable(LogLevel logLevel){
        return GLOBAL_LOG_LEVEL.getLogLevel() <= logLevel.getLogLevel();
    }

    private static void write(LogLevel logLevel, String tag, String log) {
        if (isLogEnable(logLevel) && ENABLE_FILE_LOG){
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[4];
            String logPoint = stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName() + ":" + stackTraceElement.getLineNumber();
            String msg = "["+getCurrentDateTime()+"] "+ logLevel.name() +" "+ logPoint +" "+tag+"//:"+log;
            write(msg);
        }

        if (isLogEnable(logLevel) && ENABLE_CONSOLE_LOG){
            Log.println(logLevel.getLogLevel(), makeLogTag(tag), log);
        }
    }

    private static void write(LogLevel logLevel, String tag, String log, Throwable tr){
        if (isLogEnable(logLevel) && ENABLE_FILE_LOG){
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[4];
            String logPoint = stackTraceElement.getClassName() + "::" + stackTraceElement.getMethodName() + ":" + stackTraceElement.getLineNumber();
            String msg = "["+getCurrentDateTime()+"] "+ logLevel.name() +" "+ logPoint+" "+tag+"//:"+log+"\n"+Log.getStackTraceString(tr);
            write(msg);
        }

        if (isLogEnable(logLevel) && ENABLE_CONSOLE_LOG){
            Log.println(logLevel.getLogLevel(), makeLogTag(tag), log + "\n" + Log.getStackTraceString(tr));
        }
    }

    private static void write(String text){
        BufferedWriter out = null;
        String filePath = LOG_DIRECTORY +"AppLog.txt";
        try {

            SimpleDateFormat df = new SimpleDateFormat("dd_MMM_yyyy", Locale.ENGLISH);
            String formattedDate = df.format(System.currentTimeMillis());

            if(!new File(LOG_DIRECTORY).exists()) {
                new File(LOG_DIRECTORY).mkdirs();
            }

            if(!new File(filePath).exists())
                new File(filePath).createNewFile();

            if(new File(filePath).exists()){
                FileWriter fStream = new FileWriter(filePath, true);
                out = new BufferedWriter(fStream);
                out.write(text);
                out.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if(out!=null)
                    out.close();

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static String getCurrentDateTime(){
        return new SimpleDateFormat("dd MMM yyyy HH:mm:ss:SSS" , Locale.getDefault()).format(Calendar.getInstance().getTime());
    }

}

你为什么不写一个代码来将文本存储到文件中。恐怕我不知道。你能告诉我怎么做吗!!@Harsh Mittal你可以根据你的需要修改写入方法。我已经按照上面提到的那样做了,但我没有得到任何文件!!只是像以前一样登录logcat。我哪里出错了?你在清单中指定了权限吗?你正在测试吗棉花糖设备?我正在测试棒棒糖设备。我的设备中没有存储卡,所以我可能无法在设备中获取文件。
Logger.i("Rss", String.valueOf(maxBeacon));