[slf4j-dev] Consolidating the LoggerFactory / A better plugin mechanism

Boris Unckel boris.unckel.mlg at gmx.net
Thu Feb 15 22:42:13 CET 2007


Hi,

Eric Crahen wrote:
> ....
> I think that we can resolve the undesirable issues while still 
> retaining the behavior we have today. Here is my proposal:
>
> Create a single LoggerFactory and placed it into slf4j-api, and 
> deleted the LoggerFactory classes everywhere else they exist 
> (slf4j-simple, logback, etc). This new LoggerFactory leverages Sun's 
> ServiceProvider API which exists in all JDK's since 1.4 (maybe 1.3). 
> It looks like this:

do you have a link to the service provider API? I searched the SUN site 
but did not find anything but the usage of the Service Provider but
http://java.sun.com/j2se/1.4.2/docs/guide/jar/jar.html#Service%20Provider

I am wondering this nice feature is just accesible through sun. packages.

Regards
Boris


public class SampleServiceLoader {

    public static final String SERVICE_ID =
        "META-INF/services/org.slf4j.ILoggerFactory";
   
    /**
     *
     */
    public SampleServiceLoader() {
        super();
    }
   
    public Object obtainFactory() {
        ILoggerFactory ret = null;       
        ClassLoader cl = Thread.currentThread().getContextClassLoader();
        InputStream is = cl.getResourceAsStream(SERVICE_ID);
        String factoryname = null;
        if(is != null) {
            BufferedReader bufferedReader = null;
            String errmsg = "Error obtaining service provider with 
id["+SERVICE_ID+"]";
            try {
                bufferedReader = new BufferedReader(new 
InputStreamReader(is));
                factoryname = bufferedReader.readLine();
                Class factoryClass = cl.loadClass(factoryname);
                ret = factoryClass.newInstance();
            } catch (IOException e) {
                throw new RuntimeException(errmsg,e);
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(errmsg,e);
            } catch (InstantiationException e) {
                throw new RuntimeException(errmsg,e);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(errmsg,e);
            } finally {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    // NOP
                }
            }
        } else {
            throw new RuntimeException("Service Provider["+SERVICE_ID+"] 
not found.");
        }
        return ret;
    }

}




More information about the slf4j-dev mailing list