preparing for a new logging framework
authorbruno <bruno>
Wed, 9 Oct 2002 16:25:17 +0000 (16:25 +0000)
committerbruno <bruno>
Wed, 9 Oct 2002 16:25:17 +0000 (16:25 +0000)
source/mir/log/Log.java [new file with mode: 0755]
source/mir/log/Logger.java [new file with mode: 0755]
source/mir/log/TestFramework.java [new file with mode: 0755]
source/mir/log/log4j/LoggerImpl.java [new file with mode: 0755]

diff --git a/source/mir/log/Log.java b/source/mir/log/Log.java
new file mode 100755 (executable)
index 0000000..34c943e
--- /dev/null
@@ -0,0 +1,47 @@
+package mir.log;
+
+import mir.misc.MirConfig;
+
+
+public class Log {
+
+    private static Logger myLogger;
+
+    static {
+        try {
+            myLogger = (Logger)Class.forName(MirConfig.getProp("Log.LogClass")).newInstance();
+        }
+        catch (java.lang.ClassNotFoundException cnfe) {
+            System.err.println("Log was not able to initialize: class not found");
+            cnfe.printStackTrace(System.err);
+        }
+        catch (java.lang.InstantiationException ie) {
+            System.err.println("Log was not able to initialize: could not initialize class");
+            ie.printStackTrace(System.err);
+        }
+        catch (java.lang.IllegalAccessException iae) {
+            System.err.println("Log was not able to initialize: illegal access");
+            iae.printStackTrace(System.err);
+        }
+    }
+
+    public static void debug( Object o, String s) {
+        myLogger.debug( o, s );
+    }
+
+    public static void info( Object o, String s) {
+        myLogger.info( o, s );
+    }
+
+    public static void warn( Object o, String s) {
+        myLogger.warn( o, s );
+    }
+
+    public static void error( Object o, String s) {
+        myLogger.error( o, s );
+    }
+
+    public static void fatal( Object o, String s) {
+        myLogger.fatal( o, s );
+    }
+}
diff --git a/source/mir/log/Logger.java b/source/mir/log/Logger.java
new file mode 100755 (executable)
index 0000000..8468fde
--- /dev/null
@@ -0,0 +1,10 @@
+package mir.log;
+
+
+public interface Logger {
+    public void debug( Object o, String s);
+    public void info( Object o, String s);
+    public void warn( Object o, String s);
+    public void error( Object o, String s);
+    public void fatal( Object o, String s);
+}
diff --git a/source/mir/log/TestFramework.java b/source/mir/log/TestFramework.java
new file mode 100755 (executable)
index 0000000..dc4d7bc
--- /dev/null
@@ -0,0 +1,15 @@
+package mir.log;
+
+import mir.log.Log;
+
+
+public class TestFramework {
+
+    public static void main(String[] args) {
+        TestFramework t = new TestFramework();
+
+        Log.info( TestFramework.class, "class");
+        Log.info( t, "object" );
+        Log.info( null, "lalala" );
+    }
+}
diff --git a/source/mir/log/log4j/LoggerImpl.java b/source/mir/log/log4j/LoggerImpl.java
new file mode 100755 (executable)
index 0000000..298a6d9
--- /dev/null
@@ -0,0 +1,57 @@
+package mir.log.log4j;
+
+import org.apache.log4j.PropertyConfigurator;
+import org.apache.log4j.Logger;
+
+import java.util.Map;
+import java.util.HashMap;
+
+
+public class LoggerImpl implements mir.log.Logger {
+
+    private static Map loggers = new HashMap();
+
+    public LoggerImpl() {
+        PropertyConfigurator.configure("log4j.properties");
+    }
+
+
+    public void debug( Object o, String s ) {
+        this.getLogger(o).debug(s);
+    }
+
+    public void info( Object o, String s ) {
+        this.getLogger(o).info(s);
+    }
+
+    public void warn( Object o, String s ) {
+        this.getLogger(o).warn(s);
+    }
+
+    public void error( Object o, String s ) {
+        this.getLogger(o).error(s);
+    }
+
+    public void fatal( Object o, String s ) {
+        this.getLogger(o).fatal(s);
+    }
+
+
+    private Logger getLogger( Object o ) {
+        String name;
+        if (o instanceof Class) {
+            name = ((Class)o).getName();
+        } else if (o!=null) {
+            name = o.getClass().getName();
+        } else {
+            name = "generic";
+        }
+
+        Logger l = (Logger)loggers.get(name);
+        if (l==null) {
+            l = Logger.getLogger(name);
+            loggers.put(name, l);
+        }
+        return l;
+    }
+}
\ No newline at end of file