Android Open Source - alogger A Logger






From Project

Back to project page alogger.

License

The source code is released under:

GNU General Public License

If you think the Android project alogger listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.jug6ernaut.android.logging;
/*from ww  w.  j a v  a  2  s .  com*/
import android.app.Application;
import android.content.Context;
import android.util.Log;

import java.io.*;
import java.lang.Thread.UncaughtExceptionHandler;
import java.util.ArrayList;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.LogRecord;

public class ALogger implements Serializable {

    private static final long serialVersionUID = -5853120689910966036L;
    private static java.util.logging.Logger logger = null;
    private static FileHandler handler = null;
    private static String logFilePath = "";
    private static String mTag = "";
    static ALogger alogger = null;

    public static Boolean BuildConfigDebug;

    private ALogger(){}

    public enum LogLevel {
        TRACE,
        DEBUG,
        INFO,
        WARN,
        ERROR,
        FATAL
    }

    public static void init(Application application, String tag, boolean toLogCat) {
        mTag = tag;

        if (alogger == null) {
            alogger = new ALogger(application, tag);
        }

    }

    protected static synchronized ALogger getRootLogger() {

        if (alogger == null)
            throw new RuntimeException("RootLogger not initialized! Please run ALogger.init() in your root Application.");

        return alogger;
    }

    private ALogger(Context context, String tag) {
        logger = java.util.logging.Logger.getLogger(tag);

        logFilePath = context.getFilesDir().getPath().toString() + "/" + tag + ".log";

        handler = getHandler(logFilePath);

        // Add to the desired logger
        logger.setUseParentHandlers(false);
        logger.setLevel(Level.ALL);
        logger.addHandler(handler);

        try {
            BuildConfigDebug = Class.forName(context.getPackageName()+".BuildConfig").getDeclaredField("DEBUG").getBoolean(null);
        } catch (Exception e) {
            e.printStackTrace();
            BuildConfigDebug = false;
        }
    }

    private static FileHandler getHandler(String filePath) {

        FileHandler handle = null;

        int sizeLimit = 100000;// 10kB
        int fileLimit = 1;

        try {
            handle = new FileHandler(filePath, sizeLimit, fileLimit, true) {
                @Override
                public void publish(LogRecord record) {
                    super.publish(record);

                    logToListeners(LogEntry.fromLogRecord(record));
                }
            };

            handle.setFormatter(new JSONFormatter());
        } catch (IOException e) {
            Log.e(mTag, "Error in getHandler", e);
        }

        return handle;
    }

    public void setHandleUncaughtException(boolean enable) {
        if (enable) {
            dExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
            Thread.setDefaultUncaughtExceptionHandler(mExceptionHandler);
        } else {
            if (dExceptionHandler != null) {
                Thread.setDefaultUncaughtExceptionHandler(dExceptionHandler);
            }
        }
    }

    UncaughtExceptionHandler dExceptionHandler = null;
    UncaughtExceptionHandler mExceptionHandler = new UncaughtExceptionHandler() {

        @Override
        public void uncaughtException(Thread thread, Throwable ex) {
            alogger.log("UncaughtException", ex, LogLevel.FATAL);
            dExceptionHandler.uncaughtException(thread, ex);
        }

    };

    public File getLogFile() {
        return new File(logFilePath);
    }

    public FileOutputStream getLogFileStream(Context context) {
        FileOutputStream fos = null;
        try {
            fos = context.openFileOutput(logFilePath, Context.MODE_WORLD_READABLE);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return fos;
    }

    public StringBuilder getLogAsString() {
        StringBuilder sb = new StringBuilder();

        try {
            FileInputStream fstream = new FileInputStream(logFilePath);
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));

            String strLine;

            while ((strLine = br.readLine()) != null) {
                sb.append(strLine);
                sb.append("\n");
            }

            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return sb;

    }

    public boolean clearLogFile() {
        File logFile = new File(logFilePath);
        boolean success = logFile.delete();

        if (success) {
            logger.removeHandler(handler);
            logger = java.util.logging.Logger.getLogger(mTag);
            handler = getHandler(logFilePath);
            logger.setUseParentHandlers(false);
            logger.addHandler(handler);
        }

        return success;
    }

    public interface OnLogListener {
        public void onLog(LogEntry log);
    }

    private static ArrayList<OnLogListener> listeners = new ArrayList<OnLogListener>();

    private static void logToListeners(LogEntry log) {
        for (OnLogListener listener : listeners) {
            if (listener != null) {
                listener.onLog(log);
            } else {
                synchronized (listeners) {
                    listeners.remove(listener);
                }
            }
        }
    }

    public void setOnLogListener(OnLogListener listener) {
        if (!listeners.contains(listener)) {
            listeners.add(listener);
        }
    }

    public void removeOnLogListener(OnLogListener listener) {
        if (listeners.contains(listener)) {
            listeners.remove(listener);
        }
    }

    private String buildLog(String msg, boolean showTrace) {
        return ((showTrace) ? (getMethod() + ":" + msg) : msg);
    }

    private String getMethod() {

        StackTraceElement[] elements = new Exception().getStackTrace();
        String methodName = "";

        for (StackTraceElement element : elements) {
            if (element.getClassName().equals(ALogger.class.getCanonicalName())) {
                continue;
            } else {

                methodName = element.getClassName().substring(
                        element.getClassName().lastIndexOf('.') + 1,
                        element.getClassName().length())
                        + ":" + element.getMethodName();
                break;
            }
        }
        return methodName;
    }

