It's a good thing to have a pluginnable log system, to be able to redirect logs to different locations or to change the logging library if needed, without having to manually modify each occurrence.
There are various ways to accomplish this. One easy and simple way is what I've posted here. If you need more sophisticated things, maybe you should look somewhere else. I use the following classes in all of my open source libraries here on GitHub, so I thought they may be useful also for other projects.
Just copy and paste the two classes in this Gist in your project, and then use Logger
to log your messages. For example:
Logger.debug("MyClass", "MyMessage");
You can set the log level (by default is DEBUG
for debug builds and OFF
for release builds):
Logger.setLogLevel(LogLevel.DEBUG);
And you can easily provide your custom logger implementation:
Logger.setLoggerDelegate(new Logger.LoggerDelegate() {
@Override
public void error(String tag, String message) {
//your own implementation here
}
@Override
public void error(String tag, String message, Throwable exception) {
//your own implementation here
}
@Override
public void debug(String tag, String message) {
//your own implementation here
}
@Override
public void info(String tag, String message) {
//your own implementation here
}
});
And that's all, folks :)