有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

安卓应用程序的java文件记录器

我希望最简单的记录器就是简单地将错误(主要是异常)记录到安卓文件系统中的日志文件中。例如,我过去在PC java上登录文件的最简单、最方便(至少在我看来)的方式是,简单地将所有异常打印到控制台,并将系统重定向到控制台和我的文件,就我所知,这在安卓上并不足够,我猜这是因为安卓操作系统的设计,那么在安卓系统中最简单的方法是什么呢

请注意,这个项目中已经有很多代码,我真的不想再看一遍,在catch块上添加日志调用或其他任何东西来记录我的异常,因为我只需要做一点点来记录这些异常,这对我的用例来说是最好的

谢谢你


共 (1) 个答案

  1. # 1 楼答案

    它还没有完成,但运行非常稳定。它保存了人类可读的json数组,包括异常名称、时间、堆栈跟踪和其他数据。还可以保存logcat的日志

    使用

    ExceptionWriter ew = new ExceptionWriter(new File(Environment.getExternalStorageDirectory(), "debug.txt"));
    ew.w(new IllegalArgumentException("some msg"), "additional message");
    

    来源

    import java.io.*;
    import java.text.SimpleDateFormat;
    import java.util.concurrent.ConcurrentLinkedQueue;
    
    /**
     * User: elevenetc
     * Date: 10/9/13
     * Time: 12:52 PM
     */
    public class ExceptionWriter {
    
        private final StringBuilder sb;
        private final ExceptionWriter.WriteExceptionTask writeExceptionTask;
        private final SimpleDateFormat dataFormat;
        private int totalExceptions;
        private StringBuilder stackBuilder = new StringBuilder();
    
        public int getTotalExceptions(){return totalExceptions;}
    
        public ExceptionWriter(File file) throws IOException {
            if(file != null){
                writeExceptionTask = new WriteExceptionTask(file);
                sb = new StringBuilder();
                dataFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
                new Thread(writeExceptionTask).start();
            }else{
                sb = null;
                writeExceptionTask = null;
                dataFormat = null;
            }
    
        }
    
        public synchronized int wLogcat(){
            try {
                writeExceptionTask.addStreamToRead(Runtime.getRuntime().exec("logcat -d -v time").getInputStream());
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            return 0;
        }
    
        public int w(Exception debugException, String caughtMessage){
            return w(debugException, caughtMessage, null);
        }
    
        public synchronized int w(Exception debugException, String caughtMessage, String additionalData){
    
            if(writeExceptionTask == null) return -1;
    
            sb.setLength(0);
    
            StackTraceElement[] stackTrace = debugException == null ? null : debugException.getStackTrace();
    
            sb.append("{\"date\":\"");sb.append(getTime());
            sb.append("\",\"exceptionClassName\":\"");sb.append(debugException == null ? null : debugException.getClass());
            sb.append("\",\"exceptionMessage:\":\"");sb.append(debugException == null ? null : debugException.getMessage());
            sb.append("\",\"caughtMessage:\":\"");sb.append(caughtMessage);
            if(additionalData != null) {sb.append("\",\"data:\":\"");sb.append(additionalData);}
            sb.append("\",\"stack\":");sb.append(stackToString(stackTrace));
            sb.append("},");
    
            writeExceptionTask.stringQueue.add(sb.toString());
    
            totalExceptions++;
    
            return 0;
        }
    
        public void destroy() {
            if(writeExceptionTask != null) {
                writeExceptionTask.stop();
            }
        }
    
        private String getTime(){
            return dataFormat.format(System.currentTimeMillis());
        }
    
        private String stackToString(StackTraceElement[] stackTrace){
    
            if(stackTrace == null) return null;
    
            stackBuilder.setLength(0);
    
            stackBuilder.append("[");
            for (int i = 0; i < stackTrace.length; i++) {
                StackTraceElement e = stackTrace[i];
    
                stackBuilder.append("{\"");
                stackBuilder.append(e.getLineNumber());
                stackBuilder.append("\":\"");
                stackBuilder.append(e.getClassName());
                stackBuilder.append(".");
                stackBuilder.append(e.getMethodName());
                stackBuilder.append("\"}");
    
                if(i != stackTrace.length -1) stackBuilder.append(",");
            }
            stackBuilder.append("]");
            return stackBuilder.toString();
        }
    
        ///////////////////////////////////////////////
        /// Static classes
        ///////////////////////////////////////////////
    
        private class WriteExceptionTask implements Runnable {
    
            private final File file;
            private boolean running;
            private final ConcurrentLinkedQueue<String> stringQueue;
            private final ConcurrentLinkedQueue<InputStream> isQueue;
            private final FileWriter writer;
    
            private WriteExceptionTask(File file) throws IOException {
                this.file = file;
                writer = new FileWriter(this.file, true);
                stringQueue = new ConcurrentLinkedQueue<String>();
                isQueue = new ConcurrentLinkedQueue<InputStream>();
                running = true;
            }
    
            public void addStreamToRead(InputStream is){
                if(is != null){
                    isQueue.add(is);
                }
            }
    
            @Override
            public void run() {
                while(running){
                    if(!stringQueue.isEmpty()){
    
                        //TODO check file existence
    
                        try {
                            writer.append(stringQueue.poll());
                            writer.flush();
                        } catch (IOException e) {
                            e.printStackTrace();
                            running = false;
                        }
                    }
    
                    if(!isQueue.isEmpty()){
                        InputStream is = isQueue.poll();
                        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    
                        StringBuilder builder = new StringBuilder("{\"catLog\":\"");
    
                        String aux;
    
                        try {
    
                            while ((aux = reader.readLine()) != null) {
                                //TODO view like array or \n
                                builder.append(aux);
                            }
    
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
    
                        builder.append("\"},");
    
                        stringQueue.add(builder.toString());
                    }
                }
    
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
    
            public void stop() {
                running = false;
            }
        }
    
    }