You are viewing a plain text version of this content. The canonical link for it is here.
Posted to docs@cocoon.apache.org by do...@cocoon.apache.org on 2005/01/26 16:32:15 UTC

[Cocoon Wiki] Updated: JavaLogging

   Date: 2005-01-26T07:32:14
   Editor: AndreasHartmann
   Wiki: Cocoon Wiki
   Page: JavaLogging
   URL: http://wiki.apache.org/cocoon/JavaLogging

   no comment

Change Log:

------------------------------------------------------------------------------
@@ -27,20 +27,20 @@
 is available from the parent class {{{AbstractLogEnabled}}}. 
 [[BR]]
 [[BR]]
-{{{
-  import org.apache.avalon.framework.logger.AbstractLogEnabled;
-
-  public class SomeClass extends AbstractLogEnabled {
-                      
-  public void someMethod() {
-      ...
-      getLogger().debug( "Hello, log. It worked!" ); 
-      getLogger().info(  "Hello, log. Here is info" ); 
-      getLogger().error( "Hello, log. Here is an error" ); 
-      //..etc.
-      ...
-    }
-  }
+{{{
+  import org.apache.avalon.framework.logger.AbstractLogEnabled;
+
+  public class SomeClass extends AbstractLogEnabled {
+                      
+  public void someMethod() {
+      ...
+      getLogger().debug( "Hello, log. It worked!" ); 
+      getLogger().info(  "Hello, log. Here is info" ); 
+      getLogger().error( "Hello, log. Here is an error" ); 
+      //..etc.
+      ...
+    }
+  }
 }}}
 This works fine, provided you class doesn't already extend
 some other class.
@@ -49,24 +49,24 @@
 
 Have your class implement the {{{LogEnabled}}} interface. A typical
 class might do the following:
-{{{
-  import org.apache.avalon.framework.logger.Logger;
-  import org.apache.avalon.framework.logger.LogEnabled;
-
-  class SomeBean extends SomeOtherBean implements LogEnabled {
-    ..
-    // The LogEnabled interface is one method: enableLogging
-    private Logger logger;
-    public void enableLogging( Logger logger ) { 
-      this.logger = logger; 
-    }
-
-    // Example method that writes to the log
-    public void setThing( String thing ) {
-      logger.debug( "SomeBean: thing = " + thing );
-      ...
-    }
-  }
+{{{
+  import org.apache.avalon.framework.logger.Logger;
+  import org.apache.avalon.framework.logger.LogEnabled;
+
+  class SomeBean extends SomeOtherBean implements LogEnabled {
+    ..
+    // The LogEnabled interface is one method: enableLogging
+    private Logger logger;
+    public void enableLogging( Logger logger ) { 
+      this.logger = logger; 
+    }
+
+    // Example method that writes to the log
+    public void setThing( String thing ) {
+      logger.debug( "SomeBean: thing = " + thing );
+      ...
+    }
+  }
 }}}
 Note that in this case you use the {{{logger}}} directly and
 don't need to use the {{{getLogger()}}} accessor method. Note that
@@ -85,12 +85,12 @@
 class, which extends {{{AbstractXMLFormAction}}}: 
 [[BR]]
 [[BR]]
-{{{
-  ...
-  SomeClass myClass = new SomeClass();
-  myClass.enableLogging( getLogger() );
-  myClass.someMethod(); // Writes some log messages
-  ...
+{{{
+  ...
+  SomeClass myClass = new SomeClass();
+  myClass.enableLogging( getLogger() );
+  myClass.someMethod(); // Writes some log messages
+  ...
 }}}
 Note that many of the Cocoon classes extend Avalon Component classes. 
 [[BR]]
@@ -100,6 +100,16 @@
 always obvious when to call {{{enableLogging()}}} as the creation and 
 initialization of many of your classes  will be handled automatically 
 by Avalon, one of the Cocoon sub-systems. 
+
+The {{{ContainerUtil}}} class provides a convenient way to enable logging:
+
+{{{
+ContainerUtil.enableLogging(object, logger);
+}}}
+
+This method takes care of the following issues:
+ * The logger is only passed to the object if it implements {{{LogEnabled}}}.
+ * If the logger is {{{null}}}, an exception is thrown.
 
 === Links to Avalon Documentation ===