How to get Appium Server logs
Asked Answered
L

4

5

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

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

or redirect the appium server logs to console

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

 info: [debug] [INST]  instrument logs
Leonteen answered 16/7, 2015 at 13:10 Comment(0)
L
5

As said by @Kirill Zhukov.

Also you can send log output to HTTP listener using flag -G or --webhook like that: --webhook localhost:9876.

If you're using UI you have to enable this Log to 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

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

}
Leonteen answered 17/7, 2015 at 14:58 Comment(0)
A
2

From official documentation:

  • You can specify file path where you want to store logs using appium flag -g or --log and later filter it by [INST] maybe?
  • Also you can send log output to HTTP listener using flag -G or --webhook like that: --webhook localhost:9876. I don't know how it works though, but would like to know!
  • There is flag --log-timestamp which turns on timestamps in console output (by default it is to false). I'd recommend to enable it :)
Aurlie answered 17/7, 2015 at 4:35 Comment(1)
Thanks.Anyhow check the answer I've implemented the second optionLeonteen
C
1

There is one more option, you can start the appium server from the Editor. Write a custom function, to start and stop the server from you're test, While doing so you can see the server logs in the console - in runtime. and after test completion. Logs can be captured and converted to any other formats from the editor.

Addon: If you schedule the CI/CD through Jenkins, Logs will also be a part of the report.

Refer to below Code to start appiumserver Automatically: public void startNewAppiumServer() {

    AppiumDriverLocalService service = new AppiumServiceBuilder()
            .withAppiumJS(new File(
                    "C:\\Users\\John\\AppData\\Roaming\\npm\\node_modules\\appium\\build\\lib\\main.js"))
            .withIPAddress("127.0.0.1").usingPort(4723).build();
    this.service = service;
    service.start();
}
Charlinecharlock answered 13/2, 2023 at 10:11 Comment(0)
C
0

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.

Charmer answered 16/7, 2015 at 21:5 Comment(1)
We can get only the webdriver logs not the appium logs.Anyhow I've found a solutionLeonteen

© 2022 - 2025 — McMap. All rights reserved.