当前位置:  开发笔记 > Android > 正文

在Android Studio logcat中显示java.util.logging.Logger日志

如何解决《在AndroidStudiologcat中显示java.util.logging.Logger日志》经验,为你挑选了1个好方法。

我需要在Android studio logcat中观看socket.io-client-java日志.例如:https://github.com/socketio/socket.io-client-java/blob/master/src/main/java/io/socket/client/IO.java#L70

    为什么我logger.fine(String.format("ignoring socket cache for %s", source));在logcat 中看不到输出?

    记录器例如上述的设定电平在Fine到消息,但在Android Studio中的logcat没有这个日志级别(只Verbose,Debug,Info,Warn,Error,Assert).我误解了这种不匹配.请解释一下.

小智.. 8

好的,Logcat没有显示它,因为默认记录器级别是info.

这个解决方案对我很有用.我现在在主要活动中调试socket.io

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

可能的util类

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

这是原始答案:https: //stackoverflow.com/a/9047282/5127756



1> 小智..:

好的,Logcat没有显示它,因为默认记录器级别是info.

这个解决方案对我很有用.我现在在主要活动中调试socket.io

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

可能的util类

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

这是原始答案:https: //stackoverflow.com/a/9047282/5127756

推荐阅读
linjiabin43
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有