Android Open Source - baracus-framework Logger






From Project

Back to project page baracus-framework.

License

The source code is released under:

Apache License

If you think the Android project baracus-framework 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 net.mantucon.baracus.util;
//  w ww.  j  a  v a2s . com
import android.util.Log;

/**
 * Created by IntelliJ IDEA.
 * User: mnt
 * Date: 07.02.12
 * Time: 19:21
 * very basic logger providin a log4j style combined with a slf4j vararg styled logging function
 */
public class Logger {

    private static String TAG = "PLEASE SET TAG";

    private final String loggerId;

    enum Level {
        ERROR,
        WARN,
        INFO,
        DEBUG,
        TRACE,
    }

    private static Level level = Level.DEBUG;

    private static LogTarget logTarget = new AndroidLogger();

    public static void enableTestLogger() {
        logTarget = new SysOutLogger();
    }

    public static interface LogTarget {
        public void debug(String tag, String msg, Throwable t);

        public void debug(String tag, String msg);

        public void info(String tag, String msg, Throwable t);

        public void info(String tag, String msg);

        public void warn(String tag, String msg, Throwable t);

        public void warn(String tag, String msg);

        public void error(String tag, String msg, Throwable t);

        public void error(String tag, String msg);

        public void trace(String tag, String msg, Throwable t);

        public void trace(String tag, String msg);
    }

    public static class AndroidLogger implements LogTarget {
        public void trace(String tag, String msg) {
            Log.v(tag, msg);
        }

        public void trace(String tag, String msg, Throwable tr) {
            Log.v(tag, msg, tr);
        }

        public void warn(String tag, String msg, Throwable tr) {
            Log.w(tag, msg, tr);
        }

        public void warn(String tag, Throwable tr) {
            Log.w(tag, tr);
        }

        public void debug(String tag, String msg, Throwable tr) {
            Log.d(tag, msg, tr);
        }

        public void info(String tag, String msg, Throwable tr) {
            Log.i(tag, msg, tr);
        }

        public void info(String tag, String msg) {
            Log.i(tag, msg);
        }

        public void warn(String tag, String msg) {
            Log.w(tag, msg);
        }

        public void error(String tag, String msg) {
            Log.e(tag, msg);
        }

        public void error(String tag, String msg, Throwable tr) {
            Log.e(tag, msg, tr);
        }

        public void debug(String tag, String msg) {
            Log.d(tag, msg);
        }
    }

    public static class SysOutLogger implements LogTarget {

        @SuppressWarnings("PMD")
        public void dump(String tag, String msg, Throwable t) {
            System.out.println(tag + " " + msg);
            if (t != null) {
                t.printStackTrace();
            }
        }

        @Override
        public void debug(String tag, String msg, Throwable t) {
            dump(tag, msg, t);
        }

        @Override
        public void debug(String tag, String msg) {
            dump(tag, msg, null);
        }

        @Override
        public void info(String tag, String msg, Throwable t) {
            dump(tag, msg, t);
        }

        @Override
        public void info(String tag, String msg) {
            dump(tag, msg, null);
        }

        @Override
        public void warn(String tag, String msg, Throwable t) {
            dump(tag, msg, t);
        }

        @Override
        public void warn(String tag, String msg) {
            dump(tag, msg, null);
        }

        @Override
        public void error(String tag, String msg, Throwable t) {
            dump(tag, msg, t);
        }

        @Override
        public void error(String tag, String msg) {
            dump(tag, msg, null);
        }

        @Override
        public void trace(String tag, String msg, Throwable t) {
            dump(tag, msg, t);
        }

        @Override
        public void trace(String tag, String msg) {
            dump(tag, msg, null);
        }
    }


    private final String processMessageArgs(final String message, final Object... args) {
        String result = message;
        for (int i = 0; i < args.length; ++i) {
            result = result.replace("$" + (i + 1), String.valueOf(args[i]));
        }
        return result;
    }

    public Logger(String loggerId) {
        this.loggerId = loggerId;
        log(Level.DEBUG, loggerId + " was registered");
    }

    public Logger(Class<?> classToLog) {
        this(classToLog.getSimpleName());
    }

    public void debug(final String message, final Object... args) {
        if (!isLoggable(Level.DEBUG)) {
            return;
        }
        log(Level.DEBUG, loggerId + " " + processMessageArgs(message, args));
    }

    public void error(final String message, final Object... args) {
        if (!isLoggable(Level.ERROR)) {
            return;
        }
        log(Level.ERROR, loggerId + " " + processMessageArgs(message, args));
    }

    public void fatal(final String message, final Object... args) {
        if (!isLoggable(Level.ERROR)) {
            return;
        }
        log(Level.ERROR, loggerId + " " + processMessageArgs(message, args));
    }

    public void info(final String message, final Object... args) {
        if (!isLoggable(Level.INFO)) {
            return;
        }
        log(Level.INFO, loggerId + " " + processMessageArgs(message, args));
    }

    public void trace(final String message, final Object... args) {
        if (!isLoggable(Level.TRACE)) {
            return;
        }
        log(Level.TRACE, loggerId + " " + processMessageArgs(message, args));
    }

    public void warn(final String message, final Object... args) {
        if (!isLoggable(Level.WARN)) {
            return;
        }
        log(Level.WARN, loggerId + " " + processMessageArgs(message, args));
    }

    public void debug(final String message) {
        log(Level.DEBUG, loggerId + " " + message);
    }

    public void error(final String message) {
        log(Level.ERROR, loggerId + " " + message);
    }

    public void fatal(final String message) {
        log(Level.ERROR, loggerId + " " + message);
    }

    public void info(final String message) {
        log(Level.INFO, loggerId + " " + message);
    }

    public void trace(final String message) {
        log(Level.TRACE, loggerId + " " + message);
    }

    public void warn(final String message) {
        log(Level.WARN, loggerId + " " + message);
    }


    public void warn(final String message, Throwable e) {
        log(Level.WARN, loggerId + " " + message, e);
    }

    public void trace(final String message, Throwable e) {
        log(Level.TRACE, loggerId + " " + message, e);
    }

    public void info(final String message, Throwable e) {
        log(Level.INFO, loggerId + " " + message, e);
    }

    public void fatal(final String message, Throwable e) {
        log(Level.ERROR, loggerId + " " + message, e);
    }

    public void error(final String message, Throwable e) {
        log(Level.ERROR, loggerId + " " + message, e);
    }

    public void debug(final String message, Throwable e) {
        log(Level.DEBUG, loggerId + " " + message, e);
    }


    private boolean isLoggable(Level l) {
        switch (level) {
            case TRACE:
                return true;
            case DEBUG:
                return !l.equals(Level.TRACE);
            case INFO:
                return !l.equals(Level.TRACE) && !l.equals(Level.DEBUG);
            case WARN:
                return l.equals(Level.WARN) || l.equals(Level.ERROR);
            case ERROR:
                return l.equals(Level.ERROR);
        }
        return false;
    }


    private void logMessage(String message, Level level2log, Throwable t) {
        if (message == null) {
            message = "null";
        }
        switch (level2log) {
            case TRACE:
                if (t != null) {
                    logTarget.trace(TAG, message, t);
                } else {
                    logTarget.trace(TAG, message);
                }
                break;
            case DEBUG:
                if (t != null) {
                    logTarget.debug(TAG, message, t);
                } else {
                    logTarget.debug(TAG, message);
                }
                break;
            case INFO:
                if (t != null) {
                    logTarget.info(TAG, message, t);
                } else {
                    logTarget.info(TAG, message);
                }
                break;
            case WARN:
                if (t != null) {
                    logTarget.warn(TAG, message, t);
                } else {
                    logTarget.warn(TAG, message);
                }
                break;
            case ERROR:
                if (t != null) {
                    logTarget.error(TAG, message, t);
                } else {
                    logTarget.error(TAG, message);
                }
                break;
        }
    }

    private void log(Level level, String message, Throwable e) {
        if (isLoggable(level)) {
            logMessage(message, level, e);
        }
    }

    private void log(Level level, String message) {
        if (isLoggable(level)) {
            logMessage(message, level, null);
        }
    }

