Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show java.util.logging.Logger logs in Android Studio logcat

I need to watch the log from socket.io-client-java in Android Studio logcat. For example: https://github.com/socketio/socket.io-client-java/blob/master/src/main/java/io/socket/client/IO.java#L70

  1. Why don't I see the output of logger.fine(String.format("ignoring socket cache for %s", source)); in the logcat?

  2. The logger in the above example is set to Fine, but in Android Studio logcat there is no Fine level (only Verbose, Debug, Info, Warn, Error, Assert). How to translate the level from java.util.logging.Logger to Android Studio logcat?

like image 760
igor_rb Avatar asked Dec 08 '15 06:12

igor_rb


People also ask

How do I view syslog on Android?

I usually access the system log, and event log by this sequence: 1) open any page online, notice the three dots in upper right corner of screen. Cluck on those dots. NOTE: THIS WILL ONLY ACCESS THE LOG INFO FOR THE DEVICE YOU ARE USING TO VIEW THE LOGS.


1 Answers

OK, Logcat doesn't show it because default logger level is info.

Here is solution that works for me very well. I debug socket.io now in main activity

                import io.socket.client.Manager;
                import io.socket.client.Socket;
                @Override
                    protected void onCreate(Bundle savedInstanceState) {
                        super.onCreate(savedInstanceState);
                        AndroidLoggingHandler.reset(new AndroidLoggingHandler());           
                        java.util.logging.Logger.getLogger(Socket.class.getName()).setLevel(Level.FINEST);
                        java.util.logging.Logger.getLogger(io.socket.engineio.client.Socket.class.getName()).setLevel(Level.FINEST); 
                        java.util.logging.Logger.getLogger(Manager.class.getName()).setLevel(Level.FINEST);
                        }

Possible util class

import android.util.Log;
import java.util.logging.*;
public class AndroidLoggingHandler extends Handler {

    public static void reset(Handler rootHandler) {
        Logger rootLogger = LogManager.getLogManager().getLogger("");
        Handler[] handlers = rootLogger.getHandlers();
        for (Handler handler : handlers) {
            rootLogger.removeHandler(handler);
        }
        LogManager.getLogManager().getLogger("").addHandler(rootHandler);
    }

    @Override
    public void close() {
    }

    @Override
    public void flush() {
    }

    @Override
    public void publish(LogRecord record) {
        if (!super.isLoggable(record))
            return;

        String name = record.getLoggerName();
        int maxLength = 30;
        String tag = name.length() > maxLength ? name.substring(name.length() - maxLength) : name;

        try {
            int level = getAndroidLevel(record.getLevel());
            Log.println(level, tag, record.getMessage());
            if (record.getThrown() != null) {
                Log.println(level, tag, Log.getStackTraceString(record.getThrown()));
            }
        } catch (RuntimeException e) {
            Log.e("AndroidLoggingHandler", "Error logging message.", e);
        }
    }

    static int getAndroidLevel(Level level) {
        int value = level.intValue();
        if (value >= 1000) {
            return Log.ERROR;
        } else if (value >= 900) {
            return Log.WARN;
        } else if (value >= 800) {
            return Log.INFO; 
        } else {
            return Log.DEBUG;
        }
    }
}

here is original answer: https://stackoverflow.com/a/9047282/5127756

like image 126
Roman Masarovič Avatar answered Oct 06 '22 04:10

Roman Masarovič