Skip to content

Instantly share code, notes, and snippets.

@gotev
Last active March 7, 2016 20:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gotev/4cf92dd36287ff3198be to your computer and use it in GitHub Desktop.
Save gotev/4cf92dd36287ff3198be to your computer and use it in GitHub Desktop.
Pluginnable Android Logger

Purpose

It's a good thing to have a pluginnable log system, to be able to redirect logs to different locations or to change the logging library if needed, without having to manually modify each occurrence.

There are various ways to accomplish this. One easy and simple way is what I've posted here. If you need more sophisticated things, maybe you should look somewhere else. I use the following classes in all of my open source libraries here on GitHub, so I thought they may be useful also for other projects. Just copy and paste the two classes in this Gist in your project, and then use Logger to log your messages. For example:

Logger.debug("MyClass", "MyMessage");

You can set the log level (by default is DEBUG for debug builds and OFF for release builds):

Logger.setLogLevel(LogLevel.DEBUG);

And you can easily provide your custom logger implementation:

Logger.setLoggerDelegate(new Logger.LoggerDelegate() {
    @Override
    public void error(String tag, String message) {
        //your own implementation here
    }

    @Override
    public void error(String tag, String message, Throwable exception) {
        //your own implementation here
    }

    @Override
    public void debug(String tag, String message) {
        //your own implementation here
    }

    @Override
    public void info(String tag, String message) {
        //your own implementation here
    }
});

And that's all, folks :)

import android.util.Log;
/**
* Default logger delegate implementation which logs in LogCat with {@link Log}.
* Log tag is set to <b>Logger</b> for all the logs.
* @author gotev (Aleksandar Gotev)
*/
public class DefaultLoggerDelegate implements Logger.LoggerDelegate {
private static final String TAG = "Logger";
@Override
public void error(String tag, String message) {
Log.e(TAG, tag + " - " + message);
}
@Override
public void error(String tag, String message, Throwable exception) {
Log.e(TAG, tag + " - " + message, exception);
}
@Override
public void debug(String tag, String message) {
Log.d(TAG, tag + " - " + message);
}
@Override
public void info(String tag, String message) {
Log.i(TAG, tag + " - " + message);
}
}
/**
* Pluginnable logger.
* You can provide your own logger delegate implementation, to be able to log in a different way.
* By default the log level is set to DEBUG when the build type is debug, and OFF in release.
* The default logger implementation logs in Android's LogCat.
* @author gotev (Aleksandar Gotev)
*/
public class Logger {
public enum LogLevel {
DEBUG,
INFO,
ERROR,
OFF
}
public interface LoggerDelegate {
void error(String tag, String message);
void error(String tag, String message, Throwable exception);
void debug(String tag, String message);
void info(String tag, String message);
}
private LogLevel mLogLevel = BuildConfig.DEBUG ? LogLevel.DEBUG : LogLevel.OFF;
private LoggerDelegate mDelegate = new DefaultLoggerDelegate();
private Logger() { }
private static class SingletonHolder {
private static final Logger instance = new Logger();
}
public static void resetLoggerDelegate() {
synchronized (Logger.class) {
SingletonHolder.instance.mDelegate = new DefaultLoggerDelegate();
}
}
public static void setLoggerDelegate(LoggerDelegate delegate) {
if (delegate == null)
throw new IllegalArgumentException("delegate MUST not be null!");
synchronized (Logger.class) {
SingletonHolder.instance.mDelegate = delegate;
}
}
public static void setLogLevel(LogLevel level) {
synchronized (Logger.class) {
SingletonHolder.instance.mLogLevel = level;
}
}
public static void error(String tag, String message) {
if (SingletonHolder.instance.mLogLevel.compareTo(LogLevel.ERROR) <= 0) {
SingletonHolder.instance.mDelegate.error(tag, message);
}
}
public static void error(String tag, String message, Throwable exception) {
if (SingletonHolder.instance.mLogLevel.compareTo(LogLevel.ERROR) <= 0) {
SingletonHolder.instance.mDelegate.error(tag, message, exception);
}
}
public static void info(String tag, String message) {
if (SingletonHolder.instance.mLogLevel.compareTo(LogLevel.INFO) <= 0) {
SingletonHolder.instance.mDelegate.info(tag, message);
}
}
public static void debug(String tag, String message) {
if (SingletonHolder.instance.mLogLevel.compareTo(LogLevel.DEBUG) <= 0) {
SingletonHolder.instance.mDelegate.debug(tag, message);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment