如何获取 Appium Server 日志

How to get Appium Server logs

有什么方法可以在测试脚本中获取Appium服务器日志吗

 driver.manage().logs().get("appium server");

或将 appium 服务器日志重定向到控制台

我的主要目的是单独获取 Instrumentation Logs 而不是所有日志

 info: [debug] [INST]  instrument logs

只是在没有我们手头使用的代码的情况下编写一个可能的解决方案,请注意这是我们在 windows 机器上的节点 运行 上所做的,但它也可能适用于 Appium .据我所知,您有一个 IOSDriver 实例,该驱动程序从 RemoteWebDriver 扩展而来,它具有可覆盖的日志方法(不记得名称)。您可以扩展 IOSDriver class 并覆盖该日志记录方法,以便您可以使用您喜欢的日志库在任何您想要的地方进行记录。

来自官方documentation

  • 您可以使用 appium 标志 -g--log 指定要存储日志的文件路径,然后通过 [INST] 过滤它?
  • 您还可以使用标志 -G--webhook 将日志输出发送到 HTTP 侦听器,例如:--webhook localhost:9876。虽然我不知道它是如何工作的,但我想知道!
  • 有标志 --log-timestamp 可以在控制台输出中打开时间戳(默认情况下是 false)。我建议启用它:)

正如@Kirill Zhukov 所说。

您还可以使用标志 -G--webhook 将日志输出发送到 HTTP 侦听器,如下所示:--webhook localhost:9876.

如果您使用 UI,则必须启用此 Log to webhook

我已经创建了一个简单的套接字服务器来监听日志,它可以工作perfectly.Use这个服务器根据您的need.The获取日志,下面的代码将在控制台中打印日志

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;

import java.util.logging.Logger;

public class Server extends Thread {

private static ServerSocket socket;
private static String home = "./";
private static int port = 9876;
private static boolean isAlive = true;
private static final Server server = new Server();

public static Server getServer(String[] args) {
    if (args.length == 2) {
        home = args[1];
    }
    port = Integer.parseInt(args[0]);
    return server;
}

private Server() {

}

public static Server getServer() {
    return server;
}

@Override
public void run() {
    try {
        if (socket != null) {
            if (!socket.isClosed()) {
                if (port == getPort()) {
                    System.err.println("Server active at the Same Port! ");
                } else {
                    close();
                }
            }
        }
        socket = new ServerSocket(port);
        port = socket.getLocalPort();
        System.out.println("Server accepting connections on port :" + port);
        socket.setReceiveBufferSize(146988);
        handlleRequest();
    } catch (IOException e) {
        System.err.println("Could not start server: " + e.getMessage());
        port = 0;
        run();
    }
}

public int getPort() {
    return socket == null ? 0 : port;
}

public String getPortS() {
    return String.valueOf(port);
}

public void Stop() {
    isAlive = false;
}

public void handlleRequest() {
    while (isAlive) {
        System.out.println("Test");
        try (Socket connection = socket.accept()) {
            display(connection);
        } catch (IOException e) {
            System.err.println(e);
        }
    }
}

public void display(Socket connection) {
    try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
        String line = in.readLine();
        while (in.ready() && line != null) {
            System.out.println(line);
            line = in.readLine();
        }
    } catch (IOException ex) {
        Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
    }
}

public void close() throws IOException {
    if (socket != null && !socket.isClosed()) {
        isAlive = false;
        socket.close();
    }
}

public static void main(String[] args) {
    args = new String[]{"9876", "./"};
    Server f = Server.getServer(args);
    f.start();
}

}