Search code examples
seleniumadbappiumlogcat

How to get ADB logcat with specific tag in appium?


i want to get the ADB logcats for a specific tag(ex: testing_aws) through APPIUM. actually i have done this directly from console like follows

  • I navigated to /home/jagadeesh/android-sdk-linux/platform-tools
  • Then i ran this commandadb shell logcat | grep testing_aws which is successfully given the data as expected
  • Now i want to achieve same data through appium
  • i have seen some solutions like List<LogEntry> adbLogs =driver().manage().logs().get("logcat").filter(Level.ALL); which it will give complete device logs, but i couldn't found a way to filter with specific tag (ex: testing_aws)
  • So if there is a way to filter by tagname and get the output through appium please help me.

Solution

  • You can first get all logs

    List<LogEntry> logEntries = driver.manage().logs().get("logcat").filter(Level.ALL);
    

    And then you can filter your log.

    for (LogEntry logEntry : logEntries) {
        if (logEntry.getMessage().contains("testing_aws")) {
            System.out.println(logEntry.getMessage());
        }
    }
    

    If you want to add the filtered log in the file, you can do it like following:

    List<LogEntry> logEntries = driver.manage().logs().get("logcat").filter(Level.ALL);
    File logFile = new File("path to store file"+"filename"+".txt");
    logFile.getParentFile().mkdirs();
    
    PrintWriter log_file_writer = new PrintWriter(logFile);
    for (LogEntry logEntry : logEntries) {
        if (logEntry.getMessage().contains("testing_aws")) {
            log_file_writer.println(logEntry);
        }
    }
    log_file_writer.flush();