先上代码,粘贴即用,类名还是定义为Log,当然需要和android原生Log区分的话,可以自行修改。其中的方法he原生Log类提供的方法一样。
/**
* LogUtils tool description:
* Only the logs of grades greater than or equal to LEVEL can be printed.
* so you can selectively log out by modifying LEVEL after development and product release
* when LEVEL=ASSERT blocked all logs
*
* Of course you can use it also like that in your class:
* private static final boolean DEBUG = true && Log.isLoggable(TAG, Log.DEBUG);
* This allows you to control the Log output of this class individually, the level can define yourself
* and it's more efficient.
*/
public class Log {
private static final int VERBOSE = 2;
private static final int DEBUG = 3;
private static final int INFO = 4;
private static final int WARN = 5;
private static final int ERROR = 6;
private static final int ASSERT = 7;
private static final int LEVEL = VERBOSE;
public static void v(String tag, String message) {
if (isLoggable(tag, VERBOSE)) {
android.util.Log.v(tag , message);
}
}
public static void d(String tag, String message) {
if (isLoggable(tag, DEBUG)) {
android.util.Log.d(tag , message);
}
}
public static void i(String tag, String message) {
if (isLoggable(tag, INFO)) {
android.util.Log.i(tag , message);
}
}
public static void w(String tag, String message) {
if (isLoggable(tag, WARN)) {
android.util.Log.w(tag , message);
}
}
public static void e(String tag, String message) {
if (isLoggable(tag, ERROR)) {
android.util.Log.e(tag , message);
}
}
/**
* Checks to see whether or not a log for the specified tag is loggable at the specified level.
* Only when the Os Build type is debug, or the specified tag is loggable at the specified level,
* it is allowed logged;
* Meanwhile we can change the LEVEL in this class to control whether or not this is allowed to
* be logged.
*
* @param tag The tag to check.
* @param level The level to check.
* @return Whether or not that this is allowed to be logged.
*/
public static boolean isLoggable(String tag, int level) {
try {
return (isDebugOsBuild() || shouldLog(tag, level)) && (LEVEL <= level);
} catch (IllegalArgumentException ex) {
android.util.Log.e("error", "Tag too long !!!");
return false;
}
}
/**
* Checks to see whether or not a log for the specified tag is loggable at the specified level.
*
* The default level of any tag is set to INFO. This means that any level above and including
* INFO will be logged. Before you make any calls to a logging method you should check to see
* if your tag should be logged. You can change the default level by setting a system property:
* 'setprop log.tag.<YOUR_LOG_TAG> <LEVEL>'
* Where level is either VERBOSE, DEBUG, INFO, WARN, ERROR, ASSERT, or SUPPRESS. SUPPRESS will
* turn off all logging for your tag. You can also create a local.prop file that with the
* following in it:
* 'log.tag.<YOUR_LOG_TAG>=<LEVEL>'
* and place that in /data/local.prop.
*
* @param tag The tag to check.
* @param level The level to check.
* @return Whether or not that this is allowed to be logged.
* @throws IllegalArgumentException is thrown if the tag.length() > 23.
*/
private static boolean shouldLog(String tag, int level) {
return android.util.Log.isLoggable(tag, level);
}
/**
* @return If OsBuild is "userdebug" or "eng", return true; otherwise return false;
*/
private static boolean isDebugOsBuild() {
return "userdebug".equals(Build.TYPE) || "eng".equals(Build.TYPE);
}
/**
* @return If apk build version is "debug", return true; otherwise return false;
*/
public static boolean isApkDebug(Context context) {
try {
ApplicationInfo info = context.getApplicationInfo();
return (info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
}
用法在注释中写的比较清楚,在下面总结一下Log管控的方法,可以根据不同的使用场景,选择不同的方法对Log进行管控:
(1)根据不同的系统版本(release, userdebug, eng),选择是否打印log;
(2)通过设置变量"LEVEL"的大小,设置打印Log的等级;
(3)可以通过修改系统打印Log的等级来控制打印哪些等级的log,详见shouldLog()方法的注释;
(4)如果用在Apk中,可以根据当前Apk的build版本(release,debug),选择是否打印log;
(5)可以在自己的类中定义变量,控制是否打印Log,例如:
private static final boolean DEBUG = true && Log.isLoggable(TAG, Log.DEBUG);
欢迎 扫一扫 下方二维码
关注我的公众号 阅读更多文章