0

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

KarolDepka
  • 8,318
  • 10
  • 45
  • 58
svs teja
  • 957
  • 2
  • 22
  • 43

3 Answers3

2

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

FriendlyMikhail
  • 2,857
  • 23
  • 39
  • @MikeN..can u share any reference of the library..will only slf4j suffice or logback classic and logback core also should be bundled any example will be more useful..thanks – svs teja Mar 21 '16 at 17:48
  • The problem with this is if you use 6 libraries each of which use their own favorite logging framework you have a mess. Libraries shouldn't force a dependence like that if it isn't a core requirement, instead they should leave the implementation up to the user. – Gabe Sechan Mar 21 '16 at 17:48
  • @svsteja see edits to comment. Let me know if any more help is needed. Only slf4j is needed – FriendlyMikhail Mar 21 '16 at 17:52
  • @MikeN...Thank you..will try out and let u know if any problem persists..cheers – svs teja Mar 21 '16 at 17:54
  • @MikeN..One more quick question is ..Is this correct way of addng am new to gradle as well so is there scope as release in gradle?compile 'org.slf4j:slf4j-api:1.7.14' release 'org.slf4j:slf4j-android:1.7.19'..and both these should be added in the place where java code(test class is der or in android application build.gradle) becuase unless i build fatJar dependencies wont be included – svs teja Mar 21 '16 at 18:05
  • you should be adding as debugCompile slf4jAndroid releaseCompile slf4jNoOp. This way only real logging goes into debug artifact but noop into release artifact. – FriendlyMikhail Mar 21 '16 at 18:09
2

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'
Mike
  • 3,094
  • 2
  • 12
  • 5
0

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.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • ..This is a good approach.But Is there any other approach because it shouldnt be the case that the jar is dependent on the android application implementation.. – svs teja Mar 21 '16 at 17:52