Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No Commons Logging in Android?

I have a pretty big library I developed specifically for use in my Android Application. However business logic itself has no dependency on Android. To preserve that, I used Commons Logging throughout this library and it's respective JUnit tests (which I run in Eclipse).

However now that I am starting to integrate it into an Activity which I launch on Android, I am unable to get my logging to work. In Eclipse/JUnit, I had simply pulled in log4j's jar file as well as a log4j.properties file. This doesn't seem to work when deploying to a device. After struggling with attempting to get that to work for several hours, I gave up and tried replacing all my commons logging stuff with android.util.Log. Now I can log on the device.. but all JUnit tests are broken. When any JUnit tries to log using android.util.Log, it throws a RuntimeException 'Stub!'.

I would prefer to revert to my commons logging approach.. if anyone can help with that.. otherwise.. what can I do to get my JUnit test cases running using 'android.util.Log'?

Many thanks in advance.. I've spent more than a few hours on this and I'd like to move on to writing real code again!

like image 633
Joe Boese Avatar asked Jun 17 '10 04:06

Joe Boese


4 Answers

As Qberticus mentions, commons-logging will work within Android. I just confirmed this by dropping the commons-logging library into an Android project and logging messages at the INFO level. Commons-logging will default to the Jdk14 logging framework, which will then pass through to the Android Logging subsystem, with results viewable in logcat. The tag is set to the class whose log is used: for example, using the log from LogFactory.getLog(Foo.class) will have the tag "Foo".

like image 139
Paul Avatar answered Nov 17 '22 22:11

Paul


I don't believe there is a log4j port for android atm. However, android does have java.util.logging built in and commons logging can connect to that via the Jdk14Logger. If commons logging works on android then you'll probably be good to go.

like image 27
Rich Schuler Avatar answered Nov 17 '22 21:11

Rich Schuler


You could build an Interface for your own logger that depending on the platform instantiates a delegate to the logging that is common on the platform. As long as you are working and debugging this should be enough, later in production code on the device you can simply insert a logger that does nothing and you save all the time you need to clutter the phonelog.

like image 1
Janusz Avatar answered Nov 17 '22 21:11

Janusz


Paul's solution did not work for me. It resulted in a conflict at compile time because, as it happens, the commons logging jar is already included in the SDK. My trouble was I couldn't compile against it.

Adding:

extensible.classpath=${sdk.dir}/tools/lib/commons-logging-1.1.1.jar

to my ant.properties did the trick.

like image 1
Steve Avatar answered Nov 17 '22 21:11

Steve