svs teja
svs teja

Reputation: 1027

Logging in android when java jar is imported

I have a sampleApplication which has only one class and class Name is Test

Following are the scenarios:

Case1:

public class Test {

    public void callme()
    {
        System.out.println("Inside Call me Method");
    }
}

Case2:

public class Test {
    private static final Logger log = LoggerFactory.getLogger(Test.class);
    public void callme()
    {
        log.info("Inside Call me Mthod using logback");
    }
}

The use case is as follows:

I have to bundle this Test.class into a jar and use it android project..In case1 when i bundle the test class in jar and use it in android project...Inside the android application logs i am able to see the sysout statements.

But as per the practices sysouts must be replace with some logging framework.

So i Have tried scenario2, I have implemented logback along with the Test Class,and bundled the jar and have used in android project. The application failed and threw an exception as the dependency slf4j,logback classic and logback core are missing in android,i tried to add them in android build.gradle file..it still didnt help....

Question:

I have implemented all the functional code and wasnt able to achieve logging.Is there any way the logging can be achieved

Note:I knew in android we use Android.util.Log...but that is in android project..but my jar is external jar developed in java which doesnt have access to Android.util.Log..Hope the question is clear..

Is there any way to achieve the logging ...Or should i consider building an android library instead of plain jar and plug it in android project..If so what is the process? Sorry if the framing of question is not clear

Upvotes: 0

Views: 1377

Answers (3)

Mike
Mike

Reputation: 3124

For me, adding the following Gradle dependencies in the app's build.gradle dependencies section worked:

compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.21'
compile group: 'org.slf4j', name: 'slf4j-android', version: '1.7.21'

Upvotes: 2

FriendlyMikhail
FriendlyMikhail

Reputation: 2925

In my work's large project we use SLF4j which works well for both the name android project as well as in Java library projects. SFL4J actually made some optimizations in a recent release to work correctly. you can even include a NOOP version for release which will strip all log statements. Best of luck.

Dependencies to add: slf4jNoOp : "org.slf4j:slf4j-api:$versions.slf4j", //compile slf4jAndroid : "org.slf4j:slf4j-android:$versions.slf4j", //release

sample use: private static final Logger LOGGER = LoggerFactory.getLogger(NYTApplication.class); LOGGER.info("Info Log"); https://github.com/qos-ch/slf4j

Upvotes: 2

Gabe Sechan
Gabe Sechan

Reputation: 93708

If you really need to export logging functionality, the best way is to create a logging interface and let the user implement it and pass in an instance. That way they can hook it up to whatever logging system they want to use.

Upvotes: 0

Related Questions