    public static void setTag(String TAG) {
        Logger.TAG = TAG;
    }
}




Java Source Code List

net.mantucon.baracus.annotations.Bean.java
net.mantucon.baracus.context.AnnotationScanner.java
net.mantucon.baracus.context.BaracusApplicationContext.java
net.mantucon.baracus.context.BeanContainer.java
net.mantucon.baracus.context.Exceptions.java
net.mantucon.baracus.context.ManagedActivity.java
net.mantucon.baracus.context.ManagedFragment.java
net.mantucon.baracus.dao.BaracusOpenHelper.java
net.mantucon.baracus.dao.BaseDao.java
net.mantucon.baracus.dao.ConfigurationDao.java
net.mantucon.baracus.errorhandling.CustomErrorHandler.java
net.mantucon.baracus.errorhandling.ErrorHandler.java
net.mantucon.baracus.errorhandling.ErrorHandlingFactory.java
net.mantucon.baracus.errorhandling.ErrorSeverity.java
net.mantucon.baracus.errorhandling.StandardErrorHandler.java
net.mantucon.baracus.errorhandling.TextEditErrorHandler.java
net.mantucon.baracus.lifecycle.ApplicationContextInitializer.java
net.mantucon.baracus.lifecycle.Destroyable.java
net.mantucon.baracus.lifecycle.Initializeable.java
net.mantucon.baracus.migr8.MigrationStep.java
net.mantucon.baracus.migr8.ModelVersion000.java
net.mantucon.baracus.model.ConfigurationParameter.java
net.mantucon.baracus.orm.AbstractModelBase.java
net.mantucon.baracus.orm.FieldList.java
net.mantucon.baracus.orm.Field.java
net.mantucon.baracus.orm.Identifiable.java
net.mantucon.baracus.orm.LazyCollection.java
net.mantucon.baracus.orm.LazyMorphicCollection.java
net.mantucon.baracus.orm.LazyReference.java
net.mantucon.baracus.orm.LegacyModelBase.java
net.mantucon.baracus.orm.ModelBase.java
net.mantucon.baracus.orm.NullReference.java
net.mantucon.baracus.orm.ObjectReference.java
net.mantucon.baracus.orm.OptimisticLockingModelBase.java
net.mantucon.baracus.orm.OptmisticLocking.java
net.mantucon.baracus.orm.ReferenceLoader.java
net.mantucon.baracus.orm.Reference.java
net.mantucon.baracus.orm.Timestamped.java
net.mantucon.baracus.signalling.ConfigurationChangeListener.java
net.mantucon.baracus.signalling.DataChangeAwareComponent.java
net.mantucon.baracus.signalling.DataSetChangeAwareComponent.java
net.mantucon.baracus.signalling.DeleteAwareComponent.java
net.mantucon.baracus.signalling.GenericEventAwareComponent.java
net.mantucon.baracus.signalling.GenericEvent.java
net.mantucon.baracus.ui.ConstrainedEditText.java
net.mantucon.baracus.ui.ConstrainedSpinner.java
net.mantucon.baracus.ui.ErrorView.java
net.mantucon.baracus.ui.Popup.java
net.mantucon.baracus.util.DBBackup.java
net.mantucon.baracus.util.DataUtil.java
net.mantucon.baracus.util.DateUtil.java
net.mantucon.baracus.util.DayDate.java
net.mantucon.baracus.util.Logger.java
net.mantucon.baracus.util.StringUtil.java
net.mantucon.baracus.validation.AbstractValidator.java
net.mantucon.baracus.validation.ConstrainedView.java
net.mantucon.baracus.validation.ValidatableView.java
net.mantucon.baracus.validation.ValidationFactory.java
net.mantucon.baracus.validation.Validator.java
net.mantucon.baracus.validation.builtins.DateFromNow.java
net.mantucon.baracus.validation.builtins.NumberMustBeGreaterThanZero.java
net.mantucon.baracus.validation.builtins.StringIsNumericDouble.java
net.mantucon.baracus.validation.builtins.StringIsNumericInteger.java
net.mantucon.baracus.validation.builtins.StringNotEmpty.java