Android: Logcat filter by tag using regular expression

When registering my tags always look like this:

com.myapp.SomeActivity 

For each log statement, I add the first part of "com.myapp". to the class tag that makes the log call. For instance:

 MyActivity extends Activity{ private static final String TAG = "MyActivity"; public void someMethod(){ LogWrapper.e(TAG, "Something is wrong here."); } } 

My LogWrapper class basically does the following

 public class LogWrapper { private static final String applicationTAG = "com.myapp."; public static void e(String classTag, String message){ Log.e(applicationTAG + classTag, message); } } 

So, all I do is add a static tag to the class tag. The reason is because I want to filter the output of logcat with the log operations that my application writes, and to avoid cluttering my log file with logs from the system or other applications.

Therefore, I came up with getting the logs as follows:

 String command = "logcat -d -v time com.myapp.*:I *:S" Process process = Runtime.getRuntime().exec(command); BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(process.getInputStream())); StringBuilder log = new StringBuilder(); String line; while ((line = bufferedReader.readLine()) != null) { log.append(line); log.append(StringUtils.NEW_LINE); } 

However, in the end I get an empty log file. Which makes me the subject of the regular expression "com.myapp.:I" does not work. I also tried to exclude asterix (), but this does not work. However, if I use "*: I", that works.

Which makes me wonder if it is only possible to filter by the full name of the tag? Is there a way that I can get the log filtered by "com.myapp".

I assume that plan B will be that in a while loop I could check if the string contains my tag and only add it if it is, but I really would like to avoid it ..

+6
source share
1 answer

Well, to solve the problem, I refused the more elegant (and still unknown) solution for Plan B, which I hinted at in my first post. I use the following regular expression to filter each line of the log file that I received using the logcat -d -v time * command : I (executed using Runtime.getRuntime (). Exec (command)):

 .*([DIWE]{1}/com.myapp.).* 

The above regex matches log statements as follows:

 I/com.myapp.MyClass Crashed and did not recover - too bad. 

[DEWI] means that the log level must be either D (ebug), E (rror), W (arning), or I (nfo) to match the regular expression. If you also want to extract the statement V (erbose), just add "V".

+2
source

Source: https://habr.com/ru/post/911203/


All Articles