>So how about we add a simple, lightweight facade component for logging to
>jakarta-commons?

I recently had written a small class which does log to System.out.
It also support redirecting to an arbitrary PrintStream.

Naming etc. does not adhere to the usual Sun standard as this class
was derived from a C-function used elsewhere.

It supports optional tracelevel on a per package and/or class-level.

If anyone finds it interesting I'll hereby donate it to the public.

Best,
Michael

--- snip --- snip --- snip --- snip --- snip --- snip --- snip ---
package foo.bar.log;

import java.io.PrintStream;
import java.util.Hashtable;

public class Trace
{
    public static final int INFO        = 0;
    public static final int WARNING     = 4;
    public static final int ERROR       = 8;
    public static final int FATAL       = 12;

    private static Hashtable _hashTraceLvl = new Hashtable();

    private static int _nTraceLvl = 0;
    public synchronized static int getTraceLvl() {return getTraceLvl(null);}
    public synchronized static void setTraceLvl(int nTraceLvl) {setTraceLvl(nTraceLvl, 
null);}
    public synchronized static int getTraceLvl(Object o) {
        if (null==o)
            return _nTraceLvl;
        Integer i = (Integer)_hashTraceLvl.get(o.getClass().getName());
        if (null==i && null!=o.getClass().getPackage())
            i = (Integer)_hashTraceLvl.get(o.getClass().getPackage());
        if (null==i)
            return _nTraceLvl;
        else
            return i.intValue();
    }
    public synchronized static void setTraceLvl(int nTraceLvl, Object o) {
        if (null==o)
            _nTraceLvl = nTraceLvl;
        else
            _hashTraceLvl.put(o.getClass().getName(), new Integer(nTraceLvl));
    }
    public synchronized static void setPackageTraceLvl(int nTraceLvl, Object o){
        if (null==o)
            _nTraceLvl = nTraceLvl;
        else
            _hashTraceLvl.put(o.getClass().getPackage(), new Integer(nTraceLvl));
    }

    Trace() {
    }

    public static void out(String msg) {
        out(INFO, msg, null);   
    }

    public static void out(String msg, Object o) {
        out(INFO, msg, o);
    }

    public static void out(String msg, int nSeverity) {
        out(nSeverity, msg, null);
    }

    public static void out(int nSeverity, String msg, Object o) {
        if (getTraceLvl(o)<=nSeverity)
            System.out.println(Integer.toHexString(nSeverity)+" "+(new 
java.util.Date(System.currentTimeMillis()))+"
"+(null==o?"<null>":o.getClass().getName())+" - "+msg);
    }

    public static void setOut(PrintStream newOut) {
        System.setOut(newOut);
    }

}   // end class Trace
--- snip --- snip --- snip --- snip --- snip --- snip --- snip ---
--
 Vote against SPAM - see http://www.politik-digital.de/spam/
 Michael Gerdau       email: [EMAIL PROTECTED]
 Windows isn't a virus.  A virus does something.
 PGP-keys available on request or at public keyserver


Reply via email to