1. Logcat Base
ADB Logcat Filtering method (fetch log)
2.1 Logcat Tool
In the ADB Logcat filtering method (crawl log) This article mentions some ways to filter the log,
However, this does not mean that the output log is not displayed.
In addition, in some cases need to set the log switch, many people are by adding a sentence of judgment, which is too inefficient, so they have a tool class.
2.2 Principle
2.2.1 Debug_level
First set up several debuglevel (corresponding to ADB logcat)
private static byte LOGE = 0x01; 0000 0001 private static byte LOGW = 0x02; 0000 0010 private static byte Logi = 0x04; 0000 0100 private static byte logd = 0x08; 0000- private static byte LOGV = 0x10; 0001 0000
DebugLevel that need to be filtered:
private static byte Debug_level = 0xFF;
This is to determine if the log needs to be printed as long as the two phases are
Debug_level & LOGX
debug_level_e 0x01 0000 0001
Debug_level_w 0x03 0000 0011
debug_level_i 0x07 0000 0111
debug_level_d 0x0f 0000 1111
Debug_level_v 0x1f 0001 1111
Like what:
public static void I (string tag, string msg) { if ((Logi & debug_level)! = 0) { tag = project_name + tag; LOG.I (tag, msg); } }
What level of logging you want to output requires only the Debug_level set:
The centralized default debug_level is set according to the ADB logcat mechanism:
public static byte Debug_level_n = 0x00; 0000 0000 public static byte debug_level_e = 0x01; 0000 0001 public static byte Debug_level_w = 0x03; 0000 0011 public static byte debug_level_i = 0x07; 0000 0111 public static byte debug_level_d = 0x0f; 0000 1111 public static byte Debug_level_v = 0x1f; 0001 1111
which
Debug_level_n: Do not output any logs
Debug_level_v: Output All logs
2.2.2 Tag Tag
I expanded the ADB label to 2 levels according to the requirements:
The first level is the project label:
private static String project_name = "project-";
The second level is the module tag tag
Passing through a function
public static void I (string tag, string msg) { if ((Logi & debug_level)! = 0) { tag = project_name + tag; LOG.I (tag, msg); } } <strong></strong>
2.2.3 Filter Log
So we can filter the log for this project,
ADB Logcat | Grep-e ' ^./project '
You can also filter the log of a module
ADB Logcat | Grep-e ' ^./project-tool '
3. Implementing the Code
Import Android.util.log;public class Logtool {private static String project_name = "project-"; private static byte LOGE = 0x01; 0000 0001 private static byte LOGW = 0x02; 0000 0010 private static byte Logi = 0x04; 0000 0100 private static byte logd = 0x08; 0000-private static byte LOGV = 0x10; 0001 0000//debug_level & LOGX//debug_level_e 0x01 0000 0001//debug_level_w 0x03 0000 0011 debug_level_i 0x07 0000 0111//debug_level_d 0x0f 0000 1111//debug_level_v 0x1f 0001 1111 public static byte Debug_level_n = 0x00; 0000 0000 public static Byte debug_level_e = 0x01; 0000 0001 public static byte Debug_level_w = 0x03; 0000 0011 public static byte debug_level_i = 0x07; 0000 0111 public static byte debug_level_d = 0x0f; 0000 1111 public static byte Debug_level_v = 0x1f; 0001 1111 private static byte Debug_level = 0x1f; 00011111 public static void SetUp (String projecttag, byte debuglevel) {if (Projecttag! = null && Projecttag . Length () > 0) project_name = projecttag + "-"; Debug_level = DebugLevel; } public static void I (string tag, string msg) {if ((Logi & debug_level)! = 0) {tag = Project_n AME + tag; LOG.I (tag, msg); }} public static void D (string tag, string msg) {if ((LOGD & debug_level)! = 0) {tag = PROJ Ect_name + tag; LOG.D (tag, msg); }} public static void W (string tag, string msg) {if ((LOGW & debug_level)! = 0) {tag = PROJ Ect_name + tag; LOG.W (tag, msg); }} public static void W (string tag, string msg, Throwable tr) {if ((LOGW & debug_level)! = 0) { Tag = project_name + tag; LOG.W (Tag, MSG, TR); }} public static void E (string tag, string msg) {if (LOGE &Amp Debug_level)! = 0) {tag = project_name + tag; LOG.E (tag, msg); }} public static void V (string tag, string msg) {if ((LOGV & debug_level)! = 0) {tag = PROJ Ect_name + tag; LOG.V (tag, msg); } }}
4. Usage examples
Need to set debug_level and project_name before use
Logtool.setup ("MyProject", Videologger.debug_level_v);
Print log:
Private final String TAG = "Main"; LOGTOOL.V (TAG, "Do Test >>>");
Filter logs See also: 2.2.3
5. Expansion
The current debug_level is manually set, further words can be written to the configuration file Debug_level, so only need to modify the configuration file to control the level of output, more convenient.
ADB Logcat Tool and DebugLevel settings