你只需要使用
java.util.logging.Logger.getLogger(yourFullClassName).setLevel(java.util.logging.Level.All);
和
adb shell setprop log.tag.correspondingTag VERBOSE
Android使用这个函数从类全名中获取对应的Tag:
public static String loggerNameToTag(String loggerName)
{
if (loggerName == null) {
return "null";
}
int length = loggerName.length();
if (length <= 23) {
return loggerName;
}
int lastPeriod = loggerName.lastIndexOf(".");
return length - (lastPeriod + 1) <= 23 ? loggerName.substring(lastPeriod + 1) : loggerName.substring(loggerName.length() - 23);
}
例如,我想为“org.apache.http.impl.client.DefaultRequestDirector”类启用日志记录,请执行以下操作:
String clzName = "org.apache.http.impl.client.DefaultRequestDirector";
String newClzName = loggerNameToTag(clzName);
System.out.println("className:" + clzName + " tagName is " + newClzName); //get tagName from class full name,and then it will be used in setprop
Logger jdkLogger = Logger.getLogger(clzName);
jdkLogger.setLevel(Level.ALL);
if (jdkLogger.isLoggable(Level.FINE))
{
jdkLogger.log(Level.FINE, "jdk log msg");
jdkLogger.log(Level.Fine,"tagName is")
}
然后在 adb shell
setprop log.tag.DefaultRequestDirector VERBOSE