How to Use SLF4J with log4j
1) Create the Slf4jSample.java file:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public final class Slf4jSample {
public static void main(final String[] args) {
final Logger logger = LoggerFactory.getLogger(Slf4jSample.class);
logger.info("Hello World!");
}
private Slf4jSample() {
}
}
2) Create the log4j.properties file:
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.logger.Slf4jSample=INFO, A1
3) Compile the Slf4jSample.java file.
4) Add the slf4j-log4j.jar file to the class path.
5) Launch the Slf4jSample class.


Hi,
I have a set of libraries that uses log4j with commons logging. I have one jar that uses slf4j logging. So when I add slf4j-api-1.4.3.jar and slf4j-simple-1.4.3.jar to my application it overrides settings in my log4j.properties file. This causes a whole lot of log messages to be printed into the console.
I am thankful for any help.
-Dimuthu
Dimuthu
January 28, 2008 at 6:19 am
How do you make this work if you have existing code that has references to org.apache.log4j.Logger? I really don’t want to change all of my classes so that they import org.slf4j.Logger and org.slf4j.LoggerFactory.
How do I configure log4j if this overrides it? I want the applications logging statements to be written to a file called myapp.log. As for the Jetty container’s logging, it can go to /dev/null for all I care, I just want to have my application-specific logging back.
How do I get both implementations to exist side by side? I’m using Jetty 6 which is dependent on slf4j, but my application that I’m serving in Jetty 6 uses pure log4j, at least it did before I had to deploy slf4 jar files to my jetty/lib directory.
Any help you can provide will be greatly appreciated.
Thanks for the help,
James
James
May 13, 2008 at 9:02 pm
2James:
If you want to use log4j as implementation for slf4j, you may configure log4j as you want, all slf4j logs will use this configuration.
If you want log all to slf4j and don’t want edit your existing code, you may use log4j-over-slf4j module, as described at http://www.slf4j.org/log4j-over-slf4j.html
Alno
May 26, 2008 at 1:09 pm