OSGL Logging is yet another Java Logging facade.
Why not Slf4J
I don't like the Slf4J Logger's API. Specifically the logging API with Throwable arguments, e.g.
void debug(String msg, Throwable t);
The better one in my mind should be
void debug(Throwable t, String format, Object... args);
And yes OSGL logging Logger use the better one ;-)
- Classic usage
import org.osgl.logging.*;
public class Foo {
protected final Logger logger = LogManager.get(Foo.class);
private void bar(int n) {
logger.trace("enter bar with %s", n);
try {
// do something dangerous
} catch (Exception e) {
logger.error(e, "error executing bar with %s", n);
}
}
}
- Use
LogManager
's static log methods
import org.osgl.logging.*;
public class Foo {
private void bar(int n) {
LogManager.trace("enter bar with %s", n);
try {
// do something dangerous
} catch (Exception e) {
LogManager.error(e, "error executing bar with %s", n);
}
}
}
- Use
LogManager
's aliasL
and the static log methods
import org.osgl.logging.*;
public class Foo {
private void bar(int n) {
L.trace("enter bar with %s", n);
try {
// do something dangerous
} catch (Exception e) {
L.error(e, "error executing bar with %s", n);
}
}
}
The only configuration for OSGL logging is to configure the LogServiceProvider
:
import org.osgl.logging.*;
import org.osgl.logging.service.*;
public class Bootstrap {
static {
LogManager.registerLogServiceProvider(new CommonsLoggingServiceProvider());
}
}
At the moment there are four predefined LogServiceProvider
:
- CommonsLoggingServiceProvider, bridge to Apache Commons-Logger
- Log4jServiceProvider, bridge to Apache Log4J
- Slf4jServiceProvider, bridge to Slf4j
- TinyLogServiceProvider, bridge to TinyLog
Please refer to relevant logging framework documents to check how to configure specific log service.
If user application does not explicitly register log service provider to LogManager
, then LogManager
will automatically discover the underline LogService
by trying to check if a certain Java Class is in the class path. The discovery is proceeded with the following order:
- Check if TinyLog is in the class path. If found then use
TinyLogServiceProvider
- Else then check if Slf4j is in the class path. If found then use
Slf4jServiceProvider
- Else then check if Commons-Logging is in the class path. If found then use
CommonsLoggingServiceProvider
- Else check if Log4j is in the class path. If found then use
Log4jServiceProvider
- Finally if none of the service provider is used, then by default use
JDKLogService