> From: Alan Hodgkinson [mailto:[EMAIL PROTECTED]] > > 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 > application. > > A typical use for this might be in a Java Bean used in a > Cocoon application. > > 1. The class must extend AbstractLogEnabled.
'MUST' is a little strong word here. Isn't it enough to implement the LogEnabled interface (http://jakarta.apache.org/avalon/api/org/apache/avalon/framework/logger/Log Enabled.html) and provide methods for getting the logger? The AbstractLogEnabled is a utility helper class and the problem with it is that you'll have to extend it, which is not possible in all the cases, e.g. if your class extends some app-specific base class and you can't change it. Konstantin > > 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" ); > //..etc. > ... > } > } > > 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 > sub-systems. > > 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 > at: > > Avalon logkit, which is used by Cocoon: > http://jakarta.apache.org/avalon/logkit/whitepaper.html Avalon component lifecycle: http://jakarta.apache.org/avalon/framework/reference-the-lifecycle.html If you're still curious, here is a link to an excellent white paper explaining development using avalon: http://jakarta.apache.org/avalon/developing/index.html ..and that's all there is to it. Many thanks to Marcus Crafter and Judith Andres for their help. Best wishes, Alan. --------------------------------------------------------------------- Please check that your question has not already been answered in the FAQ before posting. <http://xml.apache.org/cocoon/faq/index.html> To unsubscribe, e-mail: <[EMAIL PROTECTED]> For additional commands, e-mail: <[EMAIL PROTECTED]> --------------------------------------------------------------------- Please check that your question has not already been answered in the FAQ before posting. <http://xml.apache.org/cocoon/faq/index.html> To unsubscribe, e-mail: <[EMAIL PROTECTED]> For additional commands, e-mail: <[EMAIL PROTECTED]>