cocoon-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Alan Hodgkinson <>
Subject [SUMMARY] How can I log in non-Cocoon Java classes?
Date Fri, 13 Sep 2002 07:24:02 GMT

More material for the (soon to be written) Cocoon hackers guide. :)

Logging in non-Cocoon Java Classes

This explaines how to implement Cocoon-style logging, in a 
Java class that is not inherited from a Cocoon or Avalon 
component class. The class is however used within a Cocoon 

A typical use for this might be in a Java Bean used in a 
Cocoon application.

1. The class must extend AbstractLogEnabled.

2. You must enable logging in the class by calling the
   enableLogging() method on the class. This requires that you 
   have a Logger object to provide to enableLogging(). Generally 
   you can get the Logger from a Cocoon component class. In my 
   case I call enableLogging from a Cocoon action class, which 
   extends AbstractXMLFormAction.

The resulting code is as follows:

1. In the non-component class that needs to implement logging: 
   You simply call the appropriate log method using the Logger
   provided by the getLogger() method, which is available from 
   the parent class AbstractLogEnabled.

    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" ); 

2. In an Avalon component class, or a class that inherits from one:
   Call enableLogging(). Note that most of the Cocoon classes extend 
   Avalon Component classes.

      SomeClass myClass = new SomeClass();
      myClass.enableLogging( getLogger() );
      myClass.someMethod(); // Writes some log messages

Note that you must call enableLogging() before you call any methods 
that write log messages. It is not necessarily 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

To be absolutely sure that you are writing solid code, you'll need a
basic understanding of the Avalon component life-cycle. This is a big 
subject and beyond the scope of this short paper. You can read more 

Avalon logkit, which is used by Cocoon:

Avalon component lifecycle:

If you're still curious, here is a link to an excellent white paper 
explaining development using avalon:

..and that's all there is to it. 

Many thanks to Marcus Crafter and Judith Andres for their help.

Best wishes,


Please check that your question  has not already been answered in the
FAQ before posting.     <>

To unsubscribe, e-mail:     <>
For additional commands, e-mail:   <>

View raw message