Well, technically SLF4J doesn't offer you a logger.log(Level, message) method. But I found a way around that. [edit: uses introspection]
Using the below code snippet you can get the native logger that slf4j found and wrapped for you at runtime. If you'll recall, slf4j is simply a wrapper around an slf4j implementation from another provider (either, jdkLogging, Log4J, JCL, etc...). So here:
public Object getNativeLogger( org.slf4j.Logger logger ) {
Object result = null;
if ( logger.getClass().getName().equals("org.slf4j.impl.Log4jLoggerAdapter")) {
try {
Field f = Log4jLoggerAdapter.class.getDeclaredField("logger");
f.setAccessible( true );
result = (org.apache.log4j.Logger)f.get(logger);
}
catch( Exception e ) {
System.out.println("Unable to access native log4j logger");
}
}
else if ( logger.getClass().getName().equals("org.slf4j.impl.JDK14LoggerAdapter")) {
try {
Field f = Jdk14Logger.class.getDeclaredField("logger");
f.setAccessible( true );
result = (Jdk14Logger)f.get(logger);
}
catch( Exception e ) {
System.out.println("Unable to access native log4j logger");
}
}
else if (..... other native loggers slf4j supports)....
}
return result;
}
Then you can use it like this:
Object l = getNativeLogger(mySlf4jLogger);
if ( l instanceof org.apache.log4j.Logger ) {
org.apache.log4j.Logger logger = (org.apache.log4j.Logger) l;
logger.log( CUSTOMLog4JLevel, message);
}
else if( .... other implementations that you care about ...)...
So while it's not technically within slf4j, it is possible to do it using slf4j as your primary logging interface.