> Jon Stevens <[EMAIL PROTECTED]> writes:
>
> > on 6/6/01 9:23 AM, "David S. Faller" <[EMAIL PROTECTED]> wrote:
> >
> > > What about something like:
> > > if (Log.getLogLevel() > Log.LEVEL_DEBUG) {
> > >   Log.debug("myMethod() entered. param1="+param1+";param2="+param2);
> > > }
> >
> > Actually, that still doesn't work 100% well...here is some test
> > code...compile it and then run javap -c Test and you will see that using
a
> > method like that does not compile out the code as you would hope it
does...

If you want the compiler to optimize the code, it should look like this:

public class Test
{
    public static final boolean debug = false;

    public static void debug(String log_me) {
     if (debug) {
         System.out.println(log_me);
     }
    }

    public static void main(String args[]) {
     if (debug) debug("strings");
     debug("still");
     debug("in");
     debug("class");
     debug("file");
    }

}

The debug("strings") call is not compiled, since it is never used (public
static _final_ will never change)...

>
> Regardless of whether the log level thing compiles code out (which it
> does not, as you say), it does save numerous method calls to
> StringBuffer's append() method, and one call to its toString() method
> for a standard String concatenation operation.
>
> Dan

I would prefer a 'dynamic' method,too. Otherwise it would be necessary to
recompile your code, if you want to see the debug messages.

What about this?


--- Logger.java 2001/04/12 02:22:58 1.9
+++ Logger.java 2001/06/07 12:15:03
@@ -151,6 +151,26 @@
    public void setLogLevel(int level);

    /**
+     * Checks if DEBUG statements are enabled.
+     */
+     public boolean isDebugEnabled();
+
+    /**
+     * Checks if INFO statements are enabled.
+     */
+     public boolean isInfoEnabled();
+
+    /**
+     * Checks if WARN statements are enabled.
+     */
+     public boolean isWarnEnabled();
+
+    /**
+     * Checks if ERROR statements are enabled.
+     */
+     public boolean isErrorEnabled();
+
+    /**
     * This method should be implemented by user.
     * It performs action that are need for deterimne whether
     * logger was well configured or has any output



--- BaseLogger.java 2001/04/06 18:41:51 1.9
+++ BaseLogger.java 2001/06/07 12:27:11
@@ -222,6 +222,38 @@
}
    /**
+     * Checks if DEBUG statements are enabled.
+     */
+     public boolean isDebugEnabled()
+     {
+        return (logLevel == DEBUG);
+    }
+
+    /**
+     * Checks if INFO statements are enabled.
+     */
+    public boolean isInfoEnabled()
+    {
+        return (logLevel <= INFO);
+    }
+
+    /**
+     * Checks if WARN statements are enabled.
+     */
+    public boolean isWarnEnabled()
+    {
+        return (logLevel <= WARN);
+    }
+
+    /**
+     * Checks if ERROR statements are enabled.
+     */
+    public boolean isErrorEnabled()
+    {
+        return (logLevel <= ERROR);
+    }
+
+    /**
    * This method should be implemented by user if the logger can handle
files.
    * It adds local file as destinations for logger.
    *

cya,
David


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to