java 如何获取 Appium Server 日志

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31455145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 18:38:08  来源:igfitidea点击:

How to get Appium Server logs

javaiosseleniuminstrumentsappium

提问by Madhan

Is there any way to get the Appium server logs in the test script like

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

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

or redirect the appium server logs to console

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

My main purpose is to get the Instrumentation Logs alone not all logs

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

 info: [debug] [INST]  instrument logs

回答by Madhan

As said by @Kirill Zhukov.

正如@Kirill Zhukov 所说。

Also you can send log output to HTTP listener using flag -Gor --webhooklike that: --webhook localhost:9876.

您也可以输出日志发送到HTTP侦听标志使用-G--webhook那样:--webhook localhost:9876

If you're using UI you have to enable this Log to webhook

如果您使用 UI,则必须将此日志启用到 webhook

enter image description here

在此处输入图片说明

I have created a simple Socket server to listen to the logs and it works perfectly.Use this server to get the logs as per your need.The below code will print the logs in the console

我创建了一个简单的 Socket 服务器来监听日志,它运行良好。根据您的需要使用此服务器获取日志。下面的代码将在控制台中打印日志

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();
}

}

回答by Kirill Zhukov

From official documentation:

来自官方文档

  • You can specify file path where you want to store logs using appium flag -gor --logand later filter it by [INST]maybe?
  • Also you can send log output to HTTP listener using flag -Gor --webhooklike that: --webhook localhost:9876. I don't know how it works though, but would like to know!
  • There is flag --log-timestampwhich turns on timestamps in console output (by default it is to false). I'd recommend to enable it :)
  • 您可以使用 appium 标志指定要存储日志的文件路径,-g或者--log稍后过滤它[INST]
  • 您也可以输出日志发送到HTTP侦听标志使用-G--webhook那样:--webhook localhost:9876。我不知道它是如何工作的,但很想知道!
  • 有一个标志--log-timestamp可以在控制台输出中打开时间戳(默认情况下是 to false)。我建议启用它:)

回答by amk

Just writing a possible solution without having the code we use at hand, notice this is what we have done on our nodes running in a windows machine, but it may work for Appium aswell. AFAIK there is an IOSDriver which you instance, that driver extends from RemoteWebDriver which has an overridable log method (don't remember the name). You may extend the IOSDriver class and override that logging method so that you can log wherever you want using the log library you like.

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