Timber是一个轻量级的第三方库,能够帮助好的使用Android Log进行打印日志。
Timeber的使用主要通过添加Tree实例,添加Tree实例可以通过方法Timber.plant完成。实例化Tree需要尽量早,推荐在项目Application的onCreate()方法中完成。
其中,DebugTree自动实例化,是Timber中已经实现了tree的类,可直接拿来用,并在使用他的类中以类名为其TAG,但是其他的Tree不会自动实例化,需要进行手动实现扩展Timber.Tree实现。
implementation 'com.jakewharton.timber:timber:4.7.0'
public class TimberUtils {
private static final String LOG_FILE_PATH = Environment.getExternalStorageDirectory().getPath() + File.separator + "Card_Payment" + File.separator;
private static TimberUtils instance;
private TimberUtils() {
setLogAuto();
}
/**
* Log can auto-open in debug mode and close in release mode,we can control this int onCreate method of Application class
*/
private void setLogAuto() {
// if (BuildConfig.DEBUG) {//debug version
// Timber.plant(new Log4jTree(false));
// } else {//release version
// Timber.plant(new Log4jTree(false));
// }
Timber.plant(new Log4jTree(false));
}
public synchronized static TimberUtils getInstance() {
if (instance == null) {
instance = new TimberUtils();
}
return instance;
}
private class Log4jTree extends Timber.Tree {
private Logger logger;
private static final String LOG_PATTERN = "[%d{yyyy-MM-dd HH:mm:ss}] - [Level: %-5p] - %m%n";
private static final String LOG_CAT_PATTERN = "[Level: %-5p] - %m%n";
private static final long LOG_FILE_SIZE = 1048576L;
private static final int CALL_STACK_INDEX = 5;
Log4jTree(boolean release) {
File logDir = new File(LOG_FILE_PATH);
if (!logDir.exists()) {
if (!logDir.mkdirs()) {
throw new RuntimeException("make dir error: " + LOG_FILE_PATH);
}
}
LogConfigurator logConfigurator = new LogConfigurator();
logConfigurator.setFileName(LOG_FILE_PATH + File.separator + "card_payment.log");
if (release) {
logConfigurator.setRootLevel(Level.INFO);
logConfigurator.setUseLogCatAppender(false);
} else {
logConfigurator.setRootLevel(Level.DEBUG);
logConfigurator.setUseLogCatAppender(true);
logConfigurator.setLogCatPattern(LOG_CAT_PATTERN);
}
logConfigurator.setUseFileAppender(true);
logConfigurator.setFilePattern(LOG_PATTERN);
logConfigurator.setImmediateFlush(true);
logConfigurator.setMaxBackupSize(5);
logConfigurator.setMaxFileSize(LOG_FILE_SIZE);
logConfigurator.configure();
logger = Logger.getLogger(TimberUtils.class.getSimpleName());
}
@Override
protected void log(int priority, String tag, String msg, Throwable t) {
msg = String.format("%-60s - %s", getStackTraceInfo(new Throwable().getStackTrace()), msg);
switch (priority) {
case Log.DEBUG:
logger.debug(msg, t);
break;
case Log.INFO:
logger.info(msg, t);
break;
case Log.WARN:
logger.warn(msg, t);
break;
case Log.ERROR:
logger.error(msg, t);
break;
case Log.ASSERT:
logger.fatal(msg, t);
break;
default:
break;
}
}
private String getStackTraceInfo(StackTraceElement[] elements) {
StackTraceElement element = elements[CALL_STACK_INDEX];
String className = element.getClassName()
.substring(element.getClassName().lastIndexOf(".") + 1)
.replaceAll("\\$1", "")
.replaceAll("\\$override", "");
String methodName = element.getMethodName();
int line = element.getLineNumber();
return String.format(Locale.getDefault(), "[Method: %s#%s(%d)]", className, methodName, line);
}
}
}
//指定tag的名字,默认是的tag是当前的Activity类名
//Timber.tag(“activityName”);
Timber.d("这个是d");
Timber.v("这个是v");
Timber.i("这个是i");
Timber.e("这个是e");