Java Logging Wrapper with Static Binding – SLF4J

September 16th, 2008 thiamteck Posted in API, Java 2 Comments »

When think of logging wrapper for Java, I believe Apache Common Logging come in to most developers mind. Even if you don’t use it, the API/framework you use are very likely depend on it.

It able to auto discover your actual implementation, with the cost of complexity. The class loader and reflection based dynamic binding might behave unexpectedly in different application servers.

Why spend long time to understand a logging wrapper since it is never the main concern of your application?

Just switch to SLF4J – “Simple Logging Facade for Java”. As its name imply, it try to be simple instead of act smart with those class loading hack. It support for log4j, common-logging, Java util logging, Logback, etc.

If you code with SLF4J, your code will typically look like this:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Toolbox{
	Logger logger = LoggerFactory.getLogger(this.getClass().getName());

	public helloWorld(){
		logger.info("Hello World");
	}
}

All you need to do is place the SLF4J API jar file (slf4j-api-1.5.2.jar), your logging implementation jar file (said log4j.jar), and SLF4J adapter for the logging platform of your choice (slf4j-log4j12-1.5.2.jar) into your classpath. And It is done.

If you would like to change to Java util logging, just delete the ‘slf4j-log4j12-1.5.2.jar’, and place the adapter for Java util logging (‘slf4j-jdk14-1.5.2.jar’) into your classpath. And it will work perfectly without recompile. The only things to watch out is: there should always be only one adapter in classpath.

Besides adapter for different logging API, SLF4J also come with a set of bridge for various logging API. Said you have use an API that depend Apache Commons Logging, your application is code with log4j. And you would like to use Java Util logging as actual logging implementation.

The steps are as simple as 123:
1) Delete the common-logging and log4j jar file from classpath, place the bridging jar file (jcl-over-slf4j-1.5.2.jar, log4j-over-slf4j-1.5.2.jar) into classpath
2) Place SJF4J API (slf4j-api-1.5.2.jar) into classpath
3) Place adapter for Java util logging (‘slf4j-jdk14-1.5.2.jar’) into classpath

All call to common-logging and log4j will be intercepted by SLF4J and pipe to Java util logging. And all is done with just static binding.

For those who curious on how it can be done, I will suggest you to open the bridging and adapter jar file to have a look. It is really a good real world example of mock classes and adapter pattern.

AddThis Social Bookmark Button

Winp – Windows Process Libary

August 3rd, 2008 thiamteck Posted in API, Java No Comments »

It is not an easy task to manipulate with Windows process from Java program.

The standard JDK allowed create native process, and destroy the native process that created by same Java program. (via Process, ProcessBuilder, and Runtime)

And thanks to Winp from Kohsuke Kawaguchi, now you can do more on Windows processes.

With this tiny API (12k in size), you may kill a Windows process that not created by your Java program. And you can get the command line argument of the process (for example, not just a “java.exe” you get in task manager, but “java -server HelloWorld arg1”).

However, this API have 2 limitations:
1) It is for Windows only
2) It is for 32bits Windows only

This API is published under BSD license.

AddThis Social Bookmark Button