Skip to content

Instantly share code, notes, and snippets.

@dnanib
Last active December 4, 2020 03:47
Show Gist options
  • Save dnanib/20e02b7c4873a048afb056b372017811 to your computer and use it in GitHub Desktop.
Save dnanib/20e02b7c4873a048afb056b372017811 to your computer and use it in GitHub Desktop.
Lombok @CustomLog for Android's Log.* logging system
package com.example.log;
import android.util.Log;
import com.example.BuildConfig;
import com.google.common.io.ByteStreams;
import java.io.PrintStream;
public class AndroidLogger extends PrintStream {
private final String mLogtag;
AndroidLogger(final String name) {
super(ByteStreams.nullOutputStream());
mLogtag = getLogtag(name);
}
private String getLogtag(final String name) {
String tagBase = BuildConfig.LOGTAG_PREFIX + name;
if (tagBase.length() <= 23) {
return tagBase;
}
return tagBase.substring(0, 22) + "+";
}
public void d(String m) {
if (BuildConfig.DEBUG) {
Log.d(mLogtag, m);
}
}
public void i(String m) {
Log.i(mLogtag, m);
}
public void e(String m, Throwable tr) {
Log.e(mLogtag, m, tr);
}
public void e(String m) {
Log.e(mLogtag, m);
}
@Override
public void println(String m) {
d(m);
}
}
package com.example.log;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
public class AndroidLoggerFactory {
private static final ConcurrentMap<Class<?>, AndroidLogger> sLoggerMap = new ConcurrentHashMap<>();;
/**
* This is adapted from http://www.slf4j.org/apidocs/src-html/org/slf4j/simple/SimpleLoggerFactory.html
*/
public static AndroidLogger getLogger(final Class<?> type) {
AndroidLogger logger = sLoggerMap.get(type);
if (logger != null) {
return logger;
}
AndroidLogger newLogger = new AndroidLogger(type.getSimpleName());
AndroidLogger oldLogger = sLoggerMap.putIfAbsent(type, newLogger);
return oldLogger == null ? newLogger : oldLogger;
}
}
android {
buildTypes {
buildTypes.each {
// A prefix that helps us filter to our own log messages in Android Studio's Logcat viewer.
it.buildConfigField 'String', 'LOGTAG_PREFIX', '"ABC/"'
}
}
}
dependencies {
implementation "com.google.guava:guava:29.0-android"
}
# This is how a log message, triggered as:
# @CustomLog
# public class LoginActivity {
#
# @Override
# void onLoginSuccess() {
#
# log.d("User Login succeeded");
# }
# }
# Will look like in logcat.
2020-09-15 19:51:59.902 16387-16387/com.example.app D/ABC/LoginActivity: User Login succeeded
Note that the Lombok-introduced static variable, log, can be passed to any method that needs a PrintStream. So instead of a method doing:
void foo() {
System.err.println("Some stuff");
}
You can write it as:
void foo(PrintStream stream) {
stream.println("Some stuff");
}
Then, from Android you can call this method as foo(log) and from elsewhere, as foo(System.err).
lombok.log.custom.declaration = com.example.log.AndroidLogger com.example.log.log.AndroidLoggerFactory.getLogger(TYPE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment