How to automate logcat file and how to find particular message from that file?


#1

I am automating android app using appium in java
I want to capture log of logcat file and then I need to filter it out particular message from the logcat file. Ex:(“Home” message from logcat file)
How can I save logcat file and how can I able to filter it out the specific message from the file?


#2

Use:

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

You can change filter level by your need. The problem is that all log entries are written in the same line, so I made a function that puts each log entry into new line:

private List<LogEntry> orderLogEntries(List<LogEntry> oldEntries) {
    List<LogEntry> newEntries = new ArrayList<>();
    LogEntry tempEntry;
    for (LogEntry entry : oldEntries) {
        tempEntry = new LogEntry(entry.getLevel(), entry.getTimestamp(), entry.getMessage() + "\n");
        newEntries.add(tempEntry);
    }
    return newEntries;
}

… , then I save it to file. If you need to get specific log entry, you will need to parse it out. Maybe help yourself with

entry.getMessage()