A logger for Android and Java in general… here is a solution to the problem.
A logger for Android and Java in general
I
have a generic module developed in normal Java (PC), but I also want to use it in Android modules.
How do I log a message in the PC module and see it in the Android logs as well?
Solution
You can use the SLF4J API, which is the lightweight look and feel of the logging framework.
I think this is the best way to develop libraries for Java.
You can read more about the differences between SLF4J and log4j here
For example, you can use the following code in the public module:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Bar {
private static final Logger logger = LoggerFactory.getLogger(Bar.class);
public void foo(int value) {
logger.info("entered Bar::foo value={}", value);
}
}
After that, you can use this code with one of the SLF4J implementations:
- For non-Android modules:
- You can use LOGBack
- Or one of the adapters: Log4jLoggerAdapter, JDK14LoggerAdapter, SimpleLogger, etc.
- For Android modules:
- One of the simplest implementations Android Logger
- If you need some sophisticated logging and your application size is unlimited, logback-android is your choice.