    private String getClassString() {

        StackTraceElement[] elements = new Exception().getStackTrace();

        String class1 = "";
        String class2 = "";

        for (StackTraceElement element : elements) {
            if (element.getClassName().equals(ALogger.class)) {
                continue;
            } else {
                if (class2.equals("")) {
                    class2 = element.getMethodName();
                    continue;
                } else if (class1.equals("")) {
                    class1 = element.getClassName().substring(
                            element.getClassName().lastIndexOf('.') + 1,
                            element.getClassName().length());
                    class1 += "." + element.getMethodName();

                } else
                    break;
            }
        }

        return class1 + "." + class2;
    }


    public void log(Object message, LogLevel logLevel) {
        String msg = String.valueOf(message);
        switch (logLevel) {
            case TRACE:
                Log.v(mTag, msg);
                logger.log(Level.FINER, msg);
                break;
            case DEBUG:
                Log.d(mTag, msg);
                logger.log(Level.FINE, msg);
                break;
            case INFO:
                Log.i(mTag, msg);
                logger.log(Level.INFO, msg);
                break;
            case WARN:
                Log.w(mTag, msg);
                logger.log(Level.WARNING, msg);
                break;
            case ERROR:
                Log.e(mTag, msg);
                logger.log(Level.SEVERE, msg);
                break;
            case FATAL:
                Log.wtf(mTag, msg);
                logger.log(Level.SEVERE, msg);
                break;
        }
    }

    public void log(Object message, Throwable t, LogLevel logLevel) {
        String msg = String.valueOf(message);
        switch (logLevel) {
            case TRACE:
                Log.v(mTag, msg, t);
                logger.log(Level.FINER, msg);
                logger.log(Level.SEVERE, getStackTrace(t));
                break;
            case DEBUG:
                Log.d(mTag, msg, t);
                logger.log(Level.FINE, msg);
                logger.log(Level.FINE, getStackTrace(t));
                break;
            case INFO:
                Log.i(mTag, msg, t);
                logger.log(Level.INFO, msg);
                logger.log(Level.INFO, getStackTrace(t));
                break;
            case WARN:
                Log.w(mTag, msg, t);
                logger.log(Level.WARNING, msg);
                logger.log(Level.WARNING, getStackTrace(t));
                break;
            case ERROR:
                Log.e(mTag, msg, t);
                logger.log(Level.SEVERE, msg);
                logger.log(Level.SEVERE, getStackTrace(t));
                break;
            case FATAL:
                Log.wtf(mTag, msg, t);
                logger.log(Level.SEVERE, msg);
                logger.log(Level.SEVERE, getStackTrace(t));
                break;
        }
    }

    private static String getStackTrace(Throwable throwable) {
        StringWriter sw = new StringWriter();
        throwable.printStackTrace(new PrintWriter(sw));
        return sw.toString();
    }

}




Java Source Code List

.FileDialog.java
com.jug6ernaut.android.actiondialog.ActionDialog.java
com.jug6ernaut.android.logging.ALogger.java
com.jug6ernaut.android.logging.BriefLogFormatter.java
com.jug6ernaut.android.logging.DebugLogger.java
com.jug6ernaut.android.logging.JSONFormatter.java
com.jug6ernaut.android.logging.JSONLogReader.java
com.jug6ernaut.android.logging.JSONLogWriter.java
com.jug6ernaut.android.logging.LogAdapter.java
com.jug6ernaut.android.logging.LogEntry.java
com.jug6ernaut.android.logging.LogFragment.java
com.jug6ernaut.android.logging.Logger.java
com.jug6ernaut.android.logging.ProdLogger.java
com.jug6ernaut.android.utilites.AnalysisDir.java
com.jug6ernaut.android.utilites.Assets.java
com.jug6ernaut.android.utilites.BaseAsyncTask.java
com.jug6ernaut.android.utilites.ByteUtils.java
com.jug6ernaut.android.utilites.CopyUtils.java
com.jug6ernaut.android.utilites.DeviceWake.java
com.jug6ernaut.android.utilites.DisplayUtils.java
com.jug6ernaut.android.utilites.Eula.java
com.jug6ernaut.android.utilites.FileDownloader.java
com.jug6ernaut.android.utilites.ImagesUtils.java
com.jug6ernaut.android.utilites.PInfo.java
com.jug6ernaut.android.utilites.PackageInfo.java
com.jug6ernaut.android.utilites.ReflectionUtils.java
com.jug6ernaut.android.utilites.SecurePreferences.java
com.jug6ernaut.android.utilites.SeekBarPreference.java
com.jug6ernaut.android.utilites.SimpleCrypto.java
com.jug6ernaut.android.utilites.SuperUser.java
com.jug6ernaut.android.utilites.UserUtils.java
com.jug6ernaut.android.utilites.WakelockHelper.java
com.jug6ernaut.android.utilites.lazyloader.FileCache.java
com.jug6ernaut.android.utilites.lazyloader.ImageLoader.java
com.jug6ernaut.android.utilites.lazyloader.MemoryCache.java
com.jug6ernaut.android.utilites.lazyloader.Utils.java
com.jug6ernaut.android.utilites.time.Preconditions.java
com.jug6ernaut.android.utilites.time.Stopwatch.java
com.jug6ernaut.android.utilites.time.Ticker.java
com.jug6ernaut.android.utilites.time.TimeUtils.java
com.jug6ernaut.billing.Base64DecoderException.java
com.jug6ernaut.billing.Base64.java
com.jug6ernaut.billing.IabException.java
com.jug6ernaut.billing.IabHelper.java
com.jug6ernaut.billing.IabResult.java
com.jug6ernaut.billing.Inventory.java
com.jug6ernaut.billing.Purchase.java
com.jug6ernaut.billing.Security.java
com.jug6ernaut.billing.SkuDetails.java