Hi all. This is a patch to make turbine 2 work with log4j configuration
syntax.

if you want to use log4j in you T2 app all you have to do is put a line
like this in your TR.conf

...
services.LoggingService.log4j = myconf.conf
...

Where myconf.conf *must* be available in your app classpath.

OBS : any other configuration for LoggingService is discarded !

If you are suing lg4j already you can point myconf.conf to the file you
have already created !
If you want to use the "old" logging system just write 

...
services.LoggingService.log4j = 
...
and log4j will not be enabled and all loggingService configuration lines
will take effect.

-- 
Leandro Rodrigo Saad Cruz
IT - Inter Business Tecnologia e Servicos (IB)
http://www.ibnetwork.com.br
Index: src/java/org/apache/turbine/services/logging/TurbineLoggingService.java
===================================================================
RCS file: /home/cvspublic/jakarta-turbine-2/src/java/org/apache/turbine/services/logging/TurbineLoggingService.java,v
retrieving revision 1.3
diff -u -b -B -r1.3 TurbineLoggingService.java
--- src/java/org/apache/turbine/services/logging/TurbineLoggingService.java	5 Apr 2002 07:04:55 -0000	1.3
+++ src/java/org/apache/turbine/services/logging/TurbineLoggingService.java	12 Jun 2002 18:51:54 -0000
@@ -57,6 +57,7 @@
 import java.util.Enumeration;
 import java.util.Hashtable;
 import java.util.Vector;
+import java.net.URL;
 import javax.servlet.ServletConfig;
 import javax.servlet.ServletContext;
 import org.apache.turbine.services.InitializationException;
@@ -67,6 +68,10 @@
 import org.apache.turbine.util.RunData;
 import org.apache.turbine.Turbine;
 
+//LOG4j
+import org.apache.log4j.PropertyConfigurator;
+import org.apache.log4j.Category;
+
 /**
  * The default implementation of the logging service in Turbine.
  *
@@ -107,6 +112,11 @@
         of the properties file for logging. */
     private String loggingConfigClassName = null;
 
+    /** Flag indicating that log4j is active
+        all facilities configured in TR.props are left out */
+
+    private boolean log4jEnabled = false;
+
     public TurbineLoggingService()
     {
         loggersTable = new Hashtable();
@@ -227,6 +237,30 @@
                 "default logger name in the configuration file.");
         }
 
+        /*
+         * Leandro
+         * give log4j a chance
+         */
+        if(!isLog4jConfigured())
+        {
+            String log4jConfigFile = props.getString(LoggingConfig.LOG4J);
+            System.out.println("Log4jConfigFile : "+log4jConfigFile);
+            if(log4jConfigFile != null)
+            {
+                // use log4j to configure all loggers
+                URL url = Thread.currentThread().getContextClassLoader().getResource(log4jConfigFile);
+                if(url != null)
+                {
+                    System.out.println("LoggingService using log4j");
+                    log4jEnabled = true;
+                    PropertyConfigurator.configure(url.getFile());
+                    return;
+                }
+                System.out.println("LoggingService using Turbine Loggers");
+            }
+        }
+
+
         // Get the list of facilities to configure
         Vector facilities = props.getVector(LoggingConfig.FACILITIES);
         for (Enumeration f = facilities.elements(); f.hasMoreElements();)
@@ -402,6 +436,11 @@
      */
     public void debug(String message)
     {
+        if(log4jEnabled)
+        {
+            Category.getRoot().debug(message);
+            return;
+        }
         defaultLogger.debug(message);
     }
 
@@ -411,6 +450,11 @@
      */
     public void debug(String message, Throwable t)
     {
+        if(log4jEnabled)
+        {
+            Category.getRoot().debug(message,t);
+            return;
+        }
         defaultLogger.debug(message, t);
     }
 
@@ -420,6 +464,11 @@
      */
     public void debug(String logName, String message, Throwable t)
     {
+        if(log4jEnabled)
+        {
+            Category.getInstance(logName).debug(message,t);
+            return;
+        }
         Logger logger = (Logger)loggersTable.get(logName);
         if (logger != null)
         {
@@ -437,6 +486,11 @@
      */
     public void debug(String logName, String message)
     {
+        if(log4jEnabled)
+        {
+            Category.getInstance(logName).debug(message);
+            return;
+        }
         Logger logger = (Logger)loggersTable.get(logName);
         if (logger != null)
         {
@@ -454,6 +508,10 @@
      */
     public void debug(String message, RunData data)
     {
+        if(log4jEnabled)
+        {
+            throw new UnsupportedOperationException("Log4j can't extract info from RunData");
+        }
         defaultLogger.debug(message, data);
     }
 
@@ -463,6 +521,10 @@
      */
     public void debug(String message, RunData data, Throwable t)
     {
+        if(log4jEnabled)
+        {
+            throw new UnsupportedOperationException("Log4j can't extract info from RunData");
+        }
         defaultLogger.debug(message, data, t);
     }
 
@@ -472,6 +534,10 @@
      */
     public void debug(String logName, String message, RunData data, Throwable t)
     {
+        if(log4jEnabled)
+        {
+            throw new UnsupportedOperationException("Log4j can't extract info from RunData");
+        }
         Logger logger = (Logger)loggersTable.get(logName);
         if (logger != null)
         {
@@ -489,6 +555,10 @@
      */
     public void debug(String logName, String message, RunData data)
     {
+        if(log4jEnabled)
+        {
+            throw new UnsupportedOperationException("Log4j can't extract info from RunData");
+        }
         Logger logger = (Logger)loggersTable.get(logName);
         if (logger != null)
         {
@@ -506,6 +576,11 @@
      */
     public void info(String message)
     {
+        if(log4jEnabled)
+        {
+            Category.getRoot().info(message);
+            return;
+        }
         defaultLogger.info(message);
     }
 
@@ -515,6 +590,11 @@
      */
     public void info(String message, Throwable t)
     {
+        if(log4jEnabled)
+        {
+            Category.getRoot().info(message,t);
+            return;
+        }
         defaultLogger.info(message, t);
     }
 
@@ -524,6 +604,10 @@
      */
     public void info(String logName, String message)
     {
+        if(log4jEnabled)
+        {
+            Category.getInstance(logName).info(message);
+        }
         Logger logger = (Logger)loggersTable.get(logName);
         if (logger != null)
         {
@@ -541,6 +625,11 @@
      */
     public void info(String logName, String message, Throwable t)
     {
+        if(log4jEnabled)
+        {
+            Category.getInstance(logName).info(message,t);
+            return;
+        }
         Logger logger = (Logger)loggersTable.get(logName);
         if (logger != null)
         {
@@ -558,6 +647,10 @@
      */
     public void info(String message, RunData data)
     {
+        if(log4jEnabled)
+        {
+            throw new UnsupportedOperationException("Log4j can't extract info from RunData");
+        }
         defaultLogger.info(message, data);
     }
 
@@ -567,6 +660,10 @@
      */
     public void info(String message, RunData data, Throwable t)
     {
+        if(log4jEnabled)
+        {
+            throw new UnsupportedOperationException("Log4j can't extract info from RunData");
+        }
         defaultLogger.info(message, data, t);
     }
 
@@ -576,6 +673,10 @@
      */
     public void info(String logName, String message, RunData data)
     {
+        if(log4jEnabled)
+        {
+            throw new UnsupportedOperationException("Log4j can't extract info from RunData");
+        }
         Logger logger = (Logger)loggersTable.get(logName);
         if (logger != null)
         {
@@ -593,6 +694,10 @@
      */
     public void info(String logName, String message, RunData data, Throwable t)
     {
+        if(log4jEnabled)
+        {
+            throw new UnsupportedOperationException("Log4j can't extract info from RunData");
+        }
         Logger logger = (Logger)loggersTable.get(logName);
         if (logger != null)
         {
@@ -610,6 +715,11 @@
      */
     public void warn(String message)
     {
+        if(log4jEnabled)
+        {
+            Category.getRoot().warn(message);
+            return;
+        }
         defaultLogger.warn(message);
     }
 
@@ -619,6 +729,11 @@
      */
     public void warn(String message, Throwable t)
     {
+        if(log4jEnabled)
+        {
+            Category.getRoot().warn(message,t);
+            return;
+        }
         defaultLogger.warn(message, t);
     }
 
@@ -628,6 +743,11 @@
      */
     public void warn(String logName, String message)
     {
+        if(log4jEnabled)
+        {
+            Category.getInstance(logName).warn(message);
+            return;
+        }
         Logger logger = (Logger)loggersTable.get(logName);
         if (logger != null)
         {
@@ -645,6 +765,11 @@
      */
     public void warn(String logName, String message, Throwable t)
     {
+        if(log4jEnabled)
+        {
+            Category.getInstance(logName).warn(message,t);
+            return;
+        }
         Logger logger = (Logger)loggersTable.get(logName);
         if (logger != null)
         {
@@ -662,6 +787,10 @@
      */
     public void warn(String message, RunData data)
     {
+        if(log4jEnabled)
+        {
+            throw new UnsupportedOperationException("Log4j can't extract info from RunData");
+        }
         defaultLogger.warn(message, data);
     }
 
@@ -671,6 +800,10 @@
      */
     public void warn(String message, RunData data, Throwable t)
     {
+        if(log4jEnabled)
+        {
+            throw new UnsupportedOperationException("Log4j can't extract info from RunData");
+        }
         defaultLogger.warn(message, data, t);
     }
 
@@ -680,6 +813,10 @@
      */
     public void warn(String logName, String message, RunData data)
     {
+        if(log4jEnabled)
+        {
+            throw new UnsupportedOperationException("Log4j can't extract info from RunData");
+        }
         Logger logger = (Logger)loggersTable.get(logName);
         if (logger != null)
         {
@@ -697,6 +834,10 @@
      */
     public void warn(String logName, String message, RunData data, Throwable t)
     {
+        if(log4jEnabled)
+        {
+            throw new UnsupportedOperationException("Log4j can't extract info from RunData");
+        }
         Logger logger = (Logger)loggersTable.get(logName);
         if (logger != null)
         {
@@ -714,6 +855,11 @@
      */
     public void error(String message)
     {
+        if(log4jEnabled)
+        {
+            Category.getRoot().error(message);
+            return;
+        }
         defaultLogger.error(message);
     }
 
@@ -723,6 +869,11 @@
      */
     public void error(String message, Throwable t)
     {
+        if(log4jEnabled)
+        {
+            Category.getRoot().error(message,t);
+            return;
+        }
         defaultLogger.error(message, t);
     }
 
@@ -732,6 +883,11 @@
      */
     public void error(String logName, String message)
     {
+        if(log4jEnabled)
+        {
+            Category.getInstance(logName).error(message);
+            return;
+        }
         Logger logger = (Logger)loggersTable.get(logName);
         if (logger != null)
         {
@@ -749,6 +905,11 @@
      */
     public void error(String logName, String message, Throwable t)
     {
+        if(log4jEnabled)
+        {
+            Category.getInstance(logName).error(message,t);
+            return;
+        }
         Logger logger = (Logger)loggersTable.get(logName);
         if (logger != null)
         {
@@ -766,6 +927,10 @@
      */
     public void error(String message, RunData data)
     {
+        if(log4jEnabled)
+        {
+            throw new UnsupportedOperationException("Log4j can't extract info from RunData");
+        }
         defaultLogger.error(message, data);
     }
 
@@ -775,6 +940,10 @@
      */
     public void error(String message, RunData data, Throwable t)
     {
+        if(log4jEnabled)
+        {
+            throw new UnsupportedOperationException("Log4j can't extract info from RunData");
+        }
         defaultLogger.error(message, data, t);
     }
 
@@ -784,6 +953,10 @@
      */
     public void error(String logName, String message, RunData data)
     {
+        if(log4jEnabled)
+        {
+            throw new UnsupportedOperationException("Log4j can't extract info from RunData");
+        }
         Logger logger = (Logger)loggersTable.get(logName);
         if (logger != null)
         {
@@ -801,6 +974,10 @@
      */
     public void error(String logName, String message, RunData data, Throwable t)
     {
+        if(log4jEnabled)
+        {
+            throw new UnsupportedOperationException("Log4j can't extract info from RunData");
+        }
         Logger logger = (Logger)loggersTable.get(logName);
         if (logger != null)
         {
@@ -811,4 +988,28 @@
             defaultLogger.error("FROM logger:" + logName + ": " + message);
         }
     }
+
+    /** see http://archive.covalent.net/jakarta/log4j-user/2001/07/0004.xml */
+    private boolean isLog4jConfigured() 
+    {
+    
+        Enumeration enum = Category.getRoot().getAllAppenders();
+    
+        if(!(enum instanceof org.apache.log4j.helpers.NullEnumeration))
+        {
+            return true;
+        } 
+        else 
+        { 
+            Enumeration cats =  Category.getCurrentCategories();
+            while(cats.hasMoreElements()) 
+            {
+                Category c = (Category) cats.nextElement();
+                if(!(c.getAllAppenders() instanceof org.apache.log4j.helpers.NullEnumeration))
+                    return true;
+            }
+        }
+        return false;
+    } 
+
 }
Index: src/java/org/apache/turbine/services/logging/LoggingConfig.java
===================================================================
RCS file: /home/cvspublic/jakarta-turbine-2/src/java/org/apache/turbine/services/logging/LoggingConfig.java,v
retrieving revision 1.2
diff -u -b -B -r1.2 LoggingConfig.java
--- src/java/org/apache/turbine/services/logging/LoggingConfig.java	5 Apr 2002 07:04:55 -0000	1.2
+++ src/java/org/apache/turbine/services/logging/LoggingConfig.java	12 Jun 2002 18:52:12 -0000
@@ -74,6 +74,7 @@
     public final static String DESTINATION = "destination";
     public final static String CLASSNAME = "className";
     public final static String LEVEL = "level";
+    public final static String LOG4J = "log4j";
 
     /** default rollover file size */
     public static final long DEFAULT_FILE_SIZE = 80000;

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

Reply via email to