简体   繁体   中英

How to print to Android Studio Logcat from (fork of) Third Party Library?

I'm currently working with a fork of a third party library for Android and would like to print some information to the Android Studio console (Logcat). Normally you can simply call something like:

Log.i("mytag", "my message here");

But this doesn't seem to work when calling from a library. Does anyone know how to go about printing to the Android Studio Logcat Console from a library? Any tips or hints pointing me in the right direction would be appreciated! Thanks

UPDATE: This is a native Java/Android library for React-Native. The library I'm working with is a fork of react-native-webview. I don't know if any of that information is particularly important, but for context I thought I should mention the specifics of what I'm working with.

There are ways to do it if the default one is not working for you. The one is to use either custom Logger or use automatically created via this class static factories

Logger.getLogger(String name, String localizationBundle)

or

Logger.getLogger(String name) ).

And then use its log method which has a lot of overloads for the variety of cases. Its usage is pretty straightforward:

// usual name for the logger is the name of the main class of the library

private val logger = Logger.getLogger(MyLibraryMainClass::class.java.name)
logger.log(logLevel, message) //where logLevel is Level.WARNING or Level.INFO etc..

Also Log.println(int priority, String tag, String msg) may work.

Also try checking the settings of your logcat - it may be misconfigured. You may try to do it via adb logcat command also.

I am assuming this is a native library (C, C++). If so, you can follow a similar approach as someone writing a android NDK library can follow. You need to include

#include <android/log.h>

Then you will be able to access the __android_log_print method to print the log statement. You should be able to see this statement in the logcat with the tag. This is the prototype:

int __android_log_print(int prio, const char *tag,  const char *fmt, ...);

More details are here: https://developer.android.com/ndk/reference/group/logging

An example would be:

__android_log_print(ANDROID_LOG_DEFAULT, "Your library tag", "Your log statement");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM