CVS: plexus-container-new/src/java/org/apache/plexus/logging ConsoleLoggerManager.java,1.2,1.3 LoggerManagerFactory.java,1.4,1.5

[email protected] Sat, 31 May 2003 10:20:13 -0500
Newsgroups gmane.comp.java.plexus.devel
Message-ID <[email protected]>
Update of /cvsroot/plexus/plexus-container-new/src/java/org/apache/plexus/logging
In directory eng.werken.com:/tmp/cvs-serv21816/src/java/org/apache/plexus/logging

Modified Files:
	ConsoleLoggerManager.java LoggerManagerFactory.java 
Log Message:
The ConsoleLoggerManager can be configured with a logging threshold
level via the plexus.conf configuration file.  This enables a user to
control the level of logging messages displayed on the console.  For
example:

  <logging>
    <implementation>org.apache.plexus.logging.ConsoleLoggerManager</implementation>
    <logger>
      <threshold>DEBUG</threshold>
    </logger>
  </logging>

In order to accomodate the change, the LoggerManagerFactory, which calls
configure() on a specific LoggerManager, now passes only the <logging>
configuration block instead of the full plexus.conf configuration block.
This makes more sense because each implementation of a LoggerManager no
longer has to take the extra step of conf.getChild("logging").

On a side note, my primary goal is to enhance the logging system so each
component can specify its own logging level.  However, I have not gone
through all of the plexus code to start making these changes.  I'm
slowly making my way through.  My initial thoughts are:

- Add a <logging> block which could contain a logger-specific
  configuration.  This block would then used by the LoggerManager when
  creating the Logger for this component. 

For example:

<component>
  <role>org.apache.plexus.examples.simple.HelloWorld</role>
  <implementation>org.apache.plexus.examples.simple.DefaultHelloWorld</implementation>
  <logging>
    <priority>DEBUG</priority>
  </logging>
  <configuration>
    <greeting>Hello World!</greeting>
  </configuration>
</component>

The above <logging> block might be suitable for use with the
ConsoleLogger or Log4JLogger.  In another example, lets say I have a
SylogLogger, perhaps its logging configuration block could take a
ipaddress of where the messages should be logged.

<component>
  <role>org.apache.plexus.examples.simple.HelloWorld</role>
  <implementation>org.apache.plexus.examples.simple.DefaultHelloWorld</implementation>
  <logging>
    <priority>DEBUG</priority>
    <ipaddress>10.10.10.10</ipaddress>
  </logging>
  <configuration>
    <greeting>Hello World!</greeting>
  </configuration>
</component>

One last example, one could specify the Log4J appender to use (assuming
the appenders were defined up top in the logger manager configuration
block), here is a full plexus conf example:

<plexus>

  <logging>
    <logger>
      <appender-id>default</appender-id>
      <priority>INFO</priority>
    </logger>

    <appender>
      <id>default</id>
      <type>file</type>
      <type-configuration>
        <file>${plexus.home}/logs/plexus.log</file>
        <append>true</append>
      </type-configuration>
      <threshold>INFO</threshold>
      <layout>pattern-layout</layout>
      <conversion-pattern>%-4r [%t] %-5p %c %x - %m%n</conversion-pattern>
    </appender>
  </logging>

  <component>
    <role>org.apache.plexus.examples.simple.HelloWorld</role>
    <implementation>org.apache.plexus.examples.simple.DefaultHelloWorld</implementation>
    <logging>
      <priority>DEBUG</priority>
      <appender-id>default</appender-id>
    </logging>
    <configuration>
      <greeting>Hello World!</greeting>
    </configuration>
  </component>
 
</plexus>

Basically, in a nutshell, the <logging> block of the component
descriptor could be used in a LoggerManager-specific manner when
creating Loggers for components.  

Just some thoughts of what I am going to be working towards.  Does this
seem reasonable?




Index: ConsoleLoggerManager.java
===================================================================
RCS file: /cvsroot/plexus/plexus-container-new/src/java/org/apache/plexus/logging/ConsoleLoggerManager.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- ConsoleLoggerManager.java	1 May 2003 19:35:36 -0000	1.2
+++ ConsoleLoggerManager.java	31 May 2003 15:20:06 -0000	1.3
@@ -3,19 +3,48 @@
 import org.apache.avalon.framework.configuration.Configuration;
 import org.apache.avalon.framework.logger.Logger;
 
+/**
+ * Sample configuration.
+ * 
+ * <pre>
+ * <logging>
+ *   <implementation>org.apache.plexus.logging.ConsoleLoggerManager</implementation>
+ *   <logger>
+ *     <threshold>DEBUG</threshold>
+ *   </logger>
+ * </logging>
+ * </pre>
+ */
 public class ConsoleLoggerManager
     implements LoggerManager
 {
+    /** XML element used to start the logger configuration. */
+    private static final String LOGGER_TAG = "logger";
+
+    /** XML element used to set the threshold of the console logger. */
+    private static final String THRESHOLD_TAG = "threshold";
+
+    /** Message of this level or higher will be logged. */
+    private int thresholdLevel;
+    
+    /** The console logger used by the manager. */
     private ConsoleLogger consoleLogger;
 
     public void configure( Configuration configuration )
     {
+        setThresholdLevel(
+            configuration
+                .getChild( LOGGER_TAG )
+                .getChild( THRESHOLD_TAG )
+                .getValue( "info" )
+                .trim()
+                .toLowerCase() );
     }
 
     public void initialize()
         throws Exception
     {
-        consoleLogger = new ConsoleLogger();
+        consoleLogger = new ConsoleLogger( thresholdLevel );
     }
 
     public void start()
@@ -35,5 +64,41 @@
     public Logger getLogger( String name )
     {
         return consoleLogger.getChildLogger( name );
+    }
+
+    /**
+     * Sets the threshold for the console logger created by this
+     * manager.
+     *
+     * @param text The threshold level specified as a string which can
+     * be one of the following: debug, info, warn, error, fatal,
+     * disabled.
+     */
+    private void setThresholdLevel( String text )
+    {
+        if ( text.equals( "debug" ) )
+        {
+            thresholdLevel = ConsoleLogger.LEVEL_DEBUG;
+        }
+        else if ( text.equals( "info" ) )
+        {
+            thresholdLevel = ConsoleLogger.LEVEL_INFO;            
+        }
+        else if ( text.equals( "warn" ) )
+        {
+            thresholdLevel = ConsoleLogger.LEVEL_WARN;
+        }
+        else if ( text.equals( "error" ) )
+        {
+            thresholdLevel = ConsoleLogger.LEVEL_ERROR;
+        }
+        else if ( text.equals( "fatal" ) )
+        {
+            thresholdLevel = ConsoleLogger.LEVEL_FATAL;
+        }
+        else if ( text.equals( "disabled" ) )
+        {
+            thresholdLevel = ConsoleLogger.LEVEL_DISABLED;
+        }
     }
 }

Index: LoggerManagerFactory.java
===================================================================
RCS file: /cvsroot/plexus/plexus-container-new/src/java/org/apache/plexus/logging/LoggerManagerFactory.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- LoggerManagerFactory.java	3 May 2003 14:22:42 -0000	1.4
+++ LoggerManagerFactory.java	31 May 2003 15:20:06 -0000	1.5
@@ -6,6 +6,12 @@
 public class LoggerManagerFactory
     extends AbstractPlexusFactory
 {
+    /** XML element used to start the logging configuration block. */
+    public static final String LOGGING_TAG = "logging";
+
+    /** XML element used to select the logger manager implementation. */
+    private static final String IMPLEMENTATION_TAG = "implementation";
+    
     public static LoggerManager create( Configuration defaultConfiguration,
                                         Configuration configuration,
                                         ClassLoader classLoader )
@@ -14,10 +20,10 @@
         String implementation;
         boolean loggingWithNoImplementationSpecified = false;
 
-        if ( configuration.getChild( "logging", false ) != null )
+        if ( configuration.getChild( LOGGING_TAG, false ) != null )
         {
             implementation =
-                configuration.getChild( "logging" ).getChild( "implementation" ).getValue( null );
+                configuration.getChild( LOGGING_TAG ).getChild( IMPLEMENTATION_TAG ).getValue( null );
 
             if ( implementation == null
                  ||
@@ -26,18 +32,18 @@
                 loggingWithNoImplementationSpecified = true;
 
                 implementation =
-                    defaultConfiguration.getChild( "logging" ).getChild( "implementation" ).getValue();
+                    defaultConfiguration.getChild( LOGGING_TAG ).getChild( IMPLEMENTATION_TAG ).getValue();
             }
         }
         else
         {
-            implementation = defaultConfiguration.getChild( "logging" ).getChild( "implementation" ).getValue();
+            implementation = defaultConfiguration.getChild( LOGGING_TAG ).getChild( IMPLEMENTATION_TAG ).getValue();
         }
 
         LoggerManager lm =
             (LoggerManager) getInstance( implementation, classLoader );
 
-        lm.configure( configuration );
+        lm.configure( configuration.getChild( LOGGING_TAG ) );
         lm.initialize();
         lm.